Skip to content

Commit

Permalink
Revert "refactor loading and checking of atmos config (#869)" (#885)
Browse files Browse the repository at this point in the history
This reverts commit 80058a8.

Co-authored-by: Erik Osterman (CEO @ Cloud Posse) <[email protected]>
  • Loading branch information
mcalhoun and osterman authored Dec 23, 2024
1 parent 262affa commit e72fad0
Show file tree
Hide file tree
Showing 24 changed files with 92 additions and 284 deletions.
3 changes: 3 additions & 0 deletions cmd/atlantis_generate_repo_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ 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: 13 additions & 9 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(atmosConfig *schema.AtmosConfiguration, opts ...AtmosValidateOption) {
func checkAtmosConfig(opts ...AtmosValidateOption) {
vCfg := &ValidateConfig{
CheckStack: true, // Default value true to check the stack
}
Expand All @@ -374,10 +374,15 @@ func checkAtmosConfig(atmosConfig *schema.AtmosConfiguration, opts ...AtmosValid
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 @@ -427,7 +432,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 @@ -436,7 +441,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 @@ -449,12 +454,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 @@ -470,15 +475,14 @@ 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: 3 additions & 0 deletions cmd/describe_affected.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ 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: 3 additions & 0 deletions cmd/describe_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ 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: 3 additions & 0 deletions cmd/describe_dependents.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ 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: 3 additions & 0 deletions cmd/describe_stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ 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: 3 additions & 2 deletions cmd/helmfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ var helmfileCmd = &cobra.Command{
// Exit on help
if info.NeedHelp {
// Check for the latest Atmos release on GitHub and print update message
atmosConfig := cmd.Context().Value(contextKey("atmos_config")).(schema.AtmosConfiguration)
CheckForAtmosUpdateAndPrintMessage(&atmosConfig)
CheckForAtmosUpdateAndPrintMessage(atmosConfig)
return
}
// Check Atmos configuration
checkAtmosConfig()

err = e.ExecuteHelmfile(info)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions cmd/helmfile_generate_varfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ 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: 10 additions & 1 deletion cmd/list_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 @@ -19,9 +20,17 @@ 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")

atmosConfig := cmd.Context().Value(contextKey("atmos_config")).(schema.AtmosConfiguration)
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
}

stacksMap, err := e.ExecuteDescribeStacks(atmosConfig, "", nil, nil, nil, false, false, false)
if err != nil {
Expand Down
11 changes: 10 additions & 1 deletion cmd/list_stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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,9 +21,17 @@ 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")

atmosConfig := cmd.Context().Value(contextKey("atmos_config")).(schema.AtmosConfiguration)
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
}

stacksMap, err := e.ExecuteDescribeStacks(atmosConfig, "", nil, nil, nil, false, false, false)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions cmd/pro_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ 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: 3 additions & 0 deletions cmd/pro_unlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ 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: 5 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

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

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

// originalHelpFunc holds Cobra's original help function to avoid recursion.
var originalHelpFunc func(*cobra.Command, []string)
Expand All @@ -47,6 +45,8 @@ 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
atmosConfig, initErr := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, true)
var initErr error
atmosConfig, initErr = cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, false)
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,13 +96,6 @@ 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: 6 additions & 2 deletions cmd/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ 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 @@ -33,10 +36,11 @@ var terraformCmd = &cobra.Command{
// Exit on help
if info.NeedHelp {
// Check for the latest Atmos release on GitHub and print update message
atmosConfig := cmd.Context().Value(contextKey("atmos_config")).(schema.AtmosConfiguration)
CheckForAtmosUpdateAndPrintMessage(&atmosConfig)
CheckForAtmosUpdateAndPrintMessage(atmosConfig)
return
}
// Check Atmos configuration
checkAtmosConfig()

err = e.ExecuteTerraform(info)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions cmd/terraform_generate_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ 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: 2 additions & 0 deletions cmd/terraform_generate_backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ 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: 3 additions & 0 deletions cmd/terraform_generate_varfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ 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: 3 additions & 0 deletions cmd/terraform_generate_varfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ 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: 3 additions & 0 deletions cmd/validate_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ 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: 3 additions & 0 deletions cmd/validate_stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ 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: 3 additions & 0 deletions cmd/vendor_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ 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: 1 addition & 2 deletions cmd/vendor_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ 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
atmosConfig := cmd.Context().Value(contextKey("atmos_config")).(schema.AtmosConfiguration)
checkAtmosConfig(&atmosConfig, WithStackValidation(cmd.Flag("stack").Changed))
checkAtmosConfig(WithStackValidation(cmd.Flag("stack").Changed))

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

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

Expand Down
Loading

0 comments on commit e72fad0

Please sign in to comment.