Skip to content

Commit

Permalink
Remove ActiveProfile struct, and add ability to get a profile's viper…
Browse files Browse the repository at this point in the history
… from the MainConfig struct. Refeactor to fix failing tests.
  • Loading branch information
erikostien-pingidentity committed Nov 13, 2024
1 parent 9919220 commit 5a4c90f
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 96 deletions.
5 changes: 4 additions & 1 deletion cmd/config/list_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func configListProfilesRunE(cmd *cobra.Command, args []string) error {
l := logger.Get()
l.Debug().Msgf("Config list-profiles Subcommand Called.")

config_internal.RunInternalConfigListProfiles()
err := config_internal.RunInternalConfigListProfiles()
if err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion cmd/config/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestConfigSetCmd_CheckViperConfig(t *testing.T) {
testutils.CheckExpectedError(t, err, nil)

mainViper := profiles.GetMainConfig().ViperInstance()
profileViperKey := profiles.GetMainConfig().ActiveProfile().Name() + "." + viperKey
profileViperKey := "default." + viperKey

viperNewValue := mainViper.GetString(profileViperKey)
if viperNewValue != viperNewUUID {
Expand Down
2 changes: 1 addition & 1 deletion cmd/config/unset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestConfigUnsetCmd_CheckViperConfig(t *testing.T) {
testutils.CheckExpectedError(t, err, nil)

mainViper := profiles.GetMainConfig().ViperInstance()
profileViperKey := profiles.GetMainConfig().ActiveProfile().Name() + "." + viperKey
profileViperKey := "default." + viperKey
viperNewValue := mainViper.GetString(profileViperKey)
if viperOldValue == viperNewValue {
t.Errorf("Expected viper configuration value to be updated. Old: %s, New: %s", viperOldValue, viperNewValue)
Expand Down
14 changes: 11 additions & 3 deletions internal/commands/config/list_profiles_internal.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package config_internal

import (
"strings"

"github.com/fatih/color"
"github.com/pingidentity/pingcli/internal/configuration/options"
"github.com/pingidentity/pingcli/internal/logger"
"github.com/pingidentity/pingcli/internal/output"
"github.com/pingidentity/pingcli/internal/profiles"
)

func RunInternalConfigListProfiles() {
func RunInternalConfigListProfiles() (err error) {
l := logger.Get()

profileNames := profiles.GetMainConfig().ProfileNames()
activeProfile := profiles.GetMainConfig().ActiveProfile().Name()
activeProfileName, err := profiles.GetOptionValue(options.RootActiveProfileOption)
if err != nil {
return err
}

listStr := "Profiles:\n"

Expand All @@ -20,7 +26,7 @@ func RunInternalConfigListProfiles() {
activeFmt := color.New(color.Bold, color.FgGreen).SprintFunc()

for _, profileName := range profileNames {
if profileName == activeProfile {
if strings.EqualFold(profileName, activeProfileName) {
listStr += "- " + profileName + activeFmt(" (active)") + " \n"
} else {
listStr += "- " + profileName + "\n"
Expand All @@ -38,4 +44,6 @@ func RunInternalConfigListProfiles() {
}

output.Message(listStr, nil)

return nil
}
4 changes: 3 additions & 1 deletion internal/commands/config/list_profiles_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package config_internal
import (
"testing"

"github.com/pingidentity/pingcli/internal/testing/testutils"
"github.com/pingidentity/pingcli/internal/testing/testutils_viper"
)

// Test RunInternalConfigListProfiles function
func Test_RunInternalConfigListProfiles(t *testing.T) {
testutils_viper.InitVipers(t)

RunInternalConfigListProfiles()
err := RunInternalConfigListProfiles()
testutils.CheckExpectedError(t, err, nil)
}
5 changes: 2 additions & 3 deletions internal/commands/config/set_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ func RunInternalConfigSet(kvPair string) (err error) {
return fmt.Errorf("failed to set configuration: value for key '%s' is empty. Use 'pingcli config unset %s' to unset the key", vKey, vKey)
}

if err = profiles.GetMainConfig().ValidateExistingProfileName(pName); err != nil {
subViper, err := profiles.GetMainConfig().GetProfileViper(pName)
if err != nil {
return fmt.Errorf("failed to set configuration: %v", err)
}

subViper := profiles.GetMainConfig().ViperInstance().Sub(pName)

opt, err := configuration.OptionFromViperKey(vKey)
if err != nil {
return fmt.Errorf("failed to set configuration: %v", err)
Expand Down
5 changes: 2 additions & 3 deletions internal/commands/config/unset_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ func RunInternalConfigUnset(viperKey string) (err error) {
return fmt.Errorf("failed to unset configuration: %v", err)
}

if err = profiles.GetMainConfig().ValidateExistingProfileName(pName); err != nil {
subViper, err := profiles.GetMainConfig().GetProfileViper(pName)
if err != nil {
return fmt.Errorf("failed to unset configuration: %v", err)
}

subViper := profiles.GetMainConfig().ViperInstance().Sub(pName)

opt, err := configuration.OptionFromViperKey(viperKey)
if err != nil {
return fmt.Errorf("failed to unset configuration: %v", err)
Expand Down
17 changes: 12 additions & 5 deletions internal/commands/request/request_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,20 @@ func pingoneAuth() (accessToken string, err error) {
}

if pName == "" {
pName = profiles.GetMainConfig().ActiveProfile().Name()
pName, err = profiles.GetOptionValue(options.RootActiveProfileOption)
if err != nil {
return "", err
}
}

subViper, err := profiles.GetMainConfig().GetProfileViper(pName)
if err != nil {
return "", err
}

profileViper := profiles.GetMainConfig().ViperInstance().Sub(pName)
profileViper.Set(options.RequestAccessTokenOption.ViperKey, pingoneAuthResponse.AccessToken)
profileViper.Set(options.RequestAccessTokenExpiryOption.ViperKey, tokenExpiry)
err = profiles.GetMainConfig().SaveProfile(pName, profileViper)
subViper.Set(options.RequestAccessTokenOption.ViperKey, pingoneAuthResponse.AccessToken)
subViper.Set(options.RequestAccessTokenExpiryOption.ViperKey, tokenExpiry)
err = profiles.GetMainConfig().SaveProfile(pName, subViper)
if err != nil {
return "", err
}
Expand Down
21 changes: 14 additions & 7 deletions internal/profiles/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,31 @@ import (
"github.com/spf13/viper"
)

func Validate() error {
func Validate() (err error) {
// Get a slice of all profile names configured in the config.yaml file
profileNames := GetMainConfig().ProfileNames()

// Validate profile names
if err := validateProfileNames(profileNames); err != nil {
if err = validateProfileNames(profileNames); err != nil {
return err
}

// Make sure selected active profile is in the configuration file
activeProfile := GetMainConfig().ActiveProfile().Name()
if !slices.Contains(profileNames, activeProfile) {
return fmt.Errorf("failed to validate Ping CLI configuration: active profile '%s' not found in configuration file %s", activeProfile, GetMainConfig().ViperInstance().ConfigFileUsed())
activeProfileName, err := GetOptionValue(options.RootActiveProfileOption)
if err != nil {
return fmt.Errorf("failed to validate Ping CLI configuration: %v", err)
}
if !slices.Contains(profileNames, activeProfileName) {
return fmt.Errorf("failed to validate Ping CLI configuration: active profile '%s' not found in configuration "+
"file %s", activeProfileName, GetMainConfig().ViperInstance().ConfigFileUsed())
}

// for each profile key, set the profile based on mainViper.Sub() and validate the profile
// for each profile key, validate the profile viper
for _, pName := range profileNames {
subViper := GetMainConfig().ViperInstance().Sub(pName)
subViper, err := GetMainConfig().GetProfileViper(pName)
if err != nil {
return fmt.Errorf("failed to validate Ping CLI configuration: %v", err)
}

if err := validateProfileKeys(pName, subViper); err != nil {
return fmt.Errorf("failed to validate Ping CLI configuration: %v", err)
Expand Down
Loading

0 comments on commit 5a4c90f

Please sign in to comment.