Copilot commented on code in PR #220:
URL: 
https://github.com/apache/cloudstack-cloudmonkey/pull/220#discussion_r3924130522


##########
cmk.go:
##########
@@ -48,11 +49,36 @@ func main() {
        profile := flag.String("p", "", "server profile")
        configFilePath := flag.String("c", "", "config file path")
        acsURL := flag.String("u", config.DefaultACSAPIEndpoint, "cloudStack's 
API endpoint URL")
-       apiKey := flag.String("k", "", "cloudStack user's API Key")
-       secretKey := flag.String("s", "", "cloudStack user's secret Key")
+       apiKey := flag.String("k", "", "cloudStack user's API key")
+       secretKey := flag.String("s", "", "cloudStack user's secret key")
        flag.Parse()
        args := flag.Args()
 
+       // Fall back to environment variables for flags not passed on the
+       // command line; CLI flags take precedence over environment variables.
+       passedFlags := make(map[string]bool)
+       flag.Visit(func(f *flag.Flag) {
+               passedFlags[f.Name] = true
+       })
+       fallbackToEnvVar := func(flagName string, flagValue *string, envVar 
string) {
+               if !passedFlags[flagName] {
+                       if value := os.Getenv(envVar); value != "" {
+                               *flagValue = value
+                       }
+               }
+       }
+       fallbackToEnvVar("c", configFilePath, config.ConfigFileEnvVar)
+       fallbackToEnvVar("p", profile, config.ProfileEnvVar)
+       fallbackToEnvVar("u", acsURL, config.URLEnvVar)
+       fallbackToEnvVar("k", apiKey, config.APIKeyEnvVar)
+       fallbackToEnvVar("s", secretKey, config.SecretKeyEnvVar)
+       fallbackToEnvVar("o", outputFormat, config.OutputEnvVar)
+       if !passedFlags["d"] {
+               if value, err := 
strconv.ParseBool(strings.TrimSpace(os.Getenv(config.DebugEnvVar))); err == nil 
{
+                       *debug = value
+               }
+       }
+
        cfg := config.NewConfig(configFilePath)
 

Review Comment:
   `-u`/`CMK_URL` won't override the config file when the explicit value equals 
`config.DefaultACSAPIEndpoint` because the update is currently gated by 
`*acsURL != config.DefaultACSAPIEndpoint`. This means an explicitly provided 
URL (flag or env) can be silently ignored whenever it matches the default 
endpoint, which breaks the documented precedence over the config file.



##########
cmd/command.go:
##########
@@ -65,13 +65,15 @@ CloudMonkey (cmk) 🐵 is a command line interface for Apache 
CloudStack.
 Allowed flags:
   -h        Show this help message or API doc when specified after an API
   -v        Print version
-  -o        API response output format: json, text, table, column, csv
-  -p        Server profile
-  -d        Enable debug mode
-  -c        Different config file path
-  -u       CloudStack's API endpoint URL
-  -s       CloudStack user's secret Key
-  -k       CloudStack user's API Key
+  -o        API response output format: json, text, table, column, csv (env: 
CMK_OUTPUT)

Review Comment:
   The help text for `-o` omits the `default` output format, but 
`config.GetOutputFormats()` (used for validation) includes it. This makes `cmk 
-h` misleading.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to