From 104d7655d7b30b3f70679e82bec251a73e1d9d02 Mon Sep 17 00:00:00 2001 From: Josh Soref <2119212+jsoref@users.noreply.github.com> Date: Mon, 16 May 2022 08:24:05 -0400 Subject: [PATCH] Support and favor delete for secrets (#5617) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mislav Marohnić --- .../{remove/remove.go => delete/delete.go} | 31 +++++++------- .../remove_test.go => delete/delete_test.go} | 40 +++++++++---------- pkg/cmd/secret/secret.go | 4 +- 3 files changed, 39 insertions(+), 36 deletions(-) rename pkg/cmd/secret/{remove/remove.go => delete/delete.go} (83%) rename pkg/cmd/secret/{remove/remove_test.go => delete/delete_test.go} (90%) diff --git a/pkg/cmd/secret/remove/remove.go b/pkg/cmd/secret/delete/delete.go similarity index 83% rename from pkg/cmd/secret/remove/remove.go rename to pkg/cmd/secret/delete/delete.go index a4ec51277..ee3a3f836 100644 --- a/pkg/cmd/secret/remove/remove.go +++ b/pkg/cmd/secret/delete/delete.go @@ -1,4 +1,4 @@ -package remove +package delete import ( "fmt" @@ -14,7 +14,7 @@ import ( "github.com/spf13/cobra" ) -type RemoveOptions struct { +type DeleteOptions struct { HttpClient func() (*http.Client, error) IO *iostreams.IOStreams Config func() (config.Config, error) @@ -27,18 +27,18 @@ type RemoveOptions struct { Application string } -func NewCmdRemove(f *cmdutil.Factory, runF func(*RemoveOptions) error) *cobra.Command { - opts := &RemoveOptions{ +func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Command { + opts := &DeleteOptions{ IO: f.IOStreams, Config: f.Config, HttpClient: f.HttpClient, } cmd := &cobra.Command{ - Use: "remove ", - Short: "Remove secrets", + Use: "delete ", + Short: "Delete secrets", Long: heredoc.Doc(` - Remove a secret on one of the following levels: + Delete a secret on one of the following levels: - repository (default): available to Actions runs or Dependabot in a repository - environment: available to Actions runs for a deployment environment in a repository - organization: available to Actions runs or Dependabot within an organization @@ -61,16 +61,19 @@ func NewCmdRemove(f *cmdutil.Factory, runF func(*RemoveOptions) error) *cobra.Co return removeRun(opts) }, + Aliases: []string{ + "remove", + }, } - cmd.Flags().StringVarP(&opts.OrgName, "org", "o", "", "Remove a secret for an organization") - cmd.Flags().StringVarP(&opts.EnvName, "env", "e", "", "Remove a secret for an environment") - cmd.Flags().BoolVarP(&opts.UserSecrets, "user", "u", false, "Remove a secret for your user") - cmdutil.StringEnumFlag(cmd, &opts.Application, "app", "a", "", []string{shared.Actions, shared.Codespaces, shared.Dependabot}, "Remove a secret for a specific application") + cmd.Flags().StringVarP(&opts.OrgName, "org", "o", "", "Delete a secret for an organization") + cmd.Flags().StringVarP(&opts.EnvName, "env", "e", "", "Delete a secret for an environment") + cmd.Flags().BoolVarP(&opts.UserSecrets, "user", "u", false, "Delete a secret for your user") + cmdutil.StringEnumFlag(cmd, &opts.Application, "app", "a", "", []string{shared.Actions, shared.Codespaces, shared.Dependabot}, "Delete a secret for a specific application") return cmd } -func removeRun(opts *RemoveOptions) error { +func removeRun(opts *DeleteOptions) error { c, err := opts.HttpClient() if err != nil { return fmt.Errorf("could not create http client: %w", err) @@ -142,9 +145,9 @@ func removeRun(opts *RemoveOptions) error { cs := opts.IO.ColorScheme() if envName != "" { - fmt.Fprintf(opts.IO.Out, "%s Removed secret %s from %s environment on %s\n", cs.SuccessIconWithColor(cs.Red), opts.SecretName, envName, target) + fmt.Fprintf(opts.IO.Out, "%s Deleted secret %s from %s environment on %s\n", cs.SuccessIconWithColor(cs.Red), opts.SecretName, envName, target) } else { - fmt.Fprintf(opts.IO.Out, "%s Removed %s secret %s from %s\n", cs.SuccessIconWithColor(cs.Red), secretApp.Title(), opts.SecretName, target) + fmt.Fprintf(opts.IO.Out, "%s Deleted %s secret %s from %s\n", cs.SuccessIconWithColor(cs.Red), secretApp.Title(), opts.SecretName, target) } } diff --git a/pkg/cmd/secret/remove/remove_test.go b/pkg/cmd/secret/delete/delete_test.go similarity index 90% rename from pkg/cmd/secret/remove/remove_test.go rename to pkg/cmd/secret/delete/delete_test.go index c7596e973..30030b104 100644 --- a/pkg/cmd/secret/remove/remove_test.go +++ b/pkg/cmd/secret/delete/delete_test.go @@ -1,4 +1,4 @@ -package remove +package delete import ( "bytes" @@ -14,11 +14,11 @@ import ( "github.com/stretchr/testify/assert" ) -func TestNewCmdRemove(t *testing.T) { +func TestNewCmdDelete(t *testing.T) { tests := []struct { name string cli string - wants RemoveOptions + wants DeleteOptions wantsErr bool }{ { @@ -28,14 +28,14 @@ func TestNewCmdRemove(t *testing.T) { { name: "repo", cli: "cool", - wants: RemoveOptions{ + wants: DeleteOptions{ SecretName: "cool", }, }, { name: "org", cli: "cool --org anOrg", - wants: RemoveOptions{ + wants: DeleteOptions{ SecretName: "cool", OrgName: "anOrg", }, @@ -43,7 +43,7 @@ func TestNewCmdRemove(t *testing.T) { { name: "env", cli: "cool --env anEnv", - wants: RemoveOptions{ + wants: DeleteOptions{ SecretName: "cool", EnvName: "anEnv", }, @@ -51,7 +51,7 @@ func TestNewCmdRemove(t *testing.T) { { name: "user", cli: "cool -u", - wants: RemoveOptions{ + wants: DeleteOptions{ SecretName: "cool", UserSecrets: true, }, @@ -59,7 +59,7 @@ func TestNewCmdRemove(t *testing.T) { { name: "Dependabot repo", cli: "cool --app Dependabot", - wants: RemoveOptions{ + wants: DeleteOptions{ SecretName: "cool", Application: "Dependabot", }, @@ -67,7 +67,7 @@ func TestNewCmdRemove(t *testing.T) { { name: "Dependabot org", cli: "cool --app Dependabot --org UmbrellaCorporation", - wants: RemoveOptions{ + wants: DeleteOptions{ SecretName: "cool", OrgName: "UmbrellaCorporation", Application: "Dependabot", @@ -85,8 +85,8 @@ func TestNewCmdRemove(t *testing.T) { argv, err := shlex.Split(tt.cli) assert.NoError(t, err) - var gotOpts *RemoveOptions - cmd := NewCmdRemove(f, func(opts *RemoveOptions) error { + var gotOpts *DeleteOptions + cmd := NewCmdDelete(f, func(opts *DeleteOptions) error { gotOpts = opts return nil }) @@ -113,12 +113,12 @@ func TestNewCmdRemove(t *testing.T) { func Test_removeRun_repo(t *testing.T) { tests := []struct { name string - opts *RemoveOptions + opts *DeleteOptions wantPath string }{ { name: "Actions", - opts: &RemoveOptions{ + opts: &DeleteOptions{ Application: "actions", SecretName: "cool_secret", }, @@ -126,7 +126,7 @@ func Test_removeRun_repo(t *testing.T) { }, { name: "Dependabot", - opts: &RemoveOptions{ + opts: &DeleteOptions{ Application: "dependabot", SecretName: "cool_dependabot_secret", }, @@ -134,7 +134,7 @@ func Test_removeRun_repo(t *testing.T) { }, { name: "defaults to Actions", - opts: &RemoveOptions{ + opts: &DeleteOptions{ SecretName: "cool_secret", }, wantPath: "repos/owner/repo/actions/secrets/cool_secret", @@ -177,7 +177,7 @@ func Test_removeRun_env(t *testing.T) { ios, _, _, _ := iostreams.Test() - opts := &RemoveOptions{ + opts := &DeleteOptions{ IO: ios, HttpClient: func() (*http.Client, error) { return &http.Client{Transport: reg}, nil @@ -201,19 +201,19 @@ func Test_removeRun_env(t *testing.T) { func Test_removeRun_org(t *testing.T) { tests := []struct { name string - opts *RemoveOptions + opts *DeleteOptions wantPath string }{ { name: "org", - opts: &RemoveOptions{ + opts: &DeleteOptions{ OrgName: "UmbrellaCorporation", }, wantPath: "orgs/UmbrellaCorporation/actions/secrets/tVirus", }, { name: "Dependabot org", - opts: &RemoveOptions{ + opts: &DeleteOptions{ Application: "dependabot", OrgName: "UmbrellaCorporation", }, @@ -262,7 +262,7 @@ func Test_removeRun_user(t *testing.T) { ios, _, _, _ := iostreams.Test() - opts := &RemoveOptions{ + opts := &DeleteOptions{ IO: ios, HttpClient: func() (*http.Client, error) { return &http.Client{Transport: reg}, nil diff --git a/pkg/cmd/secret/secret.go b/pkg/cmd/secret/secret.go index 94231b661..815ed6f85 100644 --- a/pkg/cmd/secret/secret.go +++ b/pkg/cmd/secret/secret.go @@ -2,8 +2,8 @@ package secret import ( "github.com/MakeNowJust/heredoc" + cmdDelete "github.com/cli/cli/v2/pkg/cmd/secret/delete" cmdList "github.com/cli/cli/v2/pkg/cmd/secret/list" - cmdRemove "github.com/cli/cli/v2/pkg/cmd/secret/remove" cmdSet "github.com/cli/cli/v2/pkg/cmd/secret/set" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/spf13/cobra" @@ -25,7 +25,7 @@ func NewCmdSecret(f *cmdutil.Factory) *cobra.Command { cmd.AddCommand(cmdList.NewCmdList(f, nil)) cmd.AddCommand(cmdSet.NewCmdSet(f, nil)) - cmd.AddCommand(cmdRemove.NewCmdRemove(f, nil)) + cmd.AddCommand(cmdDelete.NewCmdDelete(f, nil)) return cmd }