github.com/andrewhsu/cli/v2@v2.0.1-0.20210910131313-d4b4061f5b89/pkg/cmd/secret/remove/remove.go (about)

     1  package remove
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/andrewhsu/cli/v2/api"
     8  	"github.com/andrewhsu/cli/v2/internal/config"
     9  	"github.com/andrewhsu/cli/v2/internal/ghrepo"
    10  	"github.com/andrewhsu/cli/v2/pkg/cmdutil"
    11  	"github.com/andrewhsu/cli/v2/pkg/iostreams"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  type RemoveOptions struct {
    16  	HttpClient func() (*http.Client, error)
    17  	IO         *iostreams.IOStreams
    18  	Config     func() (config.Config, error)
    19  	BaseRepo   func() (ghrepo.Interface, error)
    20  
    21  	SecretName string
    22  	OrgName    string
    23  	EnvName    string
    24  }
    25  
    26  func NewCmdRemove(f *cmdutil.Factory, runF func(*RemoveOptions) error) *cobra.Command {
    27  	opts := &RemoveOptions{
    28  		IO:         f.IOStreams,
    29  		Config:     f.Config,
    30  		HttpClient: f.HttpClient,
    31  	}
    32  
    33  	cmd := &cobra.Command{
    34  		Use:   "remove <secret-name>",
    35  		Short: "Remove secrets",
    36  		Long:  "Remove a secret for a repository, environment, or organization",
    37  		Args:  cobra.ExactArgs(1),
    38  		RunE: func(cmd *cobra.Command, args []string) error {
    39  			// support `-R, --repo` override
    40  			opts.BaseRepo = f.BaseRepo
    41  
    42  			if err := cmdutil.MutuallyExclusive("specify only one of `--org` or `--env`", opts.OrgName != "", opts.EnvName != ""); err != nil {
    43  				return err
    44  			}
    45  
    46  			opts.SecretName = args[0]
    47  
    48  			if runF != nil {
    49  				return runF(opts)
    50  			}
    51  
    52  			return removeRun(opts)
    53  		},
    54  	}
    55  	cmd.Flags().StringVarP(&opts.OrgName, "org", "o", "", "Remove a secret for an organization")
    56  	cmd.Flags().StringVarP(&opts.EnvName, "env", "e", "", "Remove a secret for an environment")
    57  
    58  	return cmd
    59  }
    60  
    61  func removeRun(opts *RemoveOptions) error {
    62  	c, err := opts.HttpClient()
    63  	if err != nil {
    64  		return fmt.Errorf("could not create http client: %w", err)
    65  	}
    66  	client := api.NewClientFromHTTP(c)
    67  
    68  	orgName := opts.OrgName
    69  	envName := opts.EnvName
    70  
    71  	var baseRepo ghrepo.Interface
    72  	if orgName == "" {
    73  		baseRepo, err = opts.BaseRepo()
    74  		if err != nil {
    75  			return fmt.Errorf("could not determine base repo: %w", err)
    76  		}
    77  	}
    78  
    79  	var path string
    80  	if orgName != "" {
    81  		path = fmt.Sprintf("orgs/%s/actions/secrets/%s", orgName, opts.SecretName)
    82  	} else if envName != "" {
    83  		path = fmt.Sprintf("repos/%s/environments/%s/secrets/%s", ghrepo.FullName(baseRepo), envName, opts.SecretName)
    84  	} else {
    85  		path = fmt.Sprintf("repos/%s/actions/secrets/%s", ghrepo.FullName(baseRepo), opts.SecretName)
    86  	}
    87  
    88  	cfg, err := opts.Config()
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	host, err := cfg.DefaultHost()
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	err = client.REST(host, "DELETE", path, nil, nil)
    99  	if err != nil {
   100  		return fmt.Errorf("failed to delete secret %s: %w", opts.SecretName, err)
   101  	}
   102  
   103  	if opts.IO.IsStdoutTTY() {
   104  		target := orgName
   105  		if orgName == "" {
   106  			target = ghrepo.FullName(baseRepo)
   107  		}
   108  		cs := opts.IO.ColorScheme()
   109  		if envName != "" {
   110  			fmt.Fprintf(opts.IO.Out, "%s Removed secret %s from %s environment on %s\n", cs.SuccessIconWithColor(cs.Red), opts.SecretName, envName, target)
   111  		} else {
   112  			fmt.Fprintf(opts.IO.Out, "%s Removed secret %s from %s\n", cs.SuccessIconWithColor(cs.Red), opts.SecretName, target)
   113  		}
   114  	}
   115  
   116  	return nil
   117  }