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

Migrate to Charmbracelet Logger #980

Merged
merged 9 commits into from
Jan 31, 2025
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
3 changes: 1 addition & 2 deletions cmd/atlantis_generate_repo_config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"github.com/cloudposse/atmos/pkg/schema"
"github.com/spf13/cobra"

e "github.com/cloudposse/atmos/internal/exec"
Expand All @@ -23,7 +22,7 @@ var atlantisGenerateRepoConfigCmd = &cobra.Command{
checkAtmosConfig()
err := e.ExecuteAtlantisGenerateRepoConfigCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
u.LogErrorAndExit(err)
}
},
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/aws_eks_update_kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/spf13/cobra"

e "github.com/cloudposse/atmos/internal/exec"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
)

Expand Down Expand Up @@ -36,7 +35,7 @@ See https://docs.aws.amazon.com/cli/latest/reference/eks/update-kubeconfig.html
Run: func(cmd *cobra.Command, args []string) {
err := e.ExecuteAwsEksUpdateKubeconfigCommand(cmd, args)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
u.LogErrorAndExit(err)
}
},
}
Expand Down
70 changes: 34 additions & 36 deletions cmd/cmd_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func processCustomCommands(
if _, exist := existingTopLevelCommands[commandConfig.Name]; exist && topLevel {
command = existingTopLevelCommands[commandConfig.Name]
} else {
var customCommand = &cobra.Command{
customCommand := &cobra.Command{
Use: commandConfig.Name,
Short: commandConfig.Description,
Long: commandConfig.Description,
Expand Down Expand Up @@ -132,21 +132,21 @@ func processCommandAliases(
aliasCmd := strings.TrimSpace(v)
aliasFor := fmt.Sprintf("alias for '%s'", aliasCmd)

var aliasCommand = &cobra.Command{
aliasCommand := &cobra.Command{
Use: alias,
Short: aliasFor,
Long: aliasFor,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: true},
Run: func(cmd *cobra.Command, args []string) {
err := cmd.ParseFlags(args)
if err != nil {
u.LogErrorAndExit(atmosConfig, err)
u.LogErrorAndExit(err)
}

commandToRun := fmt.Sprintf("%s %s %s", os.Args[0], aliasCmd, strings.Join(args, " "))
err = e.ExecuteShell(atmosConfig, commandToRun, commandToRun, ".", nil, false)
if err != nil {
u.LogErrorAndExit(atmosConfig, err)
u.LogErrorAndExit(err)
}
},
}
Expand All @@ -170,7 +170,7 @@ func preCustomCommand(
) {
var sb strings.Builder

//checking for zero arguments in config
// checking for zero arguments in config
if len(commandConfig.Arguments) == 0 {
if len(commandConfig.Steps) > 0 {
// do nothing here; let the code proceed
Expand All @@ -182,18 +182,18 @@ func preCustomCommand(
fmt.Sprintf("%d. %s %s %s\n", i+1, parentCommand.Use, commandConfig.Name, c.Name),
)
}
u.LogInfo(schema.AtmosConfiguration{}, sb.String())
u.LogInfo(sb.String())
os.Exit(1)
} else {
// truly invalid, nothing to do
u.LogError(schema.AtmosConfiguration{}, errors.New(
u.LogError(errors.New(
"invalid command: no args, no steps, no sub-commands",
))
os.Exit(1)
}
}

//Check on many arguments required and have no default value
// Check on many arguments required and have no default value
requiredNoDefaultCount := 0
for _, arg := range commandConfig.Arguments {
if arg.Required && arg.Default == "" {
Expand All @@ -218,7 +218,7 @@ func preCustomCommand(
if len(args) > 0 {
sb.WriteString(fmt.Sprintf("\nReceived %d argument(s): %s\n", len(args), strings.Join(args, ", ")))
}
u.LogErrorAndExit(schema.AtmosConfiguration{}, errors.New(sb.String()))
u.LogErrorAndExit(errors.New(sb.String()))
}

// Merge user-supplied arguments with defaults
Expand All @@ -233,7 +233,7 @@ func preCustomCommand(
} else {
// This theoretically shouldn't happen:
sb.WriteString(fmt.Sprintf("Missing required argument '%s' with no default!\n", arg.Name))
u.LogErrorAndExit(schema.AtmosConfiguration{}, errors.New(sb.String()))
u.LogErrorAndExit(errors.New(sb.String()))
}
}
}
Expand Down Expand Up @@ -297,20 +297,20 @@ func executeCustomCommand(
if fl.Type == "" || fl.Type == "string" {
providedFlag, err := flags.GetString(fl.Name)
if err != nil {
u.LogErrorAndExit(atmosConfig, err)
u.LogErrorAndExit(err)
}
flagsData[fl.Name] = providedFlag
} else if fl.Type == "bool" {
boolFlag, err := flags.GetBool(fl.Name)
if err != nil {
u.LogErrorAndExit(atmosConfig, err)
u.LogErrorAndExit(err)
}
flagsData[fl.Name] = boolFlag
}
}

// Prepare template data
var data = map[string]any{
data := map[string]any{
"Arguments": argumentsData,
"Flags": flagsData,
}
Expand All @@ -321,27 +321,27 @@ func executeCustomCommand(
// Process Go templates in the command's 'component_config.component'
component, err := e.ProcessTmpl(fmt.Sprintf("component-config-component-%d", i), commandConfig.ComponentConfig.Component, data, false)
if err != nil {
u.LogErrorAndExit(atmosConfig, err)
u.LogErrorAndExit(err)
}
if component == "" || component == "<no value>" {
u.LogErrorAndExit(atmosConfig, fmt.Errorf("the command defines an invalid 'component_config.component: %s' in '%s'",
u.LogErrorAndExit(fmt.Errorf("the command defines an invalid 'component_config.component: %s' in '%s'",
commandConfig.ComponentConfig.Component, cfg.CliConfigFileName+u.DefaultStackConfigFileExtension))
}

// Process Go templates in the command's 'component_config.stack'
stack, err := e.ProcessTmpl(fmt.Sprintf("component-config-stack-%d", i), commandConfig.ComponentConfig.Stack, data, false)
if err != nil {
u.LogErrorAndExit(atmosConfig, err)
u.LogErrorAndExit(err)
}
if stack == "" || stack == "<no value>" {
u.LogErrorAndExit(atmosConfig, fmt.Errorf("the command defines an invalid 'component_config.stack: %s' in '%s'",
u.LogErrorAndExit(fmt.Errorf("the command defines an invalid 'component_config.stack: %s' in '%s'",
commandConfig.ComponentConfig.Stack, cfg.CliConfigFileName+u.DefaultStackConfigFileExtension))
}

// Get the config for the component in the stack
componentConfig, err := e.ExecuteDescribeComponent(component, stack, true)
if err != nil {
u.LogErrorAndExit(atmosConfig, err)
u.LogErrorAndExit(err)
}
data["ComponentConfig"] = componentConfig
}
Expand All @@ -358,51 +358,51 @@ func executeCustomCommand(
err = fmt.Errorf("either 'value' or 'valueCommand' can be specified for the ENV var, but not both.\n"+
"Custom command '%s %s' defines 'value=%s' and 'valueCommand=%s' for the ENV var '%s'",
parentCommand.Name(), commandConfig.Name, value, valCommand, key)
u.LogErrorAndExit(atmosConfig, err)
u.LogErrorAndExit(err)
}

// If the command to get the value for the ENV var is provided, execute it
if valCommand != "" {
valCommandName := fmt.Sprintf("env-var-%s-valcommand", key)
res, err := e.ExecuteShellAndReturnOutput(atmosConfig, valCommand, valCommandName, ".", nil, false)
if err != nil {
u.LogErrorAndExit(atmosConfig, err)
u.LogErrorAndExit(err)
}
value = strings.TrimRight(res, "\r\n")
} else {
// Process Go templates in the values of the command's ENV vars
value, err = e.ProcessTmpl(fmt.Sprintf("env-var-%d", i), value, data, false)
if err != nil {
u.LogErrorAndExit(atmosConfig, err)
u.LogErrorAndExit(err)
}
}

envVarsList = append(envVarsList, fmt.Sprintf("%s=%s", key, value))
err = os.Setenv(key, value)
if err != nil {
u.LogErrorAndExit(atmosConfig, err)
u.LogErrorAndExit(err)
}
}

if len(envVarsList) > 0 && commandConfig.Verbose {
u.LogDebug(atmosConfig, "\nUsing ENV vars:")
u.LogDebug("\nUsing ENV vars:")
for _, v := range envVarsList {
u.LogDebug(atmosConfig, v)
u.LogDebug(v)
}
}

// Process Go templates in the command's steps.
// Steps support Go templates and have access to {{ .ComponentConfig.xxx.yyy.zzz }} Go template variables
commandToRun, err := e.ProcessTmpl(fmt.Sprintf("step-%d", i), step, data, false)
if err != nil {
u.LogErrorAndExit(atmosConfig, err)
u.LogErrorAndExit(err)
}

// Execute the command step
commandName := fmt.Sprintf("%s-step-%d", commandConfig.Name, i)
err = e.ExecuteShell(atmosConfig, commandToRun, commandName, ".", envVarsList, false)
if err != nil {
u.LogErrorAndExit(atmosConfig, err)
u.LogErrorAndExit(err)
}
}
}
Expand Down Expand Up @@ -435,7 +435,7 @@ func checkAtmosConfig(opts ...AtmosValidateOption) {

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

if vCfg.CheckStack {
Expand All @@ -455,7 +455,7 @@ func printMessageForMissingAtmosConfig(atmosConfig schema.AtmosConfiguration) {
fmt.Println()
err := tuiUtils.PrintStyledText("ATMOS")
if err != nil {
u.LogErrorAndExit(atmosConfig, err)
u.LogErrorAndExit(err)
}

if atmosConfig.Default {
Expand Down Expand Up @@ -501,7 +501,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(fmt.Sprintf("Could not load cache: %s", err))
return
}

Expand All @@ -514,12 +514,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(fmt.Sprintf("Failed to retrieve latest Atmos release info: %s", err))
return
}

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

Expand All @@ -535,8 +535,7 @@ 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(fmt.Sprintf("Unable to save cache: %s", saveErr))
}
}

Expand All @@ -554,7 +553,6 @@ func handleHelpRequest(cmd *cobra.Command, args []string) {
}

func showUsageAndExit(cmd *cobra.Command, args []string) {

var suggestions []string
unknownCommand := fmt.Sprintf("Error: Unknown command: %q\n\n", cmd.CommandPath())

Expand Down Expand Up @@ -593,7 +591,7 @@ func getConfigAndStacksInfo(commandName string, cmd *cobra.Command, args []strin
checkAtmosConfig()

var argsAfterDoubleDash []string
var finalArgs = args
finalArgs := args

doubleDashIndex := lo.IndexOf(args, "--")
if doubleDashIndex > 0 {
Expand All @@ -603,7 +601,7 @@ func getConfigAndStacksInfo(commandName string, cmd *cobra.Command, args []strin

info, err := e.ProcessCommandLineArgs(commandName, cmd, finalArgs, argsAfterDoubleDash)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
u.LogErrorAndExit(err)
}
return info
}
2 changes: 1 addition & 1 deletion cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func runCompletion(cmd *cobra.Command, args []string) {
}

if err != nil {
u.LogErrorAndExit(atmosConfig, err)
u.LogErrorAndExit(err)
}
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/describe_affected.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"github.com/cloudposse/atmos/pkg/schema"
"github.com/spf13/cobra"

e "github.com/cloudposse/atmos/internal/exec"
Expand All @@ -21,7 +20,7 @@ var describeAffectedCmd = &cobra.Command{

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

import (
"github.com/cloudposse/atmos/pkg/schema"
"github.com/spf13/cobra"

e "github.com/cloudposse/atmos/internal/exec"
Expand All @@ -20,7 +19,7 @@ var describeComponentCmd = &cobra.Command{

err := e.ExecuteDescribeComponentCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
u.LogErrorAndExit(err)
}
},
}
Expand All @@ -34,7 +33,7 @@ func init() {

err := describeComponentCmd.MarkPersistentFlagRequired("stack")
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
u.LogErrorAndExit(err)
}

describeCmd.AddCommand(describeComponentCmd)
Expand Down
4 changes: 1 addition & 3 deletions cmd/describe_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/spf13/cobra"

e "github.com/cloudposse/atmos/internal/exec"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
)

Expand All @@ -16,10 +15,9 @@ var describeConfigCmd = &cobra.Command{
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {

err := e.ExecuteDescribeConfigCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
u.LogErrorAndExit(err)
}
},
}
Expand Down
Loading
Loading