Skip to content

Fix viper flag binding for persistent flags and wrap bare errors#61

Merged
telegrapher merged 1 commit into
mainfrom
fix/bind-persistent-flags-and-wrap-errors
Feb 25, 2026
Merged

Fix viper flag binding for persistent flags and wrap bare errors#61
telegrapher merged 1 commit into
mainfrom
fix/bind-persistent-flags-and-wrap-errors

Conversation

@telegrapher

Copy link
Copy Markdown
Collaborator

Fix broken flag-over-env precedence for persistent flags

The root command registers --url, --proxyUrl, --proxyIgnoreTLS, and --configFile as persistent flags (cmd.PersistentFlags()). In initParams(), only cmd.Flags() was bound to viper. However, cmd.Flags() returns only the local flags of the leaf command being executed — it does not include persistent flags inherited from parent commands.

This meant viper never learned about these persistent flags, breaking the expected configuration priority:

CLI flag > env var > config file

Example of the bug: Running imscli profile --url https://custom.endpoint.com while IMS_URL=https://env.endpoint.com is set — the env var would win because viper saw the env var but not the CLI flag. The flag value (written to the struct by cobra) was then overwritten by v.Unmarshal() with the env var value.

The fix adds v.BindPFlags(cmd.InheritedFlags()) so viper sees both local and inherited persistent flags, restoring correct precedence.

Fix broken flag-over-env precedence for persistent flags (--url,
--proxyUrl, --proxyIgnoreTLS, --configFile). These flags were registered
on the root command's PersistentFlags() but only cmd.Flags() was bound
to viper. Since cmd.Flags() returns only local flags of the leaf
command, viper never learned about the persistent flags. This caused
env vars (e.g. IMS_URL) to silently override CLI flags, inverting the
expected priority of: CLI flag > env var > config file.

The fix adds v.BindPFlags(cmd.InheritedFlags()) so viper sees both
local and inherited persistent flags, restoring correct precedence.

Also wraps four bare error returns in ims/ methods (GetOrganizations,
GetAdminOrganizations, GetAdminProfile, GetProfile) with context
messages to match the wrapping pattern used by all other errors in
the same functions.
@telegrapher
telegrapher merged commit 3e20037 into main Feb 25, 2026
5 checks passed
@telegrapher
telegrapher deleted the fix/bind-persistent-flags-and-wrap-errors branch February 28, 2026 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants