From 4836d3b4b10bde88950792dcb3d76614102783b1 Mon Sep 17 00:00:00 2001 From: Listener430 <95291462+Listener430@users.noreply.github.com> Date: Sat, 7 Dec 2024 22:56:14 +0600 Subject: [PATCH] DEV-2797 fix (#825) --- cmd/cmd_utils.go | 37 +--------------- cmd/helmfile.go | 28 ++++++++++-- cmd/terraform.go | 26 ++++++++++- cmd/version.go | 2 +- .../exec/atlantis_generate_repo_config.go | 2 +- internal/exec/describe_affected.go | 2 +- internal/exec/describe_config.go | 2 +- internal/exec/describe_dependents.go | 2 +- internal/exec/describe_stacks.go | 2 +- internal/exec/describe_workflows.go | 3 +- internal/exec/helmfile.go | 2 +- internal/exec/helmfile_generate_varfile.go | 3 +- internal/exec/pro.go | 2 +- internal/exec/terraform.go | 2 +- internal/exec/terraform_generate_backend.go | 5 ++- internal/exec/terraform_generate_backends.go | 2 +- internal/exec/terraform_generate_varfile.go | 3 +- internal/exec/terraform_generate_varfiles.go | 2 +- internal/exec/utils.go | 7 ++- internal/exec/validate_component.go | 7 +-- internal/exec/validate_stacks.go | 2 +- internal/exec/vendor_utils.go | 2 +- internal/exec/workflow.go | 2 +- pkg/utils/version_utils.go | 43 +++++++++++++++++++ 24 files changed, 126 insertions(+), 64 deletions(-) create mode 100644 pkg/utils/version_utils.go diff --git a/cmd/cmd_utils.go b/cmd/cmd_utils.go index c3c52e3f3..3d55bf630 100644 --- a/cmd/cmd_utils.go +++ b/cmd/cmd_utils.go @@ -8,7 +8,6 @@ import ( "path" "strings" - "github.com/charmbracelet/lipgloss" "github.com/fatih/color" "github.com/spf13/cobra" @@ -429,40 +428,6 @@ func printMessageForMissingAtmosConfig(cliConfig schema.CliConfiguration) { u.PrintMessage("https://atmos.tools/quick-start\n") } -// printMessageToUpgradeToAtmosLatestRelease prints info on how to upgrade Atmos to the latest version -func printMessageToUpgradeToAtmosLatestRelease(latestVersion string) { - // Define colors - c1 := lipgloss.NewStyle().Foreground(lipgloss.Color("8")) - c2 := lipgloss.NewStyle().Foreground(lipgloss.Color("10")) - c3 := lipgloss.NewStyle().Foreground(lipgloss.Color("14")) - - // Define content - message := lipgloss.NewStyle(). - Render(fmt.Sprintf("Update available! %s » %s", - c1.Render(version.Version), - c2.Render(latestVersion))) - - links := []string{lipgloss.NewStyle().Render(fmt.Sprintf("Atmos Releases: %s", c3.Render("https://github.com/cloudposse/atmos/releases"))), - lipgloss.NewStyle().Render(fmt.Sprintf("Install Atmos: %s", c3.Render("https://atmos.tools/install"))), - } - - messageLines := append([]string{message}, links...) - messageContent := strings.Join(messageLines, "\n") - - // Define box - boxStyle := lipgloss.NewStyle(). - Border(lipgloss.RoundedBorder()). - BorderForeground(lipgloss.Color("10")). - Padding(0, 1). - Align(lipgloss.Center) - - // Render the box - box := boxStyle.Render(messageContent) - - // Print the box - fmt.Println(box) -} - // customHelpMessageToUpgradeToAtmosLatestRelease adds Atmos version info at the end of each help commnad func customHelpMessageToUpgradeToAtmosLatestRelease(cmd *cobra.Command, args []string) { originalHelpFunc(cmd, args) @@ -472,7 +437,7 @@ func customHelpMessageToUpgradeToAtmosLatestRelease(cmd *cobra.Command, args []s latestRelease := strings.TrimPrefix(latestReleaseTag, "v") currentRelease := strings.TrimPrefix(version.Version, "v") if latestRelease != currentRelease { - printMessageToUpgradeToAtmosLatestRelease(latestRelease) + u.PrintMessageToUpgradeToAtmosLatestRelease(latestRelease) } } } diff --git a/cmd/helmfile.go b/cmd/helmfile.go index d51a6c359..ae7280fe7 100644 --- a/cmd/helmfile.go +++ b/cmd/helmfile.go @@ -1,12 +1,15 @@ package cmd import ( + "strings" + "github.com/samber/lo" "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/cloudposse/atmos/pkg/version" ) // helmfileCmd represents the base command for all helmfile sub-commands @@ -17,8 +20,6 @@ var helmfileCmd = &cobra.Command{ Long: `This command runs Helmfile commands`, FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: true}, Run: func(cmd *cobra.Command, args []string) { - // Check Atmos configuration - checkAtmosConfig() var argsAfterDoubleDash []string var finalArgs = args @@ -29,7 +30,28 @@ var helmfileCmd = &cobra.Command{ argsAfterDoubleDash = lo.Slice(args, doubleDashIndex+1, len(args)) } - err := e.ExecuteHelmfileCmd(cmd, finalArgs, argsAfterDoubleDash) + info, err := e.ProcessCommandLineArgs("helmfile", cmd, finalArgs, argsAfterDoubleDash) + if err != nil { + u.LogErrorAndExit(schema.CliConfiguration{}, err) + } + + // Check for the latest Atmos release on GitHub and print update message + latestReleaseTag, err := u.GetLatestGitHubRepoRelease("cloudposse", "atmos") + if err == nil && latestReleaseTag != "" { + latestRelease := strings.TrimPrefix(latestReleaseTag, "v") + currentRelease := strings.TrimPrefix(version.Version, "v") + if latestRelease != currentRelease { + u.PrintMessageToUpgradeToAtmosLatestRelease(latestRelease) + } + } + // Exit on help + if info.NeedHelp { + return + } + // Check Atmos configuration + checkAtmosConfig() + + err = e.ExecuteHelmfile(info) if err != nil { u.LogErrorAndExit(schema.CliConfiguration{}, err) } diff --git a/cmd/terraform.go b/cmd/terraform.go index 07a8256b6..3b5ec3783 100644 --- a/cmd/terraform.go +++ b/cmd/terraform.go @@ -1,12 +1,15 @@ package cmd import ( + "strings" + "github.com/samber/lo" "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/cloudposse/atmos/pkg/version" ) // terraformCmd represents the base command for all terraform sub-commands @@ -18,7 +21,7 @@ var terraformCmd = &cobra.Command{ FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: true}, Run: func(cmd *cobra.Command, args []string) { // Check Atmos configuration - checkAtmosConfig() + //checkAtmosConfig() var argsAfterDoubleDash []string var finalArgs = args @@ -28,8 +31,27 @@ var terraformCmd = &cobra.Command{ finalArgs = lo.Slice(args, 0, doubleDashIndex) argsAfterDoubleDash = lo.Slice(args, doubleDashIndex+1, len(args)) } + info, err := e.ProcessCommandLineArgs("terraform", cmd, finalArgs, argsAfterDoubleDash) + if err != nil { + u.LogErrorAndExit(schema.CliConfiguration{}, err) + } + // Check for the latest Atmos release on GitHub and print update message + latestReleaseTag, err := u.GetLatestGitHubRepoRelease("cloudposse", "atmos") + if err == nil && latestReleaseTag != "" { + latestRelease := strings.TrimPrefix(latestReleaseTag, "v") + currentRelease := strings.TrimPrefix(version.Version, "v") + if latestRelease != currentRelease { + u.PrintMessageToUpgradeToAtmosLatestRelease(latestRelease) + } + } + // Exit on help + if info.NeedHelp { + return + } + // Check Atmos configuration + checkAtmosConfig() - err := e.ExecuteTerraformCmd(cmd, finalArgs, argsAfterDoubleDash) + err = e.ExecuteTerraform(info) if err != nil { u.LogErrorAndExit(schema.CliConfiguration{}, err) } diff --git a/cmd/version.go b/cmd/version.go index ba70483ea..5146f4ff3 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -35,7 +35,7 @@ var versionCmd = &cobra.Command{ latestRelease := strings.TrimPrefix(latestReleaseTag, "v") currentRelease := strings.TrimPrefix(version.Version, "v") if latestRelease != currentRelease { - printMessageToUpgradeToAtmosLatestRelease(latestRelease) + u.PrintMessageToUpgradeToAtmosLatestRelease(latestRelease) } } }, diff --git a/internal/exec/atlantis_generate_repo_config.go b/internal/exec/atlantis_generate_repo_config.go index 0fa651d08..7de02a4bc 100644 --- a/internal/exec/atlantis_generate_repo_config.go +++ b/internal/exec/atlantis_generate_repo_config.go @@ -19,7 +19,7 @@ import ( // ExecuteAtlantisGenerateRepoConfigCmd executes 'atlantis generate repo-config' command func ExecuteAtlantisGenerateRepoConfigCmd(cmd *cobra.Command, args []string) error { - info, err := processCommandLineArgs("", cmd, args, nil) + info, err := ProcessCommandLineArgs("", cmd, args, nil) if err != nil { return err } diff --git a/internal/exec/describe_affected.go b/internal/exec/describe_affected.go index b32f36e87..645a9b0df 100644 --- a/internal/exec/describe_affected.go +++ b/internal/exec/describe_affected.go @@ -35,7 +35,7 @@ type DescribeAffectedCmdArgs struct { } func parseDescribeAffectedCliArgs(cmd *cobra.Command, args []string) (DescribeAffectedCmdArgs, error) { - info, err := processCommandLineArgs("", cmd, args, nil) + info, err := ProcessCommandLineArgs("", cmd, args, nil) if err != nil { return DescribeAffectedCmdArgs{}, err } diff --git a/internal/exec/describe_config.go b/internal/exec/describe_config.go index 3b7cd32d6..16ed84075 100644 --- a/internal/exec/describe_config.go +++ b/internal/exec/describe_config.go @@ -15,7 +15,7 @@ func ExecuteDescribeConfigCmd(cmd *cobra.Command, args []string) error { return err } - info, err := processCommandLineArgs("", cmd, args, nil) + info, err := ProcessCommandLineArgs("", cmd, args, nil) if err != nil { return err } diff --git a/internal/exec/describe_dependents.go b/internal/exec/describe_dependents.go index 647f11185..c69663014 100644 --- a/internal/exec/describe_dependents.go +++ b/internal/exec/describe_dependents.go @@ -15,7 +15,7 @@ import ( // ExecuteDescribeDependentsCmd executes `describe dependents` command func ExecuteDescribeDependentsCmd(cmd *cobra.Command, args []string) error { - info, err := processCommandLineArgs("", cmd, args, nil) + info, err := ProcessCommandLineArgs("", cmd, args, nil) if err != nil { return err } diff --git a/internal/exec/describe_stacks.go b/internal/exec/describe_stacks.go index 4e5efcb13..d22b67554 100644 --- a/internal/exec/describe_stacks.go +++ b/internal/exec/describe_stacks.go @@ -15,7 +15,7 @@ import ( // ExecuteDescribeStacksCmd executes `describe stacks` command func ExecuteDescribeStacksCmd(cmd *cobra.Command, args []string) error { - info, err := processCommandLineArgs("", cmd, args, nil) + info, err := ProcessCommandLineArgs("", cmd, args, nil) if err != nil { return err } diff --git a/internal/exec/describe_workflows.go b/internal/exec/describe_workflows.go index c3f63df7d..af71b9046 100644 --- a/internal/exec/describe_workflows.go +++ b/internal/exec/describe_workflows.go @@ -2,13 +2,14 @@ package exec import ( "fmt" + cfg "github.com/cloudposse/atmos/pkg/config" "github.com/spf13/cobra" ) // ExecuteDescribeWorkflowsCmd executes `atmos describe workflows` CLI command func ExecuteDescribeWorkflowsCmd(cmd *cobra.Command, args []string) error { - info, err := processCommandLineArgs("terraform", cmd, args, nil) + info, err := ProcessCommandLineArgs("terraform", cmd, args, nil) if err != nil { return err } diff --git a/internal/exec/helmfile.go b/internal/exec/helmfile.go index dd422a380..bfcfde41a 100644 --- a/internal/exec/helmfile.go +++ b/internal/exec/helmfile.go @@ -18,7 +18,7 @@ import ( // ExecuteHelmfileCmd parses the provided arguments and flags and executes helmfile commands func ExecuteHelmfileCmd(cmd *cobra.Command, args []string, additionalArgsAndFlags []string) error { - info, err := processCommandLineArgs("helmfile", cmd, args, additionalArgsAndFlags) + info, err := ProcessCommandLineArgs("helmfile", cmd, args, additionalArgsAndFlags) if err != nil { return err } diff --git a/internal/exec/helmfile_generate_varfile.go b/internal/exec/helmfile_generate_varfile.go index 4c3d71c12..9a98e0944 100644 --- a/internal/exec/helmfile_generate_varfile.go +++ b/internal/exec/helmfile_generate_varfile.go @@ -3,6 +3,7 @@ package exec import ( "errors" "fmt" + "github.com/spf13/cobra" cfg "github.com/cloudposse/atmos/pkg/config" @@ -24,7 +25,7 @@ func ExecuteHelmfileGenerateVarfileCmd(cmd *cobra.Command, args []string) error component := args[0] - info, err := processCommandLineArgs("helmfile", cmd, args, nil) + info, err := ProcessCommandLineArgs("helmfile", cmd, args, nil) if err != nil { return err } diff --git a/internal/exec/pro.go b/internal/exec/pro.go index dd1ad9744..f6a221bc5 100644 --- a/internal/exec/pro.go +++ b/internal/exec/pro.go @@ -27,7 +27,7 @@ type ProUnlockCmdArgs struct { } func parseLockUnlockCliArgs(cmd *cobra.Command, args []string) (ProLockUnlockCmdArgs, error) { - info, err := processCommandLineArgs("terraform", cmd, args, nil) + info, err := ProcessCommandLineArgs("terraform", cmd, args, nil) if err != nil { return ProLockUnlockCmdArgs{}, err } diff --git a/internal/exec/terraform.go b/internal/exec/terraform.go index 4747cda29..c0d53700d 100644 --- a/internal/exec/terraform.go +++ b/internal/exec/terraform.go @@ -27,7 +27,7 @@ const ( // ExecuteTerraformCmd parses the provided arguments and flags and executes terraform commands func ExecuteTerraformCmd(cmd *cobra.Command, args []string, additionalArgsAndFlags []string) error { - info, err := processCommandLineArgs("terraform", cmd, args, additionalArgsAndFlags) + info, err := ProcessCommandLineArgs("terraform", cmd, args, additionalArgsAndFlags) if err != nil { return err } diff --git a/internal/exec/terraform_generate_backend.go b/internal/exec/terraform_generate_backend.go index eb7d940a4..0847982b8 100644 --- a/internal/exec/terraform_generate_backend.go +++ b/internal/exec/terraform_generate_backend.go @@ -2,9 +2,10 @@ package exec import ( "fmt" + "path" + "github.com/pkg/errors" "github.com/spf13/cobra" - "path" cfg "github.com/cloudposse/atmos/pkg/config" u "github.com/cloudposse/atmos/pkg/utils" @@ -25,7 +26,7 @@ func ExecuteTerraformGenerateBackendCmd(cmd *cobra.Command, args []string) error component := args[0] - info, err := processCommandLineArgs("terraform", cmd, args, nil) + info, err := ProcessCommandLineArgs("terraform", cmd, args, nil) if err != nil { return err } diff --git a/internal/exec/terraform_generate_backends.go b/internal/exec/terraform_generate_backends.go index 681b211a1..8fadf8363 100644 --- a/internal/exec/terraform_generate_backends.go +++ b/internal/exec/terraform_generate_backends.go @@ -17,7 +17,7 @@ import ( // ExecuteTerraformGenerateBackendsCmd executes `terraform generate backends` command func ExecuteTerraformGenerateBackendsCmd(cmd *cobra.Command, args []string) error { - info, err := processCommandLineArgs("terraform", cmd, args, nil) + info, err := ProcessCommandLineArgs("terraform", cmd, args, nil) if err != nil { return err } diff --git a/internal/exec/terraform_generate_varfile.go b/internal/exec/terraform_generate_varfile.go index e40992012..6ef9f8740 100644 --- a/internal/exec/terraform_generate_varfile.go +++ b/internal/exec/terraform_generate_varfile.go @@ -3,6 +3,7 @@ package exec import ( "errors" "fmt" + "github.com/spf13/cobra" cfg "github.com/cloudposse/atmos/pkg/config" @@ -24,7 +25,7 @@ func ExecuteTerraformGenerateVarfileCmd(cmd *cobra.Command, args []string) error component := args[0] - info, err := processCommandLineArgs("terraform", cmd, args, nil) + info, err := ProcessCommandLineArgs("terraform", cmd, args, nil) if err != nil { return err } diff --git a/internal/exec/terraform_generate_varfiles.go b/internal/exec/terraform_generate_varfiles.go index 556fe1932..815036e5a 100644 --- a/internal/exec/terraform_generate_varfiles.go +++ b/internal/exec/terraform_generate_varfiles.go @@ -17,7 +17,7 @@ import ( // ExecuteTerraformGenerateVarfilesCmd executes `terraform generate varfiles` command func ExecuteTerraformGenerateVarfilesCmd(cmd *cobra.Command, args []string) error { - info, err := processCommandLineArgs("terraform", cmd, args, nil) + info, err := ProcessCommandLineArgs("terraform", cmd, args, nil) if err != nil { return err } diff --git a/internal/exec/utils.go b/internal/exec/utils.go index 0d0b327bc..6fefa53ba 100644 --- a/internal/exec/utils.go +++ b/internal/exec/utils.go @@ -163,7 +163,7 @@ func ProcessComponentConfig( } // processCommandLineArgs processes command-line args -func processCommandLineArgs( +func ProcessCommandLineArgs( componentType string, cmd *cobra.Command, args []string, @@ -221,6 +221,11 @@ func processCommandLineArgs( // Check if `-h` or `--help` flags are specified if argsAndFlagsInfo.NeedHelp { + // If we're dealing with `-h` or `--help`, + // then the SubCommand should be empty. + if argsAndFlagsInfo.SubCommand == "-h" || argsAndFlagsInfo.SubCommand == "--help" { + argsAndFlagsInfo.SubCommand = "" + } err = processHelp(schema.CliConfiguration{}, componentType, argsAndFlagsInfo.SubCommand) if err != nil { return configAndStacksInfo, err diff --git a/internal/exec/validate_component.go b/internal/exec/validate_component.go index 0f622d8a7..e18e71033 100644 --- a/internal/exec/validate_component.go +++ b/internal/exec/validate_component.go @@ -2,11 +2,12 @@ package exec import ( "fmt" + "os" + "path" + "github.com/mitchellh/mapstructure" "github.com/pkg/errors" "github.com/spf13/cobra" - "os" - "path" cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/schema" @@ -15,7 +16,7 @@ import ( // ExecuteValidateComponentCmd executes `validate component` command func ExecuteValidateComponentCmd(cmd *cobra.Command, args []string) (string, string, error) { - info, err := processCommandLineArgs("", cmd, args, nil) + info, err := ProcessCommandLineArgs("", cmd, args, nil) if err != nil { return "", "", err } diff --git a/internal/exec/validate_stacks.go b/internal/exec/validate_stacks.go index 4a2a850da..81224135b 100644 --- a/internal/exec/validate_stacks.go +++ b/internal/exec/validate_stacks.go @@ -23,7 +23,7 @@ const atmosManifestDefault = "https://atmos.tools/schemas/atmos/atmos-manifest/1 // ExecuteValidateStacksCmd executes `validate stacks` command func ExecuteValidateStacksCmd(cmd *cobra.Command, args []string) error { - info, err := processCommandLineArgs("", cmd, args, nil) + info, err := ProcessCommandLineArgs("", cmd, args, nil) if err != nil { return err } diff --git a/internal/exec/vendor_utils.go b/internal/exec/vendor_utils.go index c4cb4f06f..3f4c34827 100644 --- a/internal/exec/vendor_utils.go +++ b/internal/exec/vendor_utils.go @@ -25,7 +25,7 @@ import ( // ExecuteVendorPullCommand executes `atmos vendor` commands func ExecuteVendorPullCommand(cmd *cobra.Command, args []string) error { - info, err := processCommandLineArgs("terraform", cmd, args, nil) + info, err := ProcessCommandLineArgs("terraform", cmd, args, nil) if err != nil { return err } diff --git a/internal/exec/workflow.go b/internal/exec/workflow.go index 7c41a573e..a17b85659 100644 --- a/internal/exec/workflow.go +++ b/internal/exec/workflow.go @@ -20,7 +20,7 @@ func ExecuteWorkflowCmd(cmd *cobra.Command, args []string) error { var workflowFile string var fromStep string - info, err := processCommandLineArgs("terraform", cmd, args, nil) + info, err := ProcessCommandLineArgs("terraform", cmd, args, nil) if err != nil { return err } diff --git a/pkg/utils/version_utils.go b/pkg/utils/version_utils.go new file mode 100644 index 000000000..f179da4d8 --- /dev/null +++ b/pkg/utils/version_utils.go @@ -0,0 +1,43 @@ +package utils + +import ( + "fmt" + "strings" + + "github.com/charmbracelet/lipgloss" + "github.com/cloudposse/atmos/pkg/version" +) + +// PrintMessageToUpgradeToAtmosLatestRelease prints info on how to upgrade Atmos to the latest version +func PrintMessageToUpgradeToAtmosLatestRelease(latestVersion string) { + // Define colors + c1 := lipgloss.NewStyle().Foreground(lipgloss.Color("8")) + c2 := lipgloss.NewStyle().Foreground(lipgloss.Color("10")) + c3 := lipgloss.NewStyle().Foreground(lipgloss.Color("14")) + + // Define content + message := lipgloss.NewStyle(). + Render(fmt.Sprintf("Update available! %s » %s", + c1.Render(version.Version), + c2.Render(latestVersion))) + + links := []string{lipgloss.NewStyle().Render(fmt.Sprintf("Atmos Releases: %s", c3.Render("https://github.com/cloudposse/atmos/releases"))), + lipgloss.NewStyle().Render(fmt.Sprintf("Install Atmos: %s", c3.Render("https://atmos.tools/install"))), + } + + messageLines := append([]string{message}, links...) + messageContent := strings.Join(messageLines, "\n") + + // Define box + boxStyle := lipgloss.NewStyle(). + Border(lipgloss.RoundedBorder()). + BorderForeground(lipgloss.Color("10")). + Padding(0, 1). + Align(lipgloss.Center) + + // Render the box + box := boxStyle.Render(messageContent) + + // Print the box + fmt.Println(box) +}