Support and favor delete for secrets (#5617)

Co-authored-by: Mislav Marohnić <mislav@github.com>
This commit is contained in:
Josh Soref 2022-05-16 08:24:05 -04:00 committed by GitHub
parent 60ea13b39e
commit 104d7655d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 36 deletions

View file

@ -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 <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
@ -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)
}
}

View file

@ -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

View file

@ -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
}