From 6c1e026dc36183d1abf6e47554c8b6144e1e1006 Mon Sep 17 00:00:00 2001 From: Jose Antonio Insua Date: Wed, 25 Feb 2026 16:34:56 +0100 Subject: [PATCH] Fix viper flag binding for persistent flags and wrap bare errors 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. --- cmd/params.go | 6 +++++- ims/admin_organizations.go | 2 +- ims/admin_profile.go | 2 +- ims/organizations.go | 2 +- ims/profile.go | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/cmd/params.go b/cmd/params.go index e972f45..9041a09 100644 --- a/cmd/params.go +++ b/cmd/params.go @@ -35,11 +35,15 @@ func initParams(cmd *cobra.Command, params *ims.Config, configFile string) error v.SetEnvPrefix("ims") v.AutomaticEnv() - // Command flags + // Command flags (local + inherited persistent flags) err := v.BindPFlags(cmd.Flags()) if err != nil { return fmt.Errorf("unable to process command flags: %w", err) } + err = v.BindPFlags(cmd.InheritedFlags()) + if err != nil { + return fmt.Errorf("unable to process inherited flags: %w", err) + } if configFile == "" { // Configuration file ( ~/.config/imscli.ext ) diff --git a/ims/admin_organizations.go b/ims/admin_organizations.go index 8b2590a..5d66cbf 100644 --- a/ims/admin_organizations.go +++ b/ims/admin_organizations.go @@ -72,7 +72,7 @@ func (i Config) GetAdminOrganizations() (string, error) { AuthSrc: i.AuthSrc, }) if err != nil { - return "", err + return "", fmt.Errorf("error getting admin organizations: %w", err) } return string(organizations.Body), nil diff --git a/ims/admin_profile.go b/ims/admin_profile.go index 8c91f1a..5b34cfa 100644 --- a/ims/admin_profile.go +++ b/ims/admin_profile.go @@ -71,7 +71,7 @@ func (i Config) GetAdminProfile() (string, error) { AuthSrc: i.AuthSrc, }) if err != nil { - return "", err + return "", fmt.Errorf("error getting admin profile: %w", err) } return string(profile.Body), nil diff --git a/ims/organizations.go b/ims/organizations.go index ddacc2a..b0da56e 100644 --- a/ims/organizations.go +++ b/ims/organizations.go @@ -62,7 +62,7 @@ func (i Config) GetOrganizations() (string, error) { ApiVersion: i.OrgsApiVersion, }) if err != nil { - return "", err + return "", fmt.Errorf("error getting organizations: %w", err) } return string(organizations.Body), nil diff --git a/ims/profile.go b/ims/profile.go index 046bd2e..6358856 100644 --- a/ims/profile.go +++ b/ims/profile.go @@ -67,7 +67,7 @@ func (i Config) GetProfile() (string, error) { ApiVersion: i.ProfileApiVersion, }) if err != nil { - return "", err + return "", fmt.Errorf("error getting profile: %w", err) } if !i.DecodeFulfillableData {