Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand Down
22 changes: 17 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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",
Expand All @@ -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(),
})
Expand Down
14 changes: 7 additions & 7 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion docs/tool-configuration/configuration-key.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br><br>Options are: json, text.<br><br>Example: `json` |

Expand Down
2 changes: 1 addition & 1 deletion internal/configuration/config/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func InitConfigGetOptions() {
}

func initGetProfileOption() {
cobraParamName := "profile"
cobraParamName := "profile-name"
cobraValue := new(customtypes.String)
defaultValue := customtypes.String("")

Expand Down
2 changes: 1 addition & 1 deletion internal/configuration/config/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func InitConfigSetOptions() {
}

func initSetProfileOption() {
cobraParamName := "profile"
cobraParamName := "profile-name"
cobraValue := new(customtypes.String)
defaultValue := customtypes.String("")

Expand Down
2 changes: 1 addition & 1 deletion internal/configuration/config/unset.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func InitConfigUnsetOptions() {
}

func initUnsetProfileOption() {
cobraParamName := "profile"
cobraParamName := "profile-name"
cobraValue := new(customtypes.String)
defaultValue := customtypes.String("")

Expand Down
2 changes: 2 additions & 0 deletions internal/configuration/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func Options() []Option {
PingFederateAuthenticationTypeOption,

RootActiveProfileOption,
RootProfileOption,
RootColorOption,
RootConfigOption,
RootOutputFormatOption,
Expand Down Expand Up @@ -147,6 +148,7 @@ var (
// Root Command Options
var (
RootActiveProfileOption Option
RootProfileOption Option
RootColorOption Option
RootConfigOption Option
RootOutputFormatOption Option
Expand Down
18 changes: 9 additions & 9 deletions internal/configuration/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
}
Expand Down
29 changes: 22 additions & 7 deletions internal/configuration/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
25 changes: 21 additions & 4 deletions internal/profiles/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down