Skip to content

Environment Variables Can't Be Used with Command-Line Arguments #198

@mcder0015

Description

@mcder0015

Because command-line arguments have a default and are set after the environment variables are read, environment variables cannot be used with command-line arguments. They will always be overridden by the default or passed command-line arguments.

log.Println("No config specified, using environment and args")
// no config, use environment and args
if err := cleanenv.ReadEnv(&cfg); err != nil {
log.Println("Error reading config from environment", err)
}
cfg.Target.URL = args.URL
cfg.Target.LoginMethod = args.LoginMethod

I have confirmed this behavior in production as well. Environment variables do nothing when a config file isn't used. I created a test branch where I removed the command-line setters like lines 189-190 above, and this allowed environment variables to be used again (but this disables command-line arguments entirely).

One possible way to fix this is using the flag library's visit method to get a list of only the command-line arguments that have been set and only updating those.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions