Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Terraform help printing log twice and causing error #798

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var RootCmd = &cobra.Command{
err := tuiUtils.PrintStyledText("ATMOS")
if err != nil {
u.LogErrorAndExit(schema.CliConfiguration{}, err)
return
}

err = e.ExecuteAtmosCmd()
Expand All @@ -73,10 +74,6 @@ func Execute() error {
err := RootCmd.ParseFlags(os.Args)
if err != nil && errors.Is(err, pflag.ErrHelp) {
fmt.Println()
err = tuiUtils.PrintStyledText("ATMOS")
if err != nil {
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}
}

// InitCliConfig finds and merges CLI configurations in the following order:
Expand Down
9 changes: 9 additions & 0 deletions cmd/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ var terraformCmd = &cobra.Command{
Long: `This command executes Terraform commands`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: true},
Run: func(cmd *cobra.Command, args []string) {
// Check if help is requested
if cmd.Flags().Changed("help") || (len(args) > 0 && args[0] == "help") {
err := e.ExecuteTerraformCmd(cmd, args, nil)
if err != nil {
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}
return
}

// Check Atmos configuration
checkAtmosConfig()

Expand Down
33 changes: 22 additions & 11 deletions internal/exec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,13 @@ func ExecuteTerraformCmd(cmd *cobra.Command, args []string, additionalArgsAndFla

// ExecuteTerraform executes terraform commands
func ExecuteTerraform(info schema.ConfigAndStacksInfo) error {
cliConfig, err := cfg.InitCliConfig(info, true)
if err != nil {
return err
}

if info.NeedHelp {
return nil
}
// For help commands and empty subcommand, we don't need to process stacks
if info.NeedHelp || info.SubCommand == cfg.HelpFlag1 || info.SubCommand == cfg.HelpFlag2 || info.SubCommand == "" {
cliConfig, err := cfg.InitCliConfig(info, false)
if err != nil {
return err
}

// If the user just types `atmos terraform`, print Atmos logo and show terraform help
if info.SubCommand == "" {
fmt.Println()
err = tuiUtils.PrintStyledText("ATMOS")
if err != nil {
Expand All @@ -56,16 +52,31 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error {
if err != nil {
return err
}

fmt.Println()
return nil
}

// For all other commands, we need to process stacks
cliConfig, err := cfg.InitCliConfig(info, true)
if err != nil {
return err
}

fmt.Println()
err = tuiUtils.PrintStyledText("ATMOS")
Cerebrovinny marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

info, err = ProcessStacks(cliConfig, info, true, true)
if err != nil {
return err
}

if info.ComponentFromArg == "" {
return errors.New("component must be specified")
}

if len(info.Stack) < 1 {
return errors.New("stack must be specified")
}
Expand Down
20 changes: 15 additions & 5 deletions internal/exec/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
cfg.PlanFileFlag,
cfg.HelpFlag1,
cfg.HelpFlag2,
cfg.HelpFlag3,
cfg.WorkflowDirFlag,
cfg.JsonSchemaDirFlag,
cfg.OpaDirFlag,
Expand Down Expand Up @@ -221,10 +222,6 @@ func processCommandLineArgs(

// Check if `-h` or `--help` flags are specified
if argsAndFlagsInfo.NeedHelp {
err = processHelp(schema.CliConfiguration{}, componentType, argsAndFlagsInfo.SubCommand)
if err != nil {
return configAndStacksInfo, err
}
return configAndStacksInfo, nil
}

Expand Down Expand Up @@ -285,6 +282,16 @@ func ProcessStacks(

configAndStacksInfo.StackFromArg = configAndStacksInfo.Stack

// Initialize component section maps
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Cerebrovinny why this is needed? Did it cause issues w/o this?
If needed, please add more info in the comment

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

processHelp func is not used anymore?

Copy link
Collaborator Author

@Cerebrovinny Cerebrovinny Nov 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ProcessComponentConfig is causing it to display nil pointer dereference errors in tests when we process the stack
so we need to initialize it first before accessing.

configAndStacksInfo.ComponentSection = make(map[string]any)
configAndStacksInfo.ComponentVarsSection = make(map[string]any)
configAndStacksInfo.ComponentSettingsSection = make(map[string]any)
configAndStacksInfo.ComponentOverridesSection = make(map[string]any)
configAndStacksInfo.ComponentProvidersSection = make(map[string]any)
configAndStacksInfo.ComponentEnvSection = make(map[string]any)
configAndStacksInfo.ComponentBackendSection = make(map[string]any)
configAndStacksInfo.ComponentMetadataSection = make(map[string]any)

stacksMap, rawStackConfigs, err := FindStacksMap(cliConfig, false)
if err != nil {
return configAndStacksInfo, err
Expand Down Expand Up @@ -956,8 +963,11 @@ func processArgsAndFlags(componentType string, inputArgsAndFlags []string) (sche
info.SkipInit = true
}

if arg == cfg.HelpFlag1 || arg == cfg.HelpFlag2 {
if arg == cfg.HelpFlag1 || arg == cfg.HelpFlag2 || arg == cfg.HelpFlag3 {
info.NeedHelp = true
// For help commands, we don't need a component or stack
info.ComponentFromArg = ""
return info, nil
}

for _, f := range commonFlags {
Expand Down
1 change: 1 addition & 0 deletions pkg/config/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (

HelpFlag1 = "-h"
HelpFlag2 = "--help"
HelpFlag3 = "-help"

ComponentVendorConfigFileName = "component.yaml"
AtmosVendorConfigFileName = "vendor"
Expand Down