Skip to content

Commit

Permalink
If functions that are used by Terraform providers throw errors, print…
Browse files Browse the repository at this point in the history
… errors to std.Error (#139)
  • Loading branch information
aknysh authored Apr 22, 2022
1 parent 0cf815f commit a562a02
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
12 changes: 10 additions & 2 deletions pkg/aws/aws_eks_update_kubeconfig.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package exec
package aws

import (
e "github.com/cloudposse/atmos/internal/exec"
c "github.com/cloudposse/atmos/pkg/config"
u "github.com/cloudposse/atmos/pkg/utils"
)

// ExecuteAwsEksUpdateKubeconfig executes 'aws eks update-kubeconfig'
// https://docs.aws.amazon.com/cli/latest/reference/eks/update-kubeconfig.html
func ExecuteAwsEksUpdateKubeconfig(kubeconfigContext c.AwsEksUpdateKubeconfigContext) error {
return e.ExecuteAwsEksUpdateKubeconfig(kubeconfigContext)
err := e.ExecuteAwsEksUpdateKubeconfig(kubeconfigContext)

if err != nil {
u.PrintErrorToStdError(err)
return err
}

return nil
}
8 changes: 7 additions & 1 deletion pkg/component/component_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package component
import (
e "github.com/cloudposse/atmos/internal/exec"
c "github.com/cloudposse/atmos/pkg/config"
u "github.com/cloudposse/atmos/pkg/utils"
"github.com/pkg/errors"
)

Expand All @@ -18,6 +19,7 @@ func ProcessComponentInStack(component string, stack string) (map[string]interfa
configAndStacksInfo.ComponentType = "helmfile"
configAndStacksInfo, err = e.ProcessStacks(configAndStacksInfo, true)
if err != nil {
u.PrintErrorToStdError(err)
return nil, err
}
}
Expand All @@ -29,15 +31,19 @@ func ProcessComponentInStack(component string, stack string) (map[string]interfa
func ProcessComponentFromContext(component string, tenant string, environment string, stage string) (map[string]interface{}, error) {
err := c.InitConfig()
if err != nil {
u.PrintErrorToStdError(err)
return nil, err
}

if len(c.Config.Stacks.NamePattern) < 1 {
return nil, errors.New("stack name pattern must be provided in 'stacks.name_pattern' CLI config or 'ATMOS_STACKS_NAME_PATTERN' ENV variable")
er := errors.New("stack name pattern must be provided in 'stacks.name_pattern' CLI config or 'ATMOS_STACKS_NAME_PATTERN' ENV variable")
u.PrintErrorToStdError(er)
return nil, er
}

stack, err := c.GetStackNameFromContextAndStackNamePattern(tenant, environment, stage, c.Config.Stacks.NamePattern)
if err != nil {
u.PrintErrorToStdError(err)
return nil, err
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/merge/merge.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package merge

import (
u "github.com/cloudposse/atmos/pkg/utils"
"github.com/imdario/mergo"
"gopkg.in/yaml.v2"
)
Expand All @@ -19,11 +20,13 @@ func MergeWithOptions(inputs []map[interface{}]interface{}, appendSlice, sliceDe
// so `mergo` does not have access to the original pointers
yamlCurrent, err := yaml.Marshal(current)
if err != nil {
u.PrintErrorToStdError(err)
return nil, err
}

var dataCurrent map[interface{}]interface{}
if err = yaml.Unmarshal(yamlCurrent, &dataCurrent); err != nil {
u.PrintErrorToStdError(err)
return nil, err
}

Expand All @@ -39,6 +42,7 @@ func MergeWithOptions(inputs []map[interface{}]interface{}, appendSlice, sliceDe
}

if err = mergo.Merge(&merged, dataCurrent, opts...); err != nil {
u.PrintErrorToStdError(err)
return nil, err
}
}
Expand Down
13 changes: 12 additions & 1 deletion pkg/spacelift/spacelift_stack_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,21 @@ func CreateSpaceliftStacks(
if filePaths != nil && len(filePaths) > 0 {
_, stacks, err := s.ProcessYAMLConfigFiles(basePath, filePaths, processStackDeps, processComponentDeps)
if err != nil {
u.PrintErrorToStdError(err)
return nil, err
}

return LegacyTransformStackConfigToSpaceliftStacks(stacks, stackConfigPathTemplate, processImports)
} else {
err := c.InitConfig()
if err != nil {
u.PrintErrorToStdError(err)
return nil, err
}

err = c.ProcessConfigForSpacelift()
if err != nil {
u.PrintErrorToStdError(err)
return nil, err
}

Expand All @@ -44,6 +47,7 @@ func CreateSpaceliftStacks(
processStackDeps,
processComponentDeps)
if err != nil {
u.PrintErrorToStdError(err)
return nil, err
}

Expand Down Expand Up @@ -210,6 +214,7 @@ func LegacyTransformStackConfigToSpaceliftStacks(
terraformComponentNamesInCurrentStack,
component)
if err != nil {
u.PrintErrorToStdError(err)
return nil, err
}
labels = append(labels, fmt.Sprintf("depends-on:%s", spaceliftStackName))
Expand Down Expand Up @@ -278,6 +283,7 @@ func TransformStackConfigToSpaceliftStacks(
context := c.GetContextFromVars(componentVars)
contextPrefix, err := c.GetContextPrefix(stackName, context, stackNamePattern)
if err != nil {
u.PrintErrorToStdError(err)
return nil, err
}

Expand Down Expand Up @@ -390,6 +396,7 @@ func TransformStackConfigToSpaceliftStacks(

contextPrefix, err := c.GetContextPrefix(stackName, context, stackNamePattern)
if err != nil {
u.PrintErrorToStdError(err)
return nil, err
}

Expand Down Expand Up @@ -432,6 +439,7 @@ func TransformStackConfigToSpaceliftStacks(
context,
)
if err != nil {
u.PrintErrorToStdError(err)
return nil, err
}
spaceliftConfig["workspace"] = workspace
Expand Down Expand Up @@ -465,6 +473,7 @@ func TransformStackConfigToSpaceliftStacks(
terraformComponentNamesInCurrentStack,
component)
if err != nil {
u.PrintErrorToStdError(err)
return nil, err
}
labels = append(labels, fmt.Sprintf("depends-on:%s", spaceliftStackNameDependsOn))
Expand Down Expand Up @@ -492,7 +501,9 @@ func TransformStackConfigToSpaceliftStacks(
stackName,
spaceliftStackNamePattern,
)
return nil, errors.New(errorMessage)
er := errors.New(errorMessage)
u.PrintErrorToStdError(er)
return nil, er
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/stack/stack_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
c "github.com/cloudposse/atmos/pkg/convert"
g "github.com/cloudposse/atmos/pkg/globals"
m "github.com/cloudposse/atmos/pkg/merge"
"github.com/cloudposse/atmos/pkg/utils"
u "github.com/cloudposse/atmos/pkg/utils"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -57,7 +56,7 @@ func ProcessYAMLConfigFiles(
imports = append(imports, k)
}

uniqueImports := utils.UniqueStrings(imports)
uniqueImports := u.UniqueStrings(imports)
sort.Strings(uniqueImports)

componentStackMap := map[string]map[string][]string{}
Expand Down Expand Up @@ -94,7 +93,7 @@ func ProcessYAMLConfigFiles(

stackName := strings.TrimSuffix(
strings.TrimSuffix(
utils.TrimBasePathFromPath(stackBasePath+"/", p),
u.TrimBasePathFromPath(stackBasePath+"/", p),
g.DefaultStackConfigFileExtension),
".yml",
)
Expand All @@ -110,6 +109,7 @@ func ProcessYAMLConfigFiles(
wg.Wait()

if errorResult != nil {
u.PrintErrorToStdError(errorResult)
return nil, nil, errorResult
}
return listResult, mapResult, nil
Expand Down Expand Up @@ -224,7 +224,7 @@ func ProcessConfig(

stackName := strings.TrimSuffix(
strings.TrimSuffix(
utils.TrimBasePathFromPath(basePath+"/", stack),
u.TrimBasePathFromPath(basePath+"/", stack),
g.DefaultStackConfigFileExtension),
".yml",
)
Expand Down
9 changes: 8 additions & 1 deletion pkg/utils/os_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import (

// PrintErrorToStdErrorAndExit prints errors to std.Error and exits with an error code
func PrintErrorToStdErrorAndExit(err error) {
if err != nil {
PrintErrorToStdError(err)
os.Exit(1)
}
}

// PrintErrorToStdError prints errors to std.Error
func PrintErrorToStdError(err error) {
if err != nil {
c := color.New(color.FgRed)
_, err2 := c.Fprintln(color.Error, err.Error()+"\n")
Expand All @@ -17,7 +25,6 @@ func PrintErrorToStdErrorAndExit(err error) {
fmt.Println("Original error message:")
PrintError(err)
}
os.Exit(1)
}
}

Expand Down

0 comments on commit a562a02

Please sign in to comment.