10000 fix log level priorty by samtholiya · Pull Request #1031 · cloudposse/atmos · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix log level priorty #1031

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

Merged
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
67 changes: 60 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ var RootCmd = &cobra.Command{
cmd.SilenceUsage = true
cmd.SilenceErrors = true
}

logsLevel, _ := cmd.Flags().GetString("logs-level")
logsFile, _ := cmd.Flags().GetString("logs-file")

configAndStacksInfo := schema.ConfigAndStacksInfo{
LogsLevel: logsLevel,
LogsFile: logsFile,
configAndStacksInfo := schema.ConfigAndStacksInfo{}
// TODO: Check if these value being set was actually required
if cmd.Flags().Changed("logs-level") {
logsLevel, _ := cmd.Flags().GetString("logs-level")
configAndStacksInfo.LogsLevel = logsLevel
}
if cmd.Flags().Changed("logs-file") {
logsFile, _ := cmd.Flags().GetString("logs-file")
configAndStacksInfo.LogsFile = logsFile
}

// Only validate the config, don't store it yet since commands may need to add more info
Expand Down Expand Up @@ -122,6 +124,42 @@ func setupLogger(atmosConfig *schema.AtmosConfiguration) {
log.SetOutput(output)
}

// TODO: This function works well, but we should generally avoid implementing manual flag parsing,
// as Cobra typically handles this.

// If there's no alternative, this approach may be necessary.
// However, this TODO serves as a reminder to revisit and verify if a better solution exists.

// Function to manually parse flags with double dash "--" like Cobra
func parseFlags(args []string) (map[string]string, error) {
flags := make(map[string]string)
for i := 0; i < len(args); i++ {
arg := args[i]

// Check if the argument starts with '--' (double dash)
if strings.HasPrefix(arg, "--") {
// Strip the '--' prefix and check if it's followed by a value
arg = arg[2:]
if strings.Contains(arg, "=") {
// Case like --flag=value
parts := strings.SplitN(arg, "=", 2)
flags[parts[0]] = parts[1]
} else if i+1 < len(args) && !strings.HasPrefix(args[i+1], "--") {
// Case like --flag value
flags[arg] = args[i+1]
i++ // Skip the next argument as it's the value
} else {
// Case where flag has no value, e.g., --flag (we set it to "true")
flags[arg] = "true"
}
} else {
// It's a regular argument, not a flag, so we skip
continue
}
}
return flags, nil
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the RootCmd.
func Execute() error {
Expand All @@ -142,6 +180,21 @@ func Execute() error {
}
}

// TODO: This is a quick patch to mitigate the issue we can look for better code later
if os.Getenv("ATMOS_LOGS_LEVEL") != "" {
atmosConfig.Logs.Level = os.Getenv("ATMOS_LOGS_LEVEL")
}
flagKeyValue, _ := parseFlags(os.Args)
if v, ok := flagKeyValue["logs-level"]; ok {
atmosConfig.Logs.Level = v
}
if os.Getenv("ATMOS_LOGS_FILE") != "" {
atmosConfig.Logs.File = os.Getenv("ATMOS_LOGS_FILE")
}
if v, ok := flagKeyValue["logs-file"]; ok {
atmosConfig.Logs.File = v
}

// Set the log level for the charmbracelet/log package based on the atmosConfig
setupLogger(&atmosConfig)

Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/scenarios/priority-log-check/atmos.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
logs:
level: Info
file: /dev/stderr

stacks:
base_path: stacks
included_paths:
- "**/*"

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 76 additions & 3 deletions tests/test-cases/log-level-validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ tests:
- '👽 Atmos (\d+\.\d+\.\d+|test) on [a-z]+/[a-z0-9]+'
stdout:
- '^\n👽 Atmos (\d+\.\d+\.\d+|test) on [a-z]+/[a-z0-9_]+\n\n$'
stderr:
# This is actually an invalid debug statement, we are not passing the log level in the command line
- "DEBU Using command line argument"
exit_code: 0

- name: "Valid Log Level in Environment Variable"
Expand Down Expand Up @@ -92,3 +89,79 @@ tests:
stderr:
- "^$"
exit_code: 0
- name: "Valid log level in env should be priortized over config"
enabled: true
snapshot: true
description: "Test validation of env priority over config"
workdir: "../"
command: "atmos"
args:
- version
env:
ATMOS_LOGS_LEVEL: "Debug"
expect:
diff:
- '👽 Atmos (\d+\.\d+\.\d+|test) on [a-z]+/[a-z0-9]+'
stderr:
# We are fine with any debug logs config is set to info but if env gets priority we would get debug
- "DEBU"
exit_code: 0
- name: "Valid log level in flag should be priortized over env and config"
enabled: true
snapshot: true
description: "Test validation of env priority over config"
workdir: "../"
command: "atmos"
args:
- version
- "--logs-level"
- "Debug"
env:
ATMOS_LOGS_LEVEL: "Info"
expect:
diff:
- '👽 Atmos (\d+\.\d+\.\d+|test) on [a-z]+/[a-z0-9]+'
stderr:
# We are fine with any debug logs config and env is set to info but if flag gets priority we would get debug logs
- "DEBU"
exit_code: 0
- name: "Valid log file in env should be priortized over config"
enabled: true
snapshot: true
description: "Test validation of env priority over config"
workdir: "../"
command: "atmos"
args:
- version
- "--logs-level"
- "Debug"
env:
ATMOS_LOGS_FILE: "/dev/stdout"
expect:
diff:
- '👽 Atmos (\d+\.\d+\.\d+|test) on [a-z]+/[a-z0-9]+'
stdout:
# Debug logs should be in stdout as we have choosen /dev/stdout in env
- "DEBU"
exit_code: 0
- name: "Valid log file in flag should be priortized over env and config"
enabled: true
snapshot: true
description: "Test validation of env priority over config"
workdir: "../"
command: "atmos"
args:
- version
- "--logs-level"
- "Debug"
- "--logs-file"
- "/dev/stdout"
env:
ATMOS_LOGS_FILE: "/dev/stderr"
expect:
diff:
- '👽 Atmos (\d+\.\d+\.\d+|test) on [a-z]+/[a-z0-9]+'
stdout:
# Debug logs should be in stdout as we have choosen /dev/stdout in flag
- "DEBU"
exit_code: 0
Loading
0