Skip to content

Commit

Permalink
Support and favor delete for secrets (cli#5617)
Browse files Browse the repository at this point in the history
Co-authored-by: Mislav Marohnić <[email protected]>
  • Loading branch information
jsoref and mislav authored May 16, 2022
1 parent 60ea13b commit 104d765
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
31 changes: 17 additions & 14 deletions pkg/cmd/secret/remove/remove.go → pkg/cmd/secret/delete/delete.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package remove
package delete

import (
"fmt"
Expand All @@ -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)
Expand All @@ -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 <secret-name>",
Short: "Remove secrets",
Use: "delete <secret-name>",
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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package remove
package delete

import (
"bytes"
Expand All @@ -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
}{
{
Expand All @@ -28,46 +28,46 @@ 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",
},
},
{
name: "env",
cli: "cool --env anEnv",
wants: RemoveOptions{
wants: DeleteOptions{
SecretName: "cool",
EnvName: "anEnv",
},
},
{
name: "user",
cli: "cool -u",
wants: RemoveOptions{
wants: DeleteOptions{
SecretName: "cool",
UserSecrets: true,
},
},
{
name: "Dependabot repo",
cli: "cool --app Dependabot",
wants: RemoveOptions{
wants: DeleteOptions{
SecretName: "cool",
Application: "Dependabot",
},
},
{
name: "Dependabot org",
cli: "cool --app Dependabot --org UmbrellaCorporation",
wants: RemoveOptions{
wants: DeleteOptions{
SecretName: "cool",
OrgName: "UmbrellaCorporation",
Application: "Dependabot",
Expand All @@ -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
})
Expand All @@ -113,28 +113,28 @@ 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",
},
wantPath: "repos/owner/repo/actions/secrets/cool_secret",
},
{
name: "Dependabot",
opts: &RemoveOptions{
opts: &DeleteOptions{
Application: "dependabot",
SecretName: "cool_dependabot_secret",
},
wantPath: "repos/owner/repo/dependabot/secrets/cool_dependabot_secret",
},
{
name: "defaults to Actions",
opts: &RemoveOptions{
opts: &DeleteOptions{
SecretName: "cool_secret",
},
wantPath: "repos/owner/repo/actions/secrets/cool_secret",
Expand Down Expand Up @@ -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
Expand All @@ -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",
},
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

0 comments on commit 104d765

Please sign in to comment.