Skip to content

Commit

Permalink
refactor loading and checking of atmos config (#869)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcalhoun authored Dec 19, 2024
1 parent fe637fa commit 80058a8
Show file tree
Hide file tree
Showing 24 changed files with 284 additions and 92 deletions.
3 changes: 0 additions & 3 deletions cmd/atlantis_generate_repo_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ var atlantisGenerateRepoConfigCmd = &cobra.Command{
Long: "This command generates repository configuration for Atlantis",
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
checkAtmosConfig()

err := e.ExecuteAtlantisGenerateRepoConfigCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
Expand Down
22 changes: 9 additions & 13 deletions cmd/cmd_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func cloneCommand(orig *schema.Command) (*schema.Command, error) {
}

// checkAtmosConfig checks Atmos config
func checkAtmosConfig(opts ...AtmosValidateOption) {
func checkAtmosConfig(atmosConfig *schema.AtmosConfiguration, opts ...AtmosValidateOption) {
vCfg := &ValidateConfig{
CheckStack: true, // Default value true to check the stack
}
Expand All @@ -374,15 +374,10 @@ func checkAtmosConfig(opts ...AtmosValidateOption) {
opt(vCfg)
}

atmosConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, false)
if err != nil {
u.LogErrorAndExit(atmosConfig, err)
}

if vCfg.CheckStack {
atmosConfigExists, err := u.IsDirectory(atmosConfig.StacksBaseAbsolutePath)
if !atmosConfigExists || err != nil {
printMessageForMissingAtmosConfig(atmosConfig)
printMessageForMissingAtmosConfig(*atmosConfig)
os.Exit(0)
}
}
Expand Down Expand Up @@ -432,7 +427,7 @@ func printMessageForMissingAtmosConfig(atmosConfig schema.AtmosConfiguration) {
// CheckForAtmosUpdateAndPrintMessage checks if a version update is needed and prints a message if a newer version is found.
// It loads the cache, decides if it's time to check for updates, compares the current version to the latest available release,
// and if newer, prints the update message. It also updates the cache's timestamp after printing.
func CheckForAtmosUpdateAndPrintMessage(atmosConfig schema.AtmosConfiguration) {
func CheckForAtmosUpdateAndPrintMessage(atmosConfig *schema.AtmosConfiguration) {
// If version checking is disabled in the configuration, do nothing
if !atmosConfig.Version.Check.Enabled {
return
Expand All @@ -441,7 +436,7 @@ func CheckForAtmosUpdateAndPrintMessage(atmosConfig schema.AtmosConfiguration) {
// Load the cache
cacheCfg, err := cfg.LoadCache()
if err != nil {
u.LogWarning(atmosConfig, fmt.Sprintf("Could not load cache: %s", err))
u.LogWarning(*atmosConfig, fmt.Sprintf("Could not load cache: %s", err))
return
}

Expand All @@ -454,12 +449,12 @@ func CheckForAtmosUpdateAndPrintMessage(atmosConfig schema.AtmosConfiguration) {
// Get the latest Atmos release from GitHub
latestReleaseTag, err := u.GetLatestGitHubRepoRelease("cloudposse", "atmos")
if err != nil {
u.LogWarning(atmosConfig, fmt.Sprintf("Failed to retrieve latest Atmos release info: %s", err))
u.LogWarning(*atmosConfig, fmt.Sprintf("Failed to retrieve latest Atmos release info: %s", err))
return
}

if latestReleaseTag == "" {
u.LogWarning(atmosConfig, "No release information available")
u.LogWarning(*atmosConfig, "No release information available")
return
}

Expand All @@ -475,14 +470,15 @@ func CheckForAtmosUpdateAndPrintMessage(atmosConfig schema.AtmosConfiguration) {
// Update the cache to mark the current timestamp
cacheCfg.LastChecked = time.Now().Unix()
if saveErr := cfg.SaveCache(cacheCfg); saveErr != nil {
u.LogWarning(atmosConfig, fmt.Sprintf("Unable to save cache: %s", saveErr))
u.LogWarning(*atmosConfig, fmt.Sprintf("Unable to save cache: %s", saveErr))

}
}

func customHelpMessageToUpgradeToAtmosLatestRelease(cmd *cobra.Command, args []string) {
atmosConfig := cmd.Context().Value(contextKey("atmos_config")).(schema.AtmosConfiguration)
originalHelpFunc(cmd, args)
CheckForAtmosUpdateAndPrintMessage(atmosConfig)
CheckForAtmosUpdateAndPrintMessage(&atmosConfig)
}

// Check Atmos is version command
Expand Down
3 changes: 0 additions & 3 deletions cmd/describe_affected.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ var describeAffectedCmd = &cobra.Command{
Long: `This command produces a list of the affected Atmos components and stacks given two Git commits: atmos describe affected [options]`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
checkAtmosConfig()

err := e.ExecuteDescribeAffectedCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
Expand Down
3 changes: 0 additions & 3 deletions cmd/describe_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ var describeComponentCmd = &cobra.Command{
Long: `This command shows configuration for an Atmos component in an Atmos stack: atmos describe component <component> -s <stack>`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
checkAtmosConfig()

err := e.ExecuteDescribeComponentCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
Expand Down
3 changes: 0 additions & 3 deletions cmd/describe_dependents.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ var describeDependentsCmd = &cobra.Command{
Long: `This command produces a list of Atmos components in Atmos stacks that depend on the provided Atmos component: atmos describe dependents [options]`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
checkAtmosConfig()

err := e.ExecuteDescribeDependentsCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
Expand Down
3 changes: 0 additions & 3 deletions cmd/describe_stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ var describeStacksCmd = &cobra.Command{
Long: `This command shows configuration for atmos stacks and components in the stacks: atmos describe stacks [options]`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
checkAtmosConfig()

err := e.ExecuteDescribeStacksCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
Expand Down
5 changes: 2 additions & 3 deletions cmd/helmfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ var helmfileCmd = &cobra.Command{
// Exit on help
if info.NeedHelp {
// Check for the latest Atmos release on GitHub and print update message
CheckForAtmosUpdateAndPrintMessage(atmosConfig)
atmosConfig := cmd.Context().Value(contextKey("atmos_config")).(schema.AtmosConfiguration)
CheckForAtmosUpdateAndPrintMessage(&atmosConfig)
return
}
// Check Atmos configuration
checkAtmosConfig()

err = e.ExecuteHelmfile(info)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions cmd/helmfile_generate_varfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ var helmfileGenerateVarfileCmd = &cobra.Command{
Long: `This command generates a varfile for an atmos helmfile component: atmos helmfile generate varfile <component> -s <stack> -f <file>`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
checkAtmosConfig()

err := e.ExecuteHelmfileGenerateVarfileCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
Expand Down
11 changes: 1 addition & 10 deletions cmd/list_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

e "github.com/cloudposse/atmos/internal/exec"
"github.com/cloudposse/atmos/pkg/config"
l "github.com/cloudposse/atmos/pkg/list"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
Expand All @@ -20,17 +19,9 @@ var listComponentsCmd = &cobra.Command{
Example: "atmos list components\n" +
"atmos list components -s <stack>",
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
checkAtmosConfig()

stackFlag, _ := cmd.Flags().GetString("stack")

configAndStacksInfo := schema.ConfigAndStacksInfo{}
atmosConfig, err := config.InitCliConfig(configAndStacksInfo, true)
if err != nil {
u.PrintMessageInColor(fmt.Sprintf("Error initializing CLI config: %v", err), color.New(color.FgRed))
return
}
atmosConfig := cmd.Context().Value(contextKey("atmos_config")).(schema.AtmosConfiguration)

stacksMap, err := e.ExecuteDescribeStacks(atmosConfig, "", nil, nil, nil, false, false, false)
if err != nil {
Expand Down
11 changes: 1 addition & 10 deletions cmd/list_stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

e "github.com/cloudposse/atmos/internal/exec"
"github.com/cloudposse/atmos/pkg/config"
l "github.com/cloudposse/atmos/pkg/list"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
Expand All @@ -21,17 +20,9 @@ var listStacksCmd = &cobra.Command{
"atmos list stacks -c <component>",
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
checkAtmosConfig()

componentFlag, _ := cmd.Flags().GetString("component")

configAndStacksInfo := schema.ConfigAndStacksInfo{}
atmosConfig, err := config.InitCliConfig(configAndStacksInfo, true)
if err != nil {
u.PrintMessageInColor(fmt.Sprintf("Error initializing CLI config: %v", err), color.New(color.FgRed))
return
}
atmosConfig := cmd.Context().Value(contextKey("atmos_config")).(schema.AtmosConfiguration)

stacksMap, err := e.ExecuteDescribeStacks(atmosConfig, "", nil, nil, nil, false, false, false)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions cmd/pro_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ var proLockCmd = &cobra.Command{
Long: `This command calls the atmos pro API and locks a stack`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
checkAtmosConfig()

err := e.ExecuteProLockCommand(cmd, args)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
Expand Down
3 changes: 0 additions & 3 deletions cmd/pro_unlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ var proUnlockCmd = &cobra.Command{
Long: `This command calls the atmos pro API and unlocks a stack`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
checkAtmosConfig()

err := e.ExecuteProUnlockCommand(cmd, args)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
Expand Down
17 changes: 12 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"errors"
"fmt"
"os"
Expand All @@ -18,7 +19,8 @@ import (
u "github.com/cloudposse/atmos/pkg/utils"
)

var atmosConfig schema.AtmosConfiguration
// contextKey is a type alias for a string to avoid collisions with other context keys
type contextKey string

// originalHelpFunc holds Cobra's original help function to avoid recursion.
var originalHelpFunc func(*cobra.Command, []string)
Expand All @@ -45,8 +47,6 @@ var RootCmd = &cobra.Command{
}
},
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
checkAtmosConfig()

// Print a styled Atmos logo to the terminal
fmt.Println()
Expand Down Expand Up @@ -83,11 +83,11 @@ func Execute() error {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
}
}

// InitCliConfig finds and merges CLI configurations in the following order:
// system dir, home dir, current dir, ENV vars, command-line arguments
// Here we need the custom commands from the config
var initErr error
atmosConfig, initErr = cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, false)
atmosConfig, initErr := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, true)
if initErr != nil && !errors.Is(initErr, cfg.NotFound) {
if isVersionCommand() {
u.LogTrace(schema.AtmosConfiguration{}, fmt.Sprintf("warning: CLI configuration 'atmos.yaml' file not found. Error: %s", initErr))
Expand All @@ -96,6 +96,13 @@ func Execute() error {
}
}

// Check Atmos configuration now that we have fully parsed it
checkAtmosConfig(&atmosConfig)

// Now that we have validated the Atmos config, add it to the context so it can be used by subcommands
ctx := context.WithValue(context.Background(), contextKey("atmos_config"), atmosConfig)
RootCmd.SetContext(ctx)

// Save the original help function to prevent infinite recursion when overriding it.
// This allows us to call the original help functionality within our custom help function.
originalHelpFunc = RootCmd.HelpFunc()
Expand Down
8 changes: 2 additions & 6 deletions cmd/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ var terraformCmd = &cobra.Command{
Long: `This command executes Terraform 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 @@ -36,11 +33,10 @@ var terraformCmd = &cobra.Command{
// Exit on help
if info.NeedHelp {
// Check for the latest Atmos release on GitHub and print update message
CheckForAtmosUpdateAndPrintMessage(atmosConfig)
atmosConfig := cmd.Context().Value(contextKey("atmos_config")).(schema.AtmosConfiguration)
CheckForAtmosUpdateAndPrintMessage(&atmosConfig)
return
}
// Check Atmos configuration
checkAtmosConfig()

err = e.ExecuteTerraform(info)
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions cmd/terraform_generate_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ var terraformGenerateBackendCmd = &cobra.Command{
Long: `This command generates the backend config for a terraform component: atmos terraform generate backend <component> -s <stack>`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
checkAtmosConfig()

err := e.ExecuteTerraformGenerateBackendCmd(cmd, args)
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions cmd/terraform_generate_backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ var terraformGenerateBackendsCmd = &cobra.Command{
Long: `This command generates backend configs for all terraform components`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
checkAtmosConfig()

err := e.ExecuteTerraformGenerateBackendsCmd(cmd, args)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions cmd/terraform_generate_varfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ var terraformGenerateVarfileCmd = &cobra.Command{
Long: `This command generates a varfile for an atmos terraform component: atmos terraform generate varfile <component> -s <stack> -f <file>`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
checkAtmosConfig()

err := e.ExecuteTerraformGenerateVarfileCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
Expand Down
3 changes: 0 additions & 3 deletions cmd/terraform_generate_varfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ var terraformGenerateVarfilesCmd = &cobra.Command{
Long: `This command generates varfiles for all atmos terraform components in all stacks`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
checkAtmosConfig()

err := e.ExecuteTerraformGenerateVarfilesCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
Expand Down
3 changes: 0 additions & 3 deletions cmd/validate_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ var validateComponentCmd = &cobra.Command{
"atmos validate component <component> -s <stack> --schema-path <schema_path> --schema-type opa --module-paths catalog",
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
checkAtmosConfig()

component, stack, err := e.ExecuteValidateComponentCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
Expand Down
3 changes: 0 additions & 3 deletions cmd/validate_stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ var ValidateStacksCmd = &cobra.Command{
Example: "validate stacks",
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
checkAtmosConfig()

err := e.ExecuteValidateStacksCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
Expand Down
3 changes: 0 additions & 3 deletions cmd/vendor_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ var vendorDiffCmd = &cobra.Command{
Long: `This command executes 'atmos vendor diff' CLI commands`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
checkAtmosConfig()

err := e.ExecuteVendorDiffCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
Expand Down
3 changes: 2 additions & 1 deletion cmd/vendor_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var vendorPullCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// WithStackValidation is a functional option that enables/disables stack configuration validation
// based on whether the --stack flag is provided
checkAtmosConfig(WithStackValidation(cmd.Flag("stack").Changed))
atmosConfig := cmd.Context().Value(contextKey("atmos_config")).(schema.AtmosConfiguration)
checkAtmosConfig(&atmosConfig, WithStackValidation(cmd.Flag("stack").Changed))

err := e.ExecuteVendorPullCmd(cmd, args)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ var versionCmd = &cobra.Command{
}

// Check for the cache and print update message
CheckForAtmosUpdateAndPrintMessage(atmosConfig)
atmosConfig := cmd.Context().Value(contextKey("atmos_config")).(schema.AtmosConfiguration)
CheckForAtmosUpdateAndPrintMessage(&atmosConfig)
},
}

Expand Down
Loading

0 comments on commit 80058a8

Please sign in to comment.