Fix viper flag binding for persistent flags and wrap bare errors#61
Merged
Merged
Conversation
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.
andresbott
approved these changes
Feb 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix broken flag-over-env precedence for persistent flags
The root command registers
--url,--proxyUrl,--proxyIgnoreTLS, and--configFileas persistent flags (cmd.PersistentFlags()). IninitParams(), onlycmd.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:
Example of the bug: Running
imscli profile --url https://custom.endpoint.comwhileIMS_URL=https://env.endpoint.comis 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 byv.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.