Skip to content

Commit

Permalink
fix: always bind flags to env vars when configuring a command
Browse files Browse the repository at this point in the history
Previously the `bindFlags` function that sets command flags based
on environment variables was only called the first time that the
`Config` function executes for a command.

In regular use, this isn't a problem because each time a user runs
a `metal` command, it's a separate execution.  However, this causes
problems in end-to-end tests because only the first test of each
subcommand runs the `bindFlags` function, so subsequent tests fail
because they do not look up the API token in environment variables.

This moves the `bindFlags` call so that it runs every time the `Config`
function runs, which enables tests to consistently set flags based
on environment variables.
  • Loading branch information
ctreatma committed Jul 20, 2023
1 parent 198710b commit 4e1f20c
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ func (c *Client) Config(cmd *cobra.Command) *viper.Viper {

v.SetEnvPrefix(envPrefix)
c.viper = v
bindFlags(cmd, v)
}

bindFlags(cmd, c.viper)

flagToken := cmd.Flag("token").Value.String()
envToken := cmd.Flag("auth-token").Value.String()
// TODO: are we ok with this being configured by file too? yes?
Expand Down

0 comments on commit 4e1f20c

Please sign in to comment.