Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDI-2056: [UAT] Update Config Commands to use Arguments #12

Merged
merged 4 commits into from
Oct 17, 2024
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
13 changes: 8 additions & 5 deletions cmd/config/delete_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,26 @@ 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.

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
}
Expand All @@ -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
}

Expand Down
46 changes: 21 additions & 25 deletions cmd/config/delete_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}
11 changes: 4 additions & 7 deletions cmd/config/set_active_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -15,30 +14,28 @@ 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.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.`,
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
}

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
}

Expand Down
12 changes: 6 additions & 6 deletions cmd/config/set_active_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 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)
}

Expand All @@ -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)
}

Expand Down
11 changes: 4 additions & 7 deletions cmd/config/view_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -13,30 +12,28 @@ 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
}

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
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/config/view_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}
6 changes: 3 additions & 3 deletions cmd/request/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -95,14 +95,14 @@ 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)
}

// 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)
}
42 changes: 27 additions & 15 deletions internal/commands/config/delete_profile_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = promptUserToDeleteProfile(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 := promptUserToConfirmDelete(pName, rc)
if err != nil {
return fmt.Errorf("failed to delete profile: %v", err)
}
Expand All @@ -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 promptUserToDeleteProfile(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 promptUserToConfirmDelete(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) {
Expand Down
Loading