diff --git a/cmd/platform/platform.go b/cmd/platform/platform.go index a291a13a..19e03d7e 100644 --- a/cmd/platform/platform.go +++ b/cmd/platform/platform.go @@ -10,7 +10,7 @@ func NewPlatformCommand() *cobra.Command { When multiple products are configured in the CLI, the platform command can be used to manage one or more products collectively. -The --active-profile command switch can be used to specify the profile of Ping products to be managed.`, +The --profile command switch can be used to specify the profile of Ping products to be managed.`, Short: "Administer and manage the Ping integrated platform.", Use: "platform", } diff --git a/cmd/root.go b/cmd/root.go index 3ddd9115..1806532b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -47,7 +47,7 @@ func NewRootCommand() *cobra.Command { ) cmd.PersistentFlags().AddFlag(options.RootConfigOption.Flag) - cmd.PersistentFlags().AddFlag(options.RootActiveProfileOption.Flag) + cmd.PersistentFlags().AddFlag(options.RootProfileOption.Flag) cmd.PersistentFlags().AddFlag(options.RootOutputFormatOption.Flag) cmd.PersistentFlags().AddFlag(options.RootColorOption.Flag) @@ -80,7 +80,15 @@ func initViperProfile() { //Configure the main viper instance initMainViper(cfgFile) - profileName, err := profiles.GetOptionValue(options.RootActiveProfileOption) + userDefinedProfile, err := profiles.GetOptionValue(options.RootProfileOption) + if err != nil { + output.Print(output.Opts{ + Message: "Failed to get user-defined profile", + Result: output.ENUM_RESULT_FAILURE, + FatalMessage: err.Error(), + }) + } + configFileActiveProfile, err := profiles.GetOptionValue(options.RootActiveProfileOption) if err != nil { output.Print(output.Opts{ Message: "Failed to get active profile", @@ -89,12 +97,16 @@ func initViperProfile() { }) } - l.Debug().Msgf("Using configuration profile: %s", profileName) + if userDefinedProfile != "" { + l.Debug().Msgf("Using configuration profile: %s", userDefinedProfile) + } else { + l.Debug().Msgf("Using configuration profile: %s", configFileActiveProfile) + } // Configure the profile viper instance - if err := profiles.GetMainConfig().ChangeActiveProfile(profileName); err != nil { + if err := profiles.GetMainConfig().ChangeActiveProfile(configFileActiveProfile); err != nil { output.Print(output.Opts{ - Message: "Failed to set profile viper", + Message: "Failed to set active profile viper", Result: output.ENUM_RESULT_FAILURE, FatalMessage: err.Error(), }) diff --git a/cmd/root_test.go b/cmd/root_test.go index 028fe607..d55e0c6c 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -110,15 +110,15 @@ func TestRootCmd_NoValueConfigFlag(t *testing.T) { testutils.CheckExpectedError(t, err, &expectedErrorPattern) } -// Test Root Command Executes when provided the --active-profile flag -func TestRootCmd_ActiveProfileFlag(t *testing.T) { - err := testutils_cobra.ExecutePingcli(t, "--active-profile", "default") +// Test Root Command Executes when provided the --profile flag +func TestRootCmd_ProfileFlag(t *testing.T) { + err := testutils_cobra.ExecutePingcli(t, "--profile", "default") testutils.CheckExpectedError(t, err, nil) } -// Test Root Command fails when provided no value for the --active-profile flag -func TestRootCmd_NoValueActiveProfileFlag(t *testing.T) { - expectedErrorPattern := `^flag needs an argument: --active-profile$` - err := testutils_cobra.ExecutePingcli(t, "--active-profile") +// Test Root Command fails when provided no value for the --profile flag +func TestRootCmd_NoValueProfileFlag(t *testing.T) { + expectedErrorPattern := `^flag needs an argument: --profile$` + err := testutils_cobra.ExecutePingcli(t, "--profile") testutils.CheckExpectedError(t, err, &expectedErrorPattern) } diff --git a/docs/tool-configuration/configuration-key.md b/docs/tool-configuration/configuration-key.md index 9431a594..eb133f29 100644 --- a/docs/tool-configuration/configuration-key.md +++ b/docs/tool-configuration/configuration-key.md @@ -7,7 +7,7 @@ The following parameters can be configured in Ping CLI's static configuration fi | Config File Property | Type | Equivalent Parameter | Purpose | |---|---|---|---| -| activeProfile | ENUM_STRING | --active-profile / -P | The name of the stored custom configuration profile to use. | +| activeProfile | ENUM_STRING | | The name of the stored custom configuration profile to use by default. | | color | ENUM_BOOL | --color | Show text output in color. | | outputFormat | ENUM_OUTPUT_FORMAT | --output-format / -O | Specify the console output format.

Options are: json, text.

Example: `json` | diff --git a/internal/configuration/config/get.go b/internal/configuration/config/get.go index 5e7b433d..ffdfaa27 100644 --- a/internal/configuration/config/get.go +++ b/internal/configuration/config/get.go @@ -11,7 +11,7 @@ func InitConfigGetOptions() { } func initGetProfileOption() { - cobraParamName := "profile" + cobraParamName := "profile-name" cobraValue := new(customtypes.String) defaultValue := customtypes.String("") diff --git a/internal/configuration/config/set.go b/internal/configuration/config/set.go index 952d5fd9..90dd99bf 100644 --- a/internal/configuration/config/set.go +++ b/internal/configuration/config/set.go @@ -11,7 +11,7 @@ func InitConfigSetOptions() { } func initSetProfileOption() { - cobraParamName := "profile" + cobraParamName := "profile-name" cobraValue := new(customtypes.String) defaultValue := customtypes.String("") diff --git a/internal/configuration/config/unset.go b/internal/configuration/config/unset.go index 50becd04..d8151c4c 100644 --- a/internal/configuration/config/unset.go +++ b/internal/configuration/config/unset.go @@ -11,7 +11,7 @@ func InitConfigUnsetOptions() { } func initUnsetProfileOption() { - cobraParamName := "profile" + cobraParamName := "profile-name" cobraValue := new(customtypes.String) defaultValue := customtypes.String("") diff --git a/internal/configuration/options/options.go b/internal/configuration/options/options.go index 6c5cabf0..8addec9a 100644 --- a/internal/configuration/options/options.go +++ b/internal/configuration/options/options.go @@ -60,6 +60,7 @@ func Options() []Option { PingFederateAuthenticationTypeOption, RootActiveProfileOption, + RootProfileOption, RootColorOption, RootConfigOption, RootOutputFormatOption, @@ -147,6 +148,7 @@ var ( // Root Command Options var ( RootActiveProfileOption Option + RootProfileOption Option RootColorOption Option RootConfigOption Option RootOutputFormatOption Option diff --git a/internal/configuration/request/request.go b/internal/configuration/request/request.go index e3a0f957..3d7d1e7b 100644 --- a/internal/configuration/request/request.go +++ b/internal/configuration/request/request.go @@ -89,10 +89,10 @@ func initAccessTokenOption() { defaultValue := customtypes.String("") options.RequestAccessTokenOption = options.Option{ - CobraParamName: "", // No cobra param name - CobraParamValue: nil, // No cobra param value - DefaultValue: &defaultValue, // No default value - EnvVar: "", // No environment variable + CobraParamName: "", // No cobra param name + CobraParamValue: nil, // No cobra param value + DefaultValue: &defaultValue, + EnvVar: "", // No environment variable Flag: nil, Type: options.ENUM_STRING, ViperKey: "request.accessToken", @@ -103,11 +103,11 @@ func initAccessTokenExpiryOption() { defaultValue := customtypes.Int(0) options.RequestAccessTokenExpiryOption = options.Option{ - CobraParamName: "", // No cobra param name - CobraParamValue: nil, // No cobra param value - DefaultValue: &defaultValue, // No default value - EnvVar: "", // No environment variable - Flag: nil, // No flag + CobraParamName: "", // No cobra param name + CobraParamValue: nil, // No cobra param value + DefaultValue: &defaultValue, + EnvVar: "", // No environment variable + Flag: nil, // No flag Type: options.ENUM_INT, ViperKey: "request.accessTokenExpiry", } diff --git a/internal/configuration/root/root.go b/internal/configuration/root/root.go index e8c60969..c731e0de 100644 --- a/internal/configuration/root/root.go +++ b/internal/configuration/root/root.go @@ -13,30 +13,45 @@ import ( func InitRootOptions() { initActiveProfileOption() + initProfileOption() initColorOption() initConfigOption() initOutputFormatOption() } func initActiveProfileOption() { - cobraParamName := "active-profile" - cobraValue := new(customtypes.String) - defaultValue := customtypes.String("default") + defaultValue := customtypes.String("") options.RootActiveProfileOption = options.Option{ + CobraParamName: "", // No cobra param + CobraParamValue: nil, + DefaultValue: &defaultValue, + EnvVar: "", // No env var + Flag: nil, // No flag + Type: options.ENUM_STRING, + ViperKey: "activeProfile", + } +} + +func initProfileOption() { + cobraParamName := "profile" + cobraValue := new(customtypes.String) + defaultValue := customtypes.String("") + + options.RootProfileOption = options.Option{ CobraParamName: cobraParamName, CobraParamValue: cobraValue, DefaultValue: &defaultValue, - EnvVar: "PINGCLI_ACTIVE_PROFILE", + EnvVar: "PINGCLI_PROFILE", Flag: &pflag.Flag{ Name: cobraParamName, Shorthand: "P", - Usage: "The name of the stored custom configuration profile to use.", + Usage: "The name of a configuration profile to use.", Value: cobraValue, - DefValue: "default", + DefValue: "", }, Type: options.ENUM_STRING, - ViperKey: "activeProfile", + ViperKey: "", // No viper key } } diff --git a/internal/profiles/viper.go b/internal/profiles/viper.go index 34aa8846..e07bab42 100644 --- a/internal/profiles/viper.go +++ b/internal/profiles/viper.go @@ -327,12 +327,29 @@ func GetOptionValue(opt options.Option) (pFlagValue string, err error) { if opt.ViperKey != "" && mainConfig != nil { var vValue any - if opt.ViperKey == options.RootActiveProfileOption.ViperKey { - mainViperInstance := mainConfig.ViperInstance() - if mainViperInstance != nil { - vValue = mainViperInstance.Get(opt.ViperKey) + mainViperInstance := mainConfig.ViperInstance() + // This recursive call is safe, as options.RootProfileOption.ViperKey is not set + definedProfileName, err := GetOptionValue(options.RootProfileOption) + if err != nil { + return "", err + } + + // 3 Cases: + // - 1) Viper Key is the ActiveProfile Key, get value from main viper instance + // - 2) --profile flag has been set, get value from set profile viper instance + // - 3) no --profile flag set, get value from active profile viper instance defined in main viper instance + + if opt.ViperKey == options.RootActiveProfileOption.ViperKey && mainViperInstance != nil { + // Case 1 + vValue = mainViperInstance.Get(opt.ViperKey) + } else if definedProfileName != "" { + // Case 2 + profileViperInstance := mainViperInstance.Sub(definedProfileName) + if profileViperInstance != nil { + vValue = profileViperInstance.Get(opt.ViperKey) } } else { + // Case 3 activeProfile := mainConfig.ActiveProfile() if activeProfile != nil { profileViperInstance := activeProfile.ViperInstance()