Skip to content

Commit

Permalink
Improve error stack trace. Add --stack flag to `atmos describe affe…
Browse files Browse the repository at this point in the history
…cted` command. Improve `atmos.Component` template function (#714)

* updates

* Print stack trace on error only in log level `Trace`

* Don't process Terraform output for abstract and disabled components in `atmos.Component` template function

* Don't process Terraform output for abstract and disabled components in `atmos.Component` template function

* Add `--stack` flag to `atmos describe affected` command

* Add `--stack` flag to `atmos describe affected` command

* Add `--stack` flag to `atmos describe affected` command

* Add `--stack` flag to `atmos describe affected` command

* Add `--stack` flag to `atmos describe affected` command
  • Loading branch information
aknysh authored Oct 12, 2024
1 parent 7c41087 commit 8c6591b
Show file tree
Hide file tree
Showing 54 changed files with 444 additions and 548 deletions.
3 changes: 2 additions & 1 deletion cmd/atlantis_generate_repo_config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/cloudposse/atmos/pkg/schema"
"github.com/spf13/cobra"

e "github.com/cloudposse/atmos/internal/exec"
Expand All @@ -19,7 +20,7 @@ var atlantisGenerateRepoConfigCmd = &cobra.Command{

err := e.ExecuteAtlantisGenerateRepoConfigCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/aws_eks_update_kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"

e "github.com/cloudposse/atmos/internal/exec"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
)

Expand Down Expand Up @@ -36,7 +37,7 @@ See https://docs.aws.amazon.com/cli/latest/reference/eks/update-kubeconfig.html
Run: func(cmd *cobra.Command, args []string) {
err := e.ExecuteAwsEksUpdateKubeconfigCommand(cmd, args)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}
},
}
Expand Down
34 changes: 17 additions & 17 deletions cmd/cmd_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ func processCommandAliases(
Run: func(cmd *cobra.Command, args []string) {
err := cmd.ParseFlags(args)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(cliConfig, err)
}

commandToRun := fmt.Sprintf("%s %s %s", os.Args[0], aliasCmd, strings.Join(args, " "))
err = e.ExecuteShell(cliConfig, commandToRun, commandToRun, ".", nil, false)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(cliConfig, err)
}
},
}
Expand All @@ -153,7 +153,7 @@ func preCustomCommand(

if len(args) != len(commandConfig.Arguments) {
err = fmt.Errorf("invalid number of arguments, %d argument(s) required", len(commandConfig.Arguments))
u.LogErrorAndExit(err)
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}

// no "steps" means a sub command should be specified
Expand Down Expand Up @@ -203,7 +203,7 @@ func executeCustomCommand(
if fl.Type == "" || fl.Type == "string" {
providedFlag, err := flags.GetString(fl.Name)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(cliConfig, err)
}
flagsData[fl.Name] = providedFlag
}
Expand All @@ -221,27 +221,27 @@ func executeCustomCommand(
// Process Go templates in the command's 'component_config.component'
component, err := e.ProcessTmpl(fmt.Sprintf("component-config-component-%d", i), commandConfig.ComponentConfig.Component, data, false)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(cliConfig, err)
}
if component == "" || component == "<no value>" {
u.LogErrorAndExit(fmt.Errorf("the command defines an invalid 'component_config.component: %s' in '%s'",
u.LogErrorAndExit(cliConfig, fmt.Errorf("the command defines an invalid 'component_config.component: %s' in '%s'",
commandConfig.ComponentConfig.Component, cfg.CliConfigFileName))
}

// Process Go templates in the command's 'component_config.stack'
stack, err := e.ProcessTmpl(fmt.Sprintf("component-config-stack-%d", i), commandConfig.ComponentConfig.Stack, data, false)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(cliConfig, err)
}
if stack == "" || stack == "<no value>" {
u.LogErrorAndExit(fmt.Errorf("the command defines an invalid 'component_config.stack: %s' in '%s'",
u.LogErrorAndExit(cliConfig, fmt.Errorf("the command defines an invalid 'component_config.stack: %s' in '%s'",
commandConfig.ComponentConfig.Stack, cfg.CliConfigFileName))
}

// Get the config for the component in the stack
componentConfig, err := e.ExecuteDescribeComponent(component, stack, true)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(cliConfig, err)
}
data["ComponentConfig"] = componentConfig
}
Expand All @@ -258,29 +258,29 @@ func executeCustomCommand(
err = fmt.Errorf("either 'value' or 'valueCommand' can be specified for the ENV var, but not both.\n"+
"Custom command '%s %s' defines 'value=%s' and 'valueCommand=%s' for the ENV var '%s'",
parentCommand.Name(), commandConfig.Name, value, valCommand, key)
u.LogErrorAndExit(err)
u.LogErrorAndExit(cliConfig, err)
}

// If the command to get the value for the ENV var is provided, execute it
if valCommand != "" {
valCommandName := fmt.Sprintf("env-var-%s-valcommand", key)
res, err := e.ExecuteShellAndReturnOutput(cliConfig, valCommand, valCommandName, ".", nil, false)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(cliConfig, err)
}
value = strings.TrimRight(res, "\r\n")
} else {
// Process Go templates in the values of the command's ENV vars
value, err = e.ProcessTmpl(fmt.Sprintf("env-var-%d", i), value, data, false)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(cliConfig, err)
}
}

envVarsList = append(envVarsList, fmt.Sprintf("%s=%s", key, value))
err = os.Setenv(key, value)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(cliConfig, err)
}
}

Expand All @@ -295,14 +295,14 @@ func executeCustomCommand(
// Steps support Go templates and have access to {{ .ComponentConfig.xxx.yyy.zzz }} Go template variables
commandToRun, err := e.ProcessTmpl(fmt.Sprintf("step-%d", i), step, data, false)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(cliConfig, err)
}

// Execute the command step
commandName := fmt.Sprintf("%s-step-%d", commandConfig.Name, i)
err = e.ExecuteShell(cliConfig, commandToRun, commandName, ".", envVarsList, false)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(cliConfig, err)
}
}
}
Expand All @@ -326,7 +326,7 @@ func cloneCommand(orig *schema.Command) (*schema.Command, error) {
func checkAtmosConfig() {
cliConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, false)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(cliConfig, err)
}

atmosConfigExists, err := u.IsDirectory(cliConfig.StacksBaseAbsolutePath)
Expand All @@ -345,7 +345,7 @@ func printMessageForMissingAtmosConfig(cliConfig schema.CliConfiguration) {
fmt.Println()
err := tuiUtils.PrintStyledText("ATMOS")
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(cliConfig, err)
}

if cliConfig.Default {
Expand Down
3 changes: 2 additions & 1 deletion cmd/completion.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/cloudposse/atmos/pkg/schema"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -30,7 +31,7 @@ var completionCmd = &cobra.Command{
}

if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}
},
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/describe_affected.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/cloudposse/atmos/pkg/schema"
"github.com/spf13/cobra"

e "github.com/cloudposse/atmos/internal/exec"
Expand All @@ -19,7 +20,7 @@ var describeAffectedCmd = &cobra.Command{

err := e.ExecuteDescribeAffectedCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}
},
}
Expand All @@ -39,6 +40,7 @@ func init() {
describeAffectedCmd.PersistentFlags().Bool("include-dependents", false, "Include the dependent components and stacks: atmos describe affected --include-dependents=true")
describeAffectedCmd.PersistentFlags().Bool("include-settings", false, "Include the 'settings' section for each affected component: atmos describe affected --include-settings=true")
describeAffectedCmd.PersistentFlags().Bool("upload", false, "Upload the affected components and stacks to a specified HTTP endpoint: atmos describe affected --upload=true")
describeAffectedCmd.PersistentFlags().StringP("stack", "s", "", "atmos describe affected -s <stack>")
describeAffectedCmd.PersistentFlags().Bool("clone-target-ref", false, "Clone the target reference with which to compare the current branch: atmos describe affected --clone-target-ref=true\n"+
"If set to 'false' (default), the target reference will be checked out instead\n"+
"This requires that the target reference is already cloned by Git, and the information about it exists in the '.git' directory")
Expand Down
5 changes: 3 additions & 2 deletions cmd/describe_component.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/cloudposse/atmos/pkg/schema"
"github.com/spf13/cobra"

e "github.com/cloudposse/atmos/internal/exec"
Expand All @@ -19,7 +20,7 @@ var describeComponentCmd = &cobra.Command{

err := e.ExecuteDescribeComponentCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}
},
}
Expand All @@ -33,7 +34,7 @@ func init() {

err := describeComponentCmd.MarkPersistentFlagRequired("stack")
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}

describeCmd.AddCommand(describeComponentCmd)
Expand Down
3 changes: 2 additions & 1 deletion cmd/describe_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"

e "github.com/cloudposse/atmos/internal/exec"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
)

Expand All @@ -16,7 +17,7 @@ var describeConfigCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
err := e.ExecuteDescribeConfigCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}
},
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/describe_dependents.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/cloudposse/atmos/pkg/schema"
"github.com/spf13/cobra"

e "github.com/cloudposse/atmos/internal/exec"
Expand All @@ -20,7 +21,7 @@ var describeDependentsCmd = &cobra.Command{

err := e.ExecuteDescribeDependentsCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}
},
}
Expand All @@ -34,7 +35,7 @@ func init() {

err := describeDependentsCmd.MarkPersistentFlagRequired("stack")
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}

describeCmd.AddCommand(describeDependentsCmd)
Expand Down
3 changes: 2 additions & 1 deletion cmd/describe_stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"

e "github.com/cloudposse/atmos/internal/exec"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
)

Expand All @@ -19,7 +20,7 @@ var describeStacksCmd = &cobra.Command{

err := e.ExecuteDescribeStacksCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/describe_workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"

e "github.com/cloudposse/atmos/internal/exec"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
)

Expand All @@ -23,7 +24,7 @@ var describeWorkflowsCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
err := e.ExecuteDescribeWorkflowsCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/spf13/cobra"

"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
)

Expand Down Expand Up @@ -37,7 +38,7 @@ var docsCmd = &cobra.Command{
}

if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/helmfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/spf13/cobra"

e "github.com/cloudposse/atmos/internal/exec"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
)

Expand All @@ -30,7 +31,7 @@ var helmfileCmd = &cobra.Command{

err := e.ExecuteHelmfileCmd(cmd, finalArgs, argsAfterDoubleDash)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}
},
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/helmfile_generate_varfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"

e "github.com/cloudposse/atmos/internal/exec"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
)

Expand All @@ -19,7 +20,7 @@ var helmfileGenerateVarfileCmd = &cobra.Command{

err := e.ExecuteHelmfileGenerateVarfileCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}
},
}
Expand All @@ -31,7 +32,7 @@ func init() {

err := helmfileGenerateVarfileCmd.MarkPersistentFlagRequired("stack")
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}

helmfileGenerateCmd.AddCommand(helmfileGenerateVarfileCmd)
Expand Down
6 changes: 4 additions & 2 deletions cmd/pro_lock.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cmd

import (
"github.com/spf13/cobra"

e "github.com/cloudposse/atmos/internal/exec"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
"github.com/spf13/cobra"
)

// proLockCmd executes 'pro lock' CLI command
Expand All @@ -18,7 +20,7 @@ var proLockCmd = &cobra.Command{

err := e.ExecuteProLockCommand(cmd, args)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}
},
}
Expand Down
Loading

0 comments on commit 8c6591b

Please sign in to comment.