Skip to content

Commit

Permalink
feat: add prompting to confirm deletion of gpg public key (#20539)
Browse files Browse the repository at this point in the history
  • Loading branch information
pasha-codefresh authored Oct 26, 2024
1 parent 745f1cc commit 16e7517
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
17 changes: 14 additions & 3 deletions cmd/argocd/commands/gpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/argocd/commands/utils/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 3 additions & 16 deletions cmd/argocd/commands/utils/prompt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) "))
}

0 comments on commit 16e7517

Please sign in to comment.