diff --git a/cmd/root.go b/cmd/root.go index d02cacf5d..37f52109d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -48,6 +48,7 @@ var RootCmd = &cobra.Command{ err := tuiUtils.PrintStyledText("ATMOS") if err != nil { u.LogErrorAndExit(schema.CliConfiguration{}, err) + return } err = e.ExecuteAtmosCmd() @@ -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: diff --git a/cmd/terraform.go b/cmd/terraform.go index 07a8256b6..b1862c992 100644 --- a/cmd/terraform.go +++ b/cmd/terraform.go @@ -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() diff --git a/internal/exec/terraform.go b/internal/exec/terraform.go index ecd6a6a3f..c61b96864 100644 --- a/internal/exec/terraform.go +++ b/internal/exec/terraform.go @@ -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 { @@ -56,16 +52,25 @@ 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 + } + 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") } diff --git a/internal/exec/utils.go b/internal/exec/utils.go index 37995a778..661674b57 100644 --- a/internal/exec/utils.go +++ b/internal/exec/utils.go @@ -41,6 +41,7 @@ var ( cfg.PlanFileFlag, cfg.HelpFlag1, cfg.HelpFlag2, + cfg.HelpFlag3, cfg.WorkflowDirFlag, cfg.JsonSchemaDirFlag, cfg.OpaDirFlag, @@ -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 } @@ -285,6 +282,16 @@ func ProcessStacks( configAndStacksInfo.StackFromArg = configAndStacksInfo.Stack + // Initialize component section maps + 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 @@ -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 { diff --git a/pkg/config/const.go b/pkg/config/const.go index 68af10127..ab815134f 100644 --- a/pkg/config/const.go +++ b/pkg/config/const.go @@ -39,6 +39,7 @@ const ( HelpFlag1 = "-h" HelpFlag2 = "--help" + HelpFlag3 = "-help" ComponentVendorConfigFileName = "component.yaml" AtmosVendorConfigFileName = "vendor"