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

feat: add prompting to confirm deletion of gpg public key #20539

Merged
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
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) "))
}