From 16e7517044df049a4ee56ff686e5e3a227f9375e Mon Sep 17 00:00:00 2001 From: pasha-codefresh Date: Sat, 26 Oct 2024 09:26:55 +0300 Subject: [PATCH] feat: add prompting to confirm deletion of gpg public key (#20539) --- cmd/argocd/commands/gpg.go | 17 ++++++++++++++--- cmd/argocd/commands/utils/prompt.go | 4 ++-- cmd/argocd/commands/utils/prompt_test.go | 19 +++---------------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/cmd/argocd/commands/gpg.go b/cmd/argocd/commands/gpg.go index d68c2918f1ccb..bc50655cfaec4 100644 --- a/cmd/argocd/commands/gpg.go +++ b/cmd/argocd/commands/gpg.go @@ -9,6 +9,7 @@ import ( "github.com/spf13/cobra" "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/headless" + "github.com/argoproj/argo-cd/v2/cmd/argocd/commands/utils" argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" gpgkeypkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/gpgkey" appsv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" @@ -167,11 +168,21 @@ func NewGPGDeleteCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command if len(args) != 1 { errors.CheckError(fmt.Errorf("Missing KEYID argument")) } + + keyId := args[0] + conn, gpgIf := headless.NewClientOrDie(clientOpts, c).NewGPGKeyClientOrDie() defer argoio.Close(conn) - _, err := gpgIf.Delete(ctx, &gpgkeypkg.GnuPGPublicKeyQuery{KeyID: args[0]}) - errors.CheckError(err) - fmt.Printf("Deleted key with key ID %s\n", args[0]) + + promptUtil := utils.NewPrompt(clientOpts.PromptsEnabled) + canDelete := promptUtil.Confirm(fmt.Sprintf("Are you sure you want to remove '%s'? [y/n] ", keyId)) + if canDelete { + _, err := gpgIf.Delete(ctx, &gpgkeypkg.GnuPGPublicKeyQuery{KeyID: keyId}) + errors.CheckError(err) + fmt.Printf("Deleted key with key ID %s\n", keyId) + } else { + fmt.Printf("The command to delete key with key ID '%s' was cancelled.\n", keyId) + } }, } return command diff --git a/cmd/argocd/commands/utils/prompt.go b/cmd/argocd/commands/utils/prompt.go index fdd13d6653658..5c47750e3ee6c 100644 --- a/cmd/argocd/commands/utils/prompt.go +++ b/cmd/argocd/commands/utils/prompt.go @@ -8,10 +8,10 @@ type Prompt struct { enabled bool } -func NewPrompt(promptsEnabled bool) (*Prompt, error) { +func NewPrompt(promptsEnabled bool) *Prompt { return &Prompt{ enabled: promptsEnabled, - }, nil + } } func (p *Prompt) Confirm(message string) bool { diff --git a/cmd/argocd/commands/utils/prompt_test.go b/cmd/argocd/commands/utils/prompt_test.go index 7f26d62aa838f..1da0c66cb901f 100644 --- a/cmd/argocd/commands/utils/prompt_test.go +++ b/cmd/argocd/commands/utils/prompt_test.go @@ -4,32 +4,19 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestNewPrompt_PromptsEnabled_True(t *testing.T) { - promptsEnabled := true - - prompt, err := NewPrompt(promptsEnabled) - require.NoError(t, err) - + prompt := NewPrompt(true) assert.True(t, prompt.enabled) } func TestNewPrompt_PromptsEnabled_False(t *testing.T) { - promptsEnabled := false - - prompt, err := NewPrompt(promptsEnabled) - require.NoError(t, err) - + prompt := NewPrompt(false) assert.False(t, prompt.enabled) } func TestConfirm_PromptsEnabled_False(t *testing.T) { - promptsEnabled := false - - prompt, err := NewPrompt(promptsEnabled) - require.NoError(t, err) - + prompt := NewPrompt(false) assert.True(t, prompt.Confirm("Are you sure you want to run this command? (y/n) ")) }