From 82b6eff22c0c7fac31e00081b73b42a5735f9506 Mon Sep 17 00:00:00 2001 From: Erik Ostien Date: Thu, 17 Oct 2024 12:31:34 +0200 Subject: [PATCH 1/4] Remove delete --profile flag, add --yes flag --- cmd/config/delete_profile.go | 13 ++++-- cmd/config/delete_profile_test.go | 46 +++++++++---------- .../config/delete_profile_internal.go | 42 +++++++++++------ .../configuration/config/delete_profile.go | 23 +++++----- internal/configuration/options/options.go | 4 +- 5 files changed, 70 insertions(+), 58 deletions(-) diff --git a/cmd/config/delete_profile.go b/cmd/config/delete_profile.go index 359197e8..30c7525f 100644 --- a/cmd/config/delete_profile.go +++ b/cmd/config/delete_profile.go @@ -15,12 +15,15 @@ const ( pingcli config delete-profile Delete a configuration profile by specifying the name of an existing configured profile. - pingcli config delete-profile --profile MyDeveloperEnv` + pingcli config delete-profile MyDeveloperEnv + + Delete a configuration profile by auto-accepting the deletion. + pingcli config delete-profile --yes MyDeveloperEnv` ) func NewConfigDeleteProfileCommand() *cobra.Command { cmd := &cobra.Command{ - Args: common.ExactArgs(0), + Args: common.RangeArgs(0, 1), DisableFlagsInUseLine: true, // We write our own flags in @Use attribute Example: deleteProfileCommandExamples, Long: `Delete an existing custom configuration profile from the CLI. @@ -28,10 +31,10 @@ func NewConfigDeleteProfileCommand() *cobra.Command { The profile to delete will be removed from the CLI configuration file.`, RunE: configDeleteProfileRunE, Short: "Delete a custom configuration profile.", - Use: "delete-profile [flags]", + Use: "delete-profile [flags] [profile-name]", } - cmd.Flags().AddFlag(options.ConfigDeleteProfileOption.Flag) + cmd.Flags().AddFlag(options.ConfigDeleteAutoAcceptOption.Flag) return cmd } @@ -40,7 +43,7 @@ func configDeleteProfileRunE(cmd *cobra.Command, args []string) error { l := logger.Get() l.Debug().Msgf("Config delete-profile Subcommand Called.") - if err := config_internal.RunInternalConfigDeleteProfile(os.Stdin); err != nil { + if err := config_internal.RunInternalConfigDeleteProfile(args, os.Stdin); err != nil { return err } diff --git a/cmd/config/delete_profile_test.go b/cmd/config/delete_profile_test.go index 6796ccea..ef6aaa8c 100644 --- a/cmd/config/delete_profile_test.go +++ b/cmd/config/delete_profile_test.go @@ -7,17 +7,16 @@ import ( "github.com/pingidentity/pingcli/internal/testing/testutils_cobra" ) -// TODO: Re-enable this test when a auto-accept delete-profile flag is added // Test Config delete-profile Command Executes without issue -// func TestConfigDeleteProfileCmd_Execute(t *testing.T) { -// err := testutils_cobra.ExecutePingcli(t, "config", "delete-profile", "--profile", "production") -// testutils.CheckExpectedError(t, err, nil) -// } +func TestConfigDeleteProfileCmd_Execute(t *testing.T) { + err := testutils_cobra.ExecutePingcli(t, "config", "delete-profile", "--yes", "production") + testutils.CheckExpectedError(t, err, nil) +} // Test Config delete-profile Command fails when provided too many arguments func TestConfigDeleteProfileCmd_TooManyArgs(t *testing.T) { - expectedErrorPattern := `^failed to execute 'pingcli config delete-profile': command accepts 0 arg\(s\), received 1$` - err := testutils_cobra.ExecutePingcli(t, "config", "delete-profile", "extra-arg") + expectedErrorPattern := `^failed to execute '.*': command accepts 0 to 1 arg\(s\), received 2$` + err := testutils_cobra.ExecutePingcli(t, "config", "delete-profile", "extra-arg", "extra-arg2") testutils.CheckExpectedError(t, err, &expectedErrorPattern) } @@ -28,26 +27,23 @@ func TestConfigDeleteProfileCmd_InvalidFlag(t *testing.T) { testutils.CheckExpectedError(t, err, &expectedErrorPattern) } -// TODO: Re-enable this test when a auto-accept delete-profile flag is added // Test Config delete-profile Command fails when provided an non-existent profile name -// func TestConfigDeleteProfileCmd_NonExistentProfileName(t *testing.T) { -// expectedErrorPattern := `^failed to delete profile: invalid profile name: '.*' profile does not exist$` -// err := testutils_cobra.ExecutePingcli(t, "config", "delete-profile", "--profile", "nonexistent") -// testutils.CheckExpectedError(t, err, &expectedErrorPattern) -// } +func TestConfigDeleteProfileCmd_NonExistentProfileName(t *testing.T) { + expectedErrorPattern := `^failed to delete profile: invalid profile name: '.*' profile does not exist$` + err := testutils_cobra.ExecutePingcli(t, "config", "delete-profile", "--yes", "nonexistent") + testutils.CheckExpectedError(t, err, &expectedErrorPattern) +} -// TODO: Re-enable this test when a auto-accept delete-profile flag is added // Test Config delete-profile Command fails when provided the active profile -// func TestConfigDeleteProfileCmd_ActiveProfile(t *testing.T) { -// expectedErrorPattern := `^failed to delete profile: '.*' is the active profile and cannot be deleted$` -// err := testutils_cobra.ExecutePingcli(t, "config", "delete-profile", "--profile", "default") -// testutils.CheckExpectedError(t, err, &expectedErrorPattern) -// } +func TestConfigDeleteProfileCmd_ActiveProfile(t *testing.T) { + expectedErrorPattern := `^failed to delete profile: '.*' is the active profile and cannot be deleted$` + err := testutils_cobra.ExecutePingcli(t, "config", "delete-profile", "--yes", "default") + testutils.CheckExpectedError(t, err, &expectedErrorPattern) +} -// TODO: Re-enable this test when a auto-accept delete-profile flag is added // Test Config delete-profile Command fails when provided an invalid profile name -// func TestConfigDeleteProfileCmd_InvalidProfileName(t *testing.T) { -// expectedErrorPattern := `^failed to delete profile: invalid profile name: '.*'\. name must contain only alphanumeric characters, underscores, and dashes$` -// err := testutils_cobra.ExecutePingcli(t, "config", "delete-profile", "--profile", "pname&*^*&^$&@!") -// testutils.CheckExpectedError(t, err, &expectedErrorPattern) -// } +func TestConfigDeleteProfileCmd_InvalidProfileName(t *testing.T) { + expectedErrorPattern := `^failed to delete profile: invalid profile name: '.*'\. name must contain only alphanumeric characters, underscores, and dashes$` + err := testutils_cobra.ExecutePingcli(t, "config", "delete-profile", "--yes", "pname&*^*&^$&@!") + testutils.CheckExpectedError(t, err, &expectedErrorPattern) +} diff --git a/internal/commands/config/delete_profile_internal.go b/internal/commands/config/delete_profile_internal.go index c4be2a03..0364c790 100644 --- a/internal/commands/config/delete_profile_internal.go +++ b/internal/commands/config/delete_profile_internal.go @@ -10,14 +10,18 @@ import ( "github.com/pingidentity/pingcli/internal/profiles" ) -func RunInternalConfigDeleteProfile(rc io.ReadCloser) (err error) { - pName, err := readConfigDeleteProfileOptions(rc) - if err != nil { - return fmt.Errorf("failed to delete profile: %v", err) +func RunInternalConfigDeleteProfile(args []string, rc io.ReadCloser) (err error) { + var pName string + if len(args) == 1 { + pName = args[0] + } else { + pName, err = promptUserToSelectProfile(rc) + if err != nil { + return fmt.Errorf("failed to delete profile: %v", err) + } } - // TODO: Add auto-accept flag in future release to avoid user confirmation prompt - confirmed, err := input.RunPromptConfirm(fmt.Sprintf("Are you sure you want to delete profile '%s'?", pName), rc) + confirmed, err := promptUserToConfirm(pName, rc) if err != nil { return fmt.Errorf("failed to delete profile: %v", err) } @@ -39,22 +43,30 @@ func RunInternalConfigDeleteProfile(rc io.ReadCloser) (err error) { return nil } -func readConfigDeleteProfileOptions(rc io.ReadCloser) (pName string, err error) { - if !options.ConfigDeleteProfileOption.Flag.Changed { - pName, err = input.RunPromptSelect("Select profile to delete: ", profiles.GetMainConfig().ProfileNames(), rc) - } else { - pName, err = profiles.GetOptionValue(options.ConfigDeleteProfileOption) - } +func promptUserToSelectProfile(rc io.ReadCloser) (pName string, err error) { + pName, err = input.RunPromptSelect("Select profile to delete: ", profiles.GetMainConfig().ProfileNames(), rc) if err != nil { return pName, err } - if pName == "" { - return pName, fmt.Errorf("unable to determine profile name to delete") + return pName, nil +} + +func promptUserToConfirm(pName string, rc io.ReadCloser) (confirmed bool, err error) { + autoAccept := "false" + if options.ConfigDeleteAutoAcceptOption.Flag.Changed { + autoAccept, err = profiles.GetOptionValue(options.ConfigDeleteAutoAcceptOption) + if err != nil { + return false, err + } } - return pName, nil + if autoAccept == "true" { + return true, nil + } + + return input.RunPromptConfirm(fmt.Sprintf("Are you sure you want to delete profile '%s'?", pName), rc) } func deleteProfile(pName string) (err error) { diff --git a/internal/configuration/config/delete_profile.go b/internal/configuration/config/delete_profile.go index 24fb66f2..6cbcf2bb 100644 --- a/internal/configuration/config/delete_profile.go +++ b/internal/configuration/config/delete_profile.go @@ -7,25 +7,26 @@ import ( ) func InitConfigDeleteProfileOptions() { - initDeleteProfileOption() + initDeleteAutoAcceptOption() } -func initDeleteProfileOption() { - cobraParamName := "profile" - cobraValue := new(customtypes.String) - defaultValue := customtypes.String("") +func initDeleteAutoAcceptOption() { + cobraParamName := "yes" + cobraValue := new(customtypes.Bool) + defaultValue := customtypes.Bool(false) - options.ConfigDeleteProfileOption = options.Option{ + options.ConfigDeleteAutoAcceptOption = options.Option{ CobraParamName: cobraParamName, CobraParamValue: cobraValue, DefaultValue: &defaultValue, EnvVar: "", // No environment variable Flag: &pflag.Flag{ - Name: cobraParamName, - Shorthand: "p", - Usage: "The name of the configuration profile to delete.", - Value: cobraValue, - DefValue: "", + Name: cobraParamName, + Shorthand: "y", + Usage: "Auto-accept the profile deletion confirmation prompt.", + Value: cobraValue, + DefValue: "false", + NoOptDefVal: "true", // Make the flag a boolean flag }, Type: options.ENUM_STRING, ViperKey: "", // No viper key diff --git a/internal/configuration/options/options.go b/internal/configuration/options/options.go index 42e9de69..b30fd03c 100644 --- a/internal/configuration/options/options.go +++ b/internal/configuration/options/options.go @@ -72,7 +72,7 @@ func Options() []Option { ConfigAddProfileDescriptionOption, ConfigAddProfileNameOption, ConfigAddProfileSetActiveOption, - ConfigDeleteProfileOption, + ConfigDeleteAutoAcceptOption, ConfigViewProfileOption, ConfigSetActiveProfileOption, ConfigGetProfileOption, @@ -123,7 +123,7 @@ var ( ConfigAddProfileNameOption Option ConfigAddProfileSetActiveOption Option - ConfigDeleteProfileOption Option + ConfigDeleteAutoAcceptOption Option ConfigViewProfileOption Option From 357e92ee2fbc4ad04fd6e065d5cf487aa8b7b996 Mon Sep 17 00:00:00 2001 From: Erik Ostien Date: Thu, 17 Oct 2024 12:42:23 +0200 Subject: [PATCH 2/4] Remove set-active-profile --profile flag in favor of argument --- cmd/config/set_active_profile.go | 11 +++---- cmd/config/set_active_profile_test.go | 12 +++---- .../config/delete_profile_internal.go | 8 ++--- .../config/set_active_profile_internal.go | 26 +++++++-------- .../set_active_profile_internal_test.go | 30 +++-------------- .../config/set_active_profile.go | 33 ------------------- internal/configuration/configuration.go | 1 - internal/configuration/options/options.go | 3 -- 8 files changed, 29 insertions(+), 95 deletions(-) delete mode 100644 internal/configuration/config/set_active_profile.go diff --git a/cmd/config/set_active_profile.go b/cmd/config/set_active_profile.go index 6be8fa77..3eb1a9cd 100644 --- a/cmd/config/set_active_profile.go +++ b/cmd/config/set_active_profile.go @@ -5,7 +5,6 @@ import ( "github.com/pingidentity/pingcli/cmd/common" config_internal "github.com/pingidentity/pingcli/internal/commands/config" - "github.com/pingidentity/pingcli/internal/configuration/options" "github.com/pingidentity/pingcli/internal/logger" "github.com/spf13/cobra" ) @@ -15,22 +14,20 @@ const ( pingcli config set-active-profile Set an active profile with a specific profile name. - pingcli config set-active-profile --profile myprofile` + pingcli config set-active-profile myprofile` ) func NewConfigSetActiveProfileCommand() *cobra.Command { cmd := &cobra.Command{ - Args: common.ExactArgs(0), + Args: common.ExactArgs(1), DisableFlagsInUseLine: true, // We write our own flags in @Use attribute Example: setActiveProfileCommandExamples, Long: `Set a custom configuration profile as the in-use profile.`, RunE: configSetActiveProfileRunE, Short: "Set a custom configuration profile as the in-use profile.", - Use: "set-active-profile [flags]", + Use: "set-active-profile [flags] [profile-name]", } - cmd.Flags().AddFlag(options.ConfigSetActiveProfileOption.Flag) - return cmd } @@ -38,7 +35,7 @@ func configSetActiveProfileRunE(cmd *cobra.Command, args []string) error { l := logger.Get() l.Debug().Msgf("Config set-active-profile Subcommand Called.") - if err := config_internal.RunInternalConfigSetActiveProfile(os.Stdin); err != nil { + if err := config_internal.RunInternalConfigSetActiveProfile(args, os.Stdin); err != nil { return err } diff --git a/cmd/config/set_active_profile_test.go b/cmd/config/set_active_profile_test.go index 209c9fae..babd42bd 100644 --- a/cmd/config/set_active_profile_test.go +++ b/cmd/config/set_active_profile_test.go @@ -9,14 +9,14 @@ import ( // Test Config set-active-profile Command Executes without issue func TestConfigSetActiveProfileCmd_Execute(t *testing.T) { - err := testutils_cobra.ExecutePingcli(t, "config", "set-active-profile", "--profile", "production") + err := testutils_cobra.ExecutePingcli(t, "config", "set-active-profile", "production") testutils.CheckExpectedError(t, err, nil) } // Test Config set-active-profile Command fails when provided too many arguments func TestConfigSetActiveProfileCmd_TooManyArgs(t *testing.T) { - expectedErrorPattern := `^failed to execute 'pingcli config set-active-profile': command accepts 0 arg\(s\), received 1$` - err := testutils_cobra.ExecutePingcli(t, "config", "set-active-profile", "extra-arg") + expectedErrorPattern := `^failed to execute '.*': command accepts 1 arg\(s\), received 2$` + err := testutils_cobra.ExecutePingcli(t, "config", "set-active-profile", "extra-arg", "extra-arg2") testutils.CheckExpectedError(t, err, &expectedErrorPattern) } @@ -30,20 +30,20 @@ func TestConfigSetActiveProfileCmd_InvalidFlag(t *testing.T) { // Test Config set-active-profile Command fails when provided an non-existent profile name func TestConfigSetActiveProfileCmd_NonExistentProfileName(t *testing.T) { expectedErrorPattern := `^failed to set active profile: invalid profile name: '.*' profile does not exist$` - err := testutils_cobra.ExecutePingcli(t, "config", "set-active-profile", "--profile", "nonexistent") + err := testutils_cobra.ExecutePingcli(t, "config", "set-active-profile", "nonexistent") testutils.CheckExpectedError(t, err, &expectedErrorPattern) } // Test Config set-active-profile Command succeeds when provided the active profile func TestConfigSetActiveProfileCmd_ActiveProfile(t *testing.T) { - err := testutils_cobra.ExecutePingcli(t, "config", "set-active-profile", "--profile", "default") + err := testutils_cobra.ExecutePingcli(t, "config", "set-active-profile", "default") testutils.CheckExpectedError(t, err, nil) } // Test Config set-active-profile Command fails when provided an invalid profile name func TestConfigSetActiveProfileCmd_InvalidProfileName(t *testing.T) { expectedErrorPattern := `^failed to set active profile: invalid profile name: '.*'\. name must contain only alphanumeric characters, underscores, and dashes$` - err := testutils_cobra.ExecutePingcli(t, "config", "set-active-profile", "--profile", "pname&*^*&^$&@!") + err := testutils_cobra.ExecutePingcli(t, "config", "set-active-profile", "pname&*^*&^$&@!") testutils.CheckExpectedError(t, err, &expectedErrorPattern) } diff --git a/internal/commands/config/delete_profile_internal.go b/internal/commands/config/delete_profile_internal.go index 0364c790..ee6073e8 100644 --- a/internal/commands/config/delete_profile_internal.go +++ b/internal/commands/config/delete_profile_internal.go @@ -15,13 +15,13 @@ func RunInternalConfigDeleteProfile(args []string, rc io.ReadCloser) (err error) if len(args) == 1 { pName = args[0] } else { - pName, err = promptUserToSelectProfile(rc) + pName, err = promptUserToDeleteProfile(rc) if err != nil { return fmt.Errorf("failed to delete profile: %v", err) } } - confirmed, err := promptUserToConfirm(pName, rc) + confirmed, err := promptUserToConfirmDelete(pName, rc) if err != nil { return fmt.Errorf("failed to delete profile: %v", err) } @@ -43,7 +43,7 @@ func RunInternalConfigDeleteProfile(args []string, rc io.ReadCloser) (err error) return nil } -func promptUserToSelectProfile(rc io.ReadCloser) (pName string, err error) { +func promptUserToDeleteProfile(rc io.ReadCloser) (pName string, err error) { pName, err = input.RunPromptSelect("Select profile to delete: ", profiles.GetMainConfig().ProfileNames(), rc) if err != nil { @@ -53,7 +53,7 @@ func promptUserToSelectProfile(rc io.ReadCloser) (pName string, err error) { return pName, nil } -func promptUserToConfirm(pName string, rc io.ReadCloser) (confirmed bool, err error) { +func promptUserToConfirmDelete(pName string, rc io.ReadCloser) (confirmed bool, err error) { autoAccept := "false" if options.ConfigDeleteAutoAcceptOption.Flag.Changed { autoAccept, err = profiles.GetOptionValue(options.ConfigDeleteAutoAcceptOption) diff --git a/internal/commands/config/set_active_profile_internal.go b/internal/commands/config/set_active_profile_internal.go index 77d80114..45f4f33e 100644 --- a/internal/commands/config/set_active_profile_internal.go +++ b/internal/commands/config/set_active_profile_internal.go @@ -4,16 +4,20 @@ import ( "fmt" "io" - "github.com/pingidentity/pingcli/internal/configuration/options" "github.com/pingidentity/pingcli/internal/input" "github.com/pingidentity/pingcli/internal/output" "github.com/pingidentity/pingcli/internal/profiles" ) -func RunInternalConfigSetActiveProfile(rc io.ReadCloser) (err error) { - pName, err := readConfigSetActiveProfileOptions(rc) - if err != nil { - return fmt.Errorf("failed to set active profile: %v", err) +func RunInternalConfigSetActiveProfile(args []string, rc io.ReadCloser) (err error) { + var pName string + if len(args) == 1 { + pName = args[0] + } else { + pName, err = promptUserToSelectActiveProfile(rc) + if err != nil { + return fmt.Errorf("failed to set active profile: %v", err) + } } output.Print(output.Opts{ @@ -33,20 +37,12 @@ func RunInternalConfigSetActiveProfile(rc io.ReadCloser) (err error) { return nil } -func readConfigSetActiveProfileOptions(rc io.ReadCloser) (pName string, err error) { - if !options.ConfigSetActiveProfileOption.Flag.Changed { - pName, err = input.RunPromptSelect("Select profile to set as active: ", profiles.GetMainConfig().ProfileNames(), rc) - } else { - pName, err = profiles.GetOptionValue(options.ConfigSetActiveProfileOption) - } +func promptUserToSelectActiveProfile(rc io.ReadCloser) (pName string, err error) { + pName, err = input.RunPromptSelect("Select profile to set as active: ", profiles.GetMainConfig().ProfileNames(), rc) if err != nil { return pName, err } - if pName == "" { - return pName, fmt.Errorf("unable to determine profile name to set as active") - } - return pName, nil } diff --git a/internal/commands/config/set_active_profile_internal_test.go b/internal/commands/config/set_active_profile_internal_test.go index cfd867c6..e115cb72 100644 --- a/internal/commands/config/set_active_profile_internal_test.go +++ b/internal/commands/config/set_active_profile_internal_test.go @@ -4,8 +4,6 @@ import ( "os" "testing" - "github.com/pingidentity/pingcli/internal/configuration/options" - "github.com/pingidentity/pingcli/internal/customtypes" "github.com/pingidentity/pingcli/internal/testing/testutils" "github.com/pingidentity/pingcli/internal/testing/testutils_viper" ) @@ -14,30 +12,16 @@ import ( func Test_RunInternalConfigSetActiveProfile(t *testing.T) { testutils_viper.InitVipers(t) - var ( - profileName = customtypes.String("production") - ) - options.ConfigSetActiveProfileOption.Flag.Changed = true - options.ConfigSetActiveProfileOption.CobraParamValue = &profileName - - err := RunInternalConfigSetActiveProfile(os.Stdin) - if err != nil { - t.Errorf("RunInternalConfigSetActiveProfile returned error: %v", err) - } + err := RunInternalConfigSetActiveProfile([]string{"production"}, os.Stdin) + testutils.CheckExpectedError(t, err, nil) } // Test RunInternalConfigSetActiveProfile function fails with invalid profile name func Test_RunInternalConfigSetActiveProfile_InvalidProfileName(t *testing.T) { testutils_viper.InitVipers(t) - var ( - profileName = customtypes.String("(*#&)") - ) - options.ConfigSetActiveProfileOption.Flag.Changed = true - options.ConfigSetActiveProfileOption.CobraParamValue = &profileName - expectedErrorPattern := `^failed to set active profile: invalid profile name: '.*'\. name must contain only alphanumeric characters, underscores, and dashes$` - err := RunInternalConfigSetActiveProfile(os.Stdin) + err := RunInternalConfigSetActiveProfile([]string{"(*#&)"}, os.Stdin) testutils.CheckExpectedError(t, err, &expectedErrorPattern) } @@ -45,13 +29,7 @@ func Test_RunInternalConfigSetActiveProfile_InvalidProfileName(t *testing.T) { func Test_RunInternalConfigSetActiveProfile_NonExistentProfile(t *testing.T) { testutils_viper.InitVipers(t) - var ( - profileName = customtypes.String("non-existent") - ) - options.ConfigSetActiveProfileOption.Flag.Changed = true - options.ConfigSetActiveProfileOption.CobraParamValue = &profileName - expectedErrorPattern := `^failed to set active profile: invalid profile name: '.*' profile does not exist$` - err := RunInternalConfigSetActiveProfile(os.Stdin) + err := RunInternalConfigSetActiveProfile([]string{"non-existent"}, os.Stdin) testutils.CheckExpectedError(t, err, &expectedErrorPattern) } diff --git a/internal/configuration/config/set_active_profile.go b/internal/configuration/config/set_active_profile.go deleted file mode 100644 index 4d29081b..00000000 --- a/internal/configuration/config/set_active_profile.go +++ /dev/null @@ -1,33 +0,0 @@ -package configuration_config - -import ( - "github.com/pingidentity/pingcli/internal/configuration/options" - "github.com/pingidentity/pingcli/internal/customtypes" - "github.com/spf13/pflag" -) - -func InitConfigSetActiveProfileOptions() { - initSetActiveProfileOption() -} - -func initSetActiveProfileOption() { - cobraParamName := "profile" - cobraValue := new(customtypes.String) - defaultValue := customtypes.String("") - - options.ConfigSetActiveProfileOption = options.Option{ - CobraParamName: cobraParamName, - CobraParamValue: cobraValue, - DefaultValue: &defaultValue, - EnvVar: "", // No environment variable - Flag: &pflag.Flag{ - Name: cobraParamName, - Shorthand: "p", - Usage: "The name of the configuration profile to set as the active profile.", - Value: cobraValue, - DefValue: "", - }, - Type: options.ENUM_STRING, - ViperKey: "", // No viper key - } -} diff --git a/internal/configuration/configuration.go b/internal/configuration/configuration.go index 19ff6704..d8c10e3f 100644 --- a/internal/configuration/configuration.go +++ b/internal/configuration/configuration.go @@ -85,7 +85,6 @@ func InitAllOptions() { configuration_config.InitConfigAddProfileOptions() configuration_config.InitConfigDeleteProfileOptions() configuration_config.InitConfigViewProfileOptions() - configuration_config.InitConfigSetActiveProfileOptions() configuration_config.InitConfigSetOptions() configuration_config.InitConfigGetOptions() configuration_config.InitConfigUnsetOptions() diff --git a/internal/configuration/options/options.go b/internal/configuration/options/options.go index b30fd03c..6f3a8068 100644 --- a/internal/configuration/options/options.go +++ b/internal/configuration/options/options.go @@ -74,7 +74,6 @@ func Options() []Option { ConfigAddProfileSetActiveOption, ConfigDeleteAutoAcceptOption, ConfigViewProfileOption, - ConfigSetActiveProfileOption, ConfigGetProfileOption, ConfigSetProfileOption, ConfigUnsetProfileOption, @@ -127,8 +126,6 @@ var ( ConfigViewProfileOption Option - ConfigSetActiveProfileOption Option - ConfigGetProfileOption Option ConfigSetProfileOption Option From b6fcc31767b0911f88e1218bfa70835a008289ab Mon Sep 17 00:00:00 2001 From: Erik Ostien Date: Thu, 17 Oct 2024 12:49:03 +0200 Subject: [PATCH 3/4] Remove view-profile --profile flag in favor of argument --- cmd/config/set_active_profile.go | 2 +- cmd/config/view_profile.go | 11 +++---- cmd/config/view_profile_test.go | 8 ++--- .../commands/config/view_profile_internal.go | 31 +++++------------ .../config/view_profile_internal_test.go | 30 +++-------------- internal/configuration/config/view_profile.go | 33 ------------------- internal/configuration/configuration.go | 1 - internal/configuration/options/options.go | 3 -- 8 files changed, 23 insertions(+), 96 deletions(-) delete mode 100644 internal/configuration/config/view_profile.go diff --git a/cmd/config/set_active_profile.go b/cmd/config/set_active_profile.go index 3eb1a9cd..669625c0 100644 --- a/cmd/config/set_active_profile.go +++ b/cmd/config/set_active_profile.go @@ -19,7 +19,7 @@ const ( func NewConfigSetActiveProfileCommand() *cobra.Command { cmd := &cobra.Command{ - Args: common.ExactArgs(1), + Args: common.RangeArgs(0, 1), DisableFlagsInUseLine: true, // We write our own flags in @Use attribute Example: setActiveProfileCommandExamples, Long: `Set a custom configuration profile as the in-use profile.`, diff --git a/cmd/config/view_profile.go b/cmd/config/view_profile.go index a94d06a2..86b84bea 100644 --- a/cmd/config/view_profile.go +++ b/cmd/config/view_profile.go @@ -3,7 +3,6 @@ package config import ( "github.com/pingidentity/pingcli/cmd/common" config_internal "github.com/pingidentity/pingcli/internal/commands/config" - "github.com/pingidentity/pingcli/internal/configuration/options" "github.com/pingidentity/pingcli/internal/logger" "github.com/spf13/cobra" ) @@ -13,22 +12,20 @@ const ( pingcli config view-profile View configuration for a specific profile - pingcli config view-profile --profile myprofile` + pingcli config view-profile myprofile` ) func NewConfigViewProfileCommand() *cobra.Command { cmd := &cobra.Command{ - Args: common.ExactArgs(0), + Args: common.RangeArgs(0, 1), DisableFlagsInUseLine: true, // We write our own flags in @Use attribute Example: viewProfileCommandExamples, Long: `View the stored configuration of a custom configuration profile.`, RunE: configViewProfileRunE, Short: "View the stored configuration of a custom configuration profile.", - Use: "view-profile [flags]", + Use: "view-profile [flags] [profile-name]", } - cmd.Flags().AddFlag(options.ConfigViewProfileOption.Flag) - return cmd } @@ -36,7 +33,7 @@ func configViewProfileRunE(cmd *cobra.Command, args []string) error { l := logger.Get() l.Debug().Msgf("Config view-profile Subcommand Called.") - if err := config_internal.RunInternalConfigViewProfile(); err != nil { + if err := config_internal.RunInternalConfigViewProfile(args); err != nil { return err } diff --git a/cmd/config/view_profile_test.go b/cmd/config/view_profile_test.go index 38103437..4abaf9b0 100644 --- a/cmd/config/view_profile_test.go +++ b/cmd/config/view_profile_test.go @@ -13,9 +13,9 @@ func TestConfigViewProfileCmd_Execute(t *testing.T) { testutils.CheckExpectedError(t, err, nil) } -// Test Config Set Command Executes with --profile flag +// Test Config Set Command Executes with a defined profile func TestConfigViewProfileCmd_Execute_WithProfileFlag(t *testing.T) { - err := testutils_cobra.ExecutePingcli(t, "config", "view-profile", "--profile", "production") + err := testutils_cobra.ExecutePingcli(t, "config", "view-profile", "production") testutils.CheckExpectedError(t, err, nil) } @@ -29,13 +29,13 @@ func TestConfigViewProfileCmd_Execute_InvalidFlag(t *testing.T) { // Test Config Set Command fails with non-existent profile func TestConfigViewProfileCmd_Execute_NonExistentProfile(t *testing.T) { expectedErrorPattern := `^failed to view profile: invalid profile name: '.*' profile does not exist$` - err := testutils_cobra.ExecutePingcli(t, "config", "view-profile", "--profile", "non-existent") + err := testutils_cobra.ExecutePingcli(t, "config", "view-profile", "non-existent") testutils.CheckExpectedError(t, err, &expectedErrorPattern) } // Test Config Set Command fails with invalid profile func TestConfigViewProfileCmd_Execute_InvalidProfile(t *testing.T) { expectedErrorPattern := `^failed to view profile: invalid profile name: '.*'\. name must contain only alphanumeric characters, underscores, and dashes$` - err := testutils_cobra.ExecutePingcli(t, "config", "view-profile", "--profile", "(*&*(#))") + err := testutils_cobra.ExecutePingcli(t, "config", "view-profile", "(*&*(#))") testutils.CheckExpectedError(t, err, &expectedErrorPattern) } diff --git a/internal/commands/config/view_profile_internal.go b/internal/commands/config/view_profile_internal.go index b5e39e1f..85d3225f 100644 --- a/internal/commands/config/view_profile_internal.go +++ b/internal/commands/config/view_profile_internal.go @@ -8,10 +8,15 @@ import ( "github.com/pingidentity/pingcli/internal/profiles" ) -func RunInternalConfigViewProfile() (err error) { - pName, err := readConfigViewProfileOptions() - if err != nil { - return fmt.Errorf("failed to view profile: %v", err) +func RunInternalConfigViewProfile(args []string) (err error) { + var pName string + if len(args) == 1 { + pName = args[0] + } else { + pName, err = profiles.GetOptionValue(options.RootActiveProfileOption) + if err != nil { + return fmt.Errorf("failed to view profile: %v", err) + } } profileStr, err := profiles.GetMainConfig().ProfileToString(pName) @@ -28,21 +33,3 @@ func RunInternalConfigViewProfile() (err error) { return nil } - -func readConfigViewProfileOptions() (pName string, err error) { - if !options.ConfigViewProfileOption.Flag.Changed { - pName, err = profiles.GetOptionValue(options.RootActiveProfileOption) - } else { - pName, err = profiles.GetOptionValue(options.ConfigViewProfileOption) - } - - if err != nil { - return pName, err - } - - if pName == "" { - return pName, fmt.Errorf("unable to determine profile name to view") - } - - return pName, nil -} diff --git a/internal/commands/config/view_profile_internal_test.go b/internal/commands/config/view_profile_internal_test.go index 75de0df4..2fee080a 100644 --- a/internal/commands/config/view_profile_internal_test.go +++ b/internal/commands/config/view_profile_internal_test.go @@ -3,8 +3,6 @@ package config_internal import ( "testing" - "github.com/pingidentity/pingcli/internal/configuration/options" - "github.com/pingidentity/pingcli/internal/customtypes" "github.com/pingidentity/pingcli/internal/testing/testutils" "github.com/pingidentity/pingcli/internal/testing/testutils_viper" ) @@ -13,25 +11,16 @@ import ( func Test_RunInternalConfigViewProfile(t *testing.T) { testutils_viper.InitVipers(t) - err := RunInternalConfigViewProfile() - if err != nil { - t.Errorf("RunInternalConfigViewProfile returned error: %v", err) - } + err := RunInternalConfigViewProfile([]string{}) + testutils.CheckExpectedError(t, err, nil) } // Test RunInternalConfigViewProfile function fails with invalid profile name func Test_RunInternalConfigViewProfile_InvalidProfileName(t *testing.T) { testutils_viper.InitVipers(t) - var ( - profileName = customtypes.String("invalid") - ) - - options.ConfigViewProfileOption.Flag.Changed = true - options.ConfigViewProfileOption.CobraParamValue = &profileName - expectedErrorPattern := `^failed to view profile: invalid profile name: '.*' profile does not exist$` - err := RunInternalConfigViewProfile() + err := RunInternalConfigViewProfile([]string{"invalid"}) testutils.CheckExpectedError(t, err, &expectedErrorPattern) } @@ -39,15 +28,6 @@ func Test_RunInternalConfigViewProfile_InvalidProfileName(t *testing.T) { func Test_RunInternalConfigViewProfile_DifferentProfile(t *testing.T) { testutils_viper.InitVipers(t) - var ( - profileName = customtypes.String("production") - ) - - options.ConfigViewProfileOption.Flag.Changed = true - options.ConfigViewProfileOption.CobraParamValue = &profileName - - err := RunInternalConfigViewProfile() - if err != nil { - t.Errorf("RunInternalConfigViewProfile returned error: %v", err) - } + err := RunInternalConfigViewProfile([]string{"production"}) + testutils.CheckExpectedError(t, err, nil) } diff --git a/internal/configuration/config/view_profile.go b/internal/configuration/config/view_profile.go deleted file mode 100644 index 8d3f296a..00000000 --- a/internal/configuration/config/view_profile.go +++ /dev/null @@ -1,33 +0,0 @@ -package configuration_config - -import ( - "github.com/pingidentity/pingcli/internal/configuration/options" - "github.com/pingidentity/pingcli/internal/customtypes" - "github.com/spf13/pflag" -) - -func InitConfigViewProfileOptions() { - initViewProfileOption() -} - -func initViewProfileOption() { - cobraParamName := "profile" - cobraValue := new(customtypes.String) - defaultValue := customtypes.String("") - - options.ConfigViewProfileOption = options.Option{ - CobraParamName: cobraParamName, - CobraParamValue: cobraValue, - DefaultValue: &defaultValue, - EnvVar: "", // No environment variable - Flag: &pflag.Flag{ - Name: cobraParamName, - Shorthand: "p", - Usage: "The name of configuration profile to view.", - Value: cobraValue, - DefValue: "The active profile", - }, - Type: options.ENUM_STRING, - ViperKey: "", // No viper key - } -} diff --git a/internal/configuration/configuration.go b/internal/configuration/configuration.go index d8c10e3f..ff95b30d 100644 --- a/internal/configuration/configuration.go +++ b/internal/configuration/configuration.go @@ -84,7 +84,6 @@ func InitAllOptions() { configuration_config.InitConfigOptions() configuration_config.InitConfigAddProfileOptions() configuration_config.InitConfigDeleteProfileOptions() - configuration_config.InitConfigViewProfileOptions() configuration_config.InitConfigSetOptions() configuration_config.InitConfigGetOptions() configuration_config.InitConfigUnsetOptions() diff --git a/internal/configuration/options/options.go b/internal/configuration/options/options.go index 6f3a8068..6c5cabf0 100644 --- a/internal/configuration/options/options.go +++ b/internal/configuration/options/options.go @@ -73,7 +73,6 @@ func Options() []Option { ConfigAddProfileNameOption, ConfigAddProfileSetActiveOption, ConfigDeleteAutoAcceptOption, - ConfigViewProfileOption, ConfigGetProfileOption, ConfigSetProfileOption, ConfigUnsetProfileOption, @@ -124,8 +123,6 @@ var ( ConfigDeleteAutoAcceptOption Option - ConfigViewProfileOption Option - ConfigGetProfileOption Option ConfigSetProfileOption Option From 81d8da2e8242764718467b1e008358aa6ecdf0e5 Mon Sep 17 00:00:00 2001 From: Erik Ostien Date: Thu, 17 Oct 2024 12:56:59 +0200 Subject: [PATCH 4/4] Testing fixes --- cmd/config/set_active_profile_test.go | 2 +- cmd/request/request_test.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/config/set_active_profile_test.go b/cmd/config/set_active_profile_test.go index babd42bd..82c171d1 100644 --- a/cmd/config/set_active_profile_test.go +++ b/cmd/config/set_active_profile_test.go @@ -15,7 +15,7 @@ func TestConfigSetActiveProfileCmd_Execute(t *testing.T) { // Test Config set-active-profile Command fails when provided too many arguments func TestConfigSetActiveProfileCmd_TooManyArgs(t *testing.T) { - expectedErrorPattern := `^failed to execute '.*': command accepts 1 arg\(s\), received 2$` + expectedErrorPattern := `^failed to execute '.*': command accepts 0 to 1 arg\(s\), received 2$` err := testutils_cobra.ExecutePingcli(t, "config", "set-active-profile", "extra-arg", "extra-arg2") testutils.CheckExpectedError(t, err, &expectedErrorPattern) } diff --git a/cmd/request/request_test.go b/cmd/request/request_test.go index 38d75005..c4449c2b 100644 --- a/cmd/request/request_test.go +++ b/cmd/request/request_test.go @@ -84,7 +84,7 @@ func TestRequestCmd_Execute_InvalidService(t *testing.T) { err := testutils_cobra.ExecutePingcli(t, "request", "--service", "invalid-service", "--http-method", "GET", - "environments", + fmt.Sprintf("environments/%s/populations", os.Getenv(options.PingOneAuthenticationWorkerEnvironmentIDOption.EnvVar)), ) testutils.CheckExpectedError(t, err, &expectedErrorPattern) } @@ -95,7 +95,7 @@ func TestRequestCmd_Execute_InvalidHTTPMethod(t *testing.T) { err := testutils_cobra.ExecutePingcli(t, "request", "--service", "pingone", "--http-method", "INVALID", - "environments", + fmt.Sprintf("environments/%s/populations", os.Getenv(options.PingOneAuthenticationWorkerEnvironmentIDOption.EnvVar)), ) testutils.CheckExpectedError(t, err, &expectedErrorPattern) } @@ -103,6 +103,6 @@ func TestRequestCmd_Execute_InvalidHTTPMethod(t *testing.T) { // Test Request Command with Missing Required Service Flag func TestRequestCmd_Execute_MissingRequiredServiceFlag(t *testing.T) { expectedErrorPattern := `failed to send custom request: service is required` - err := testutils_cobra.ExecutePingcli(t, "request", "environments") + err := testutils_cobra.ExecutePingcli(t, "request", fmt.Sprintf("environments/%s/populations", os.Getenv(options.PingOneAuthenticationWorkerEnvironmentIDOption.EnvVar))) testutils.CheckExpectedError(t, err, &expectedErrorPattern) }