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

Any usage of --help should not require stack configurations #825

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 1 addition & 36 deletions cmd/cmd_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path"
"strings"

"github.com/charmbracelet/lipgloss"
"github.com/fatih/color"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}
}
Expand Down
28 changes: 25 additions & 3 deletions cmd/helmfile.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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)
}
Expand Down
26 changes: 24 additions & 2 deletions cmd/terraform.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/atlantis_generate_repo_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/describe_affected.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/describe_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/describe_dependents.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/describe_stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion internal/exec/describe_workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/helmfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion internal/exec/helmfile_generate_varfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package exec
import (
"errors"
"fmt"

"github.com/spf13/cobra"

cfg "github.com/cloudposse/atmos/pkg/config"
Expand All @@ -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
}
Listener430 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
aknysh marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions internal/exec/terraform_generate_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package exec

import (
"fmt"
"path"
aknysh marked this conversation as resolved.
Show resolved Hide resolved

"github.com/pkg/errors"
"github.com/spf13/cobra"
"path"

cfg "github.com/cloudposse/atmos/pkg/config"
u "github.com/cloudposse/atmos/pkg/utils"
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/terraform_generate_backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion internal/exec/terraform_generate_varfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package exec
import (
"errors"
"fmt"

"github.com/spf13/cobra"

cfg "github.com/cloudposse/atmos/pkg/config"
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/terraform_generate_varfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 6 additions & 1 deletion internal/exec/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func ProcessComponentConfig(
}

// processCommandLineArgs processes command-line args
func processCommandLineArgs(
func ProcessCommandLineArgs(
componentType string,
cmd *cobra.Command,
args []string,
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions internal/exec/validate_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/validate_stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/vendor_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading
Loading