github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/repo/deploy-key/delete/delete.go (about)

     1  package delete
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/ungtb10d/cli/v2/internal/ghrepo"
     8  	"github.com/ungtb10d/cli/v2/pkg/cmdutil"
     9  	"github.com/ungtb10d/cli/v2/pkg/iostreams"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  type DeleteOptions struct {
    14  	IO         *iostreams.IOStreams
    15  	HTTPClient func() (*http.Client, error)
    16  	BaseRepo   func() (ghrepo.Interface, error)
    17  
    18  	KeyID string
    19  }
    20  
    21  func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Command {
    22  	opts := &DeleteOptions{
    23  		HTTPClient: f.HttpClient,
    24  		IO:         f.IOStreams,
    25  	}
    26  
    27  	cmd := &cobra.Command{
    28  		Use:   "delete <key-id>",
    29  		Short: "Delete a deploy key from a GitHub repository",
    30  		Args:  cobra.ExactArgs(1),
    31  		RunE: func(cmd *cobra.Command, args []string) error {
    32  			opts.BaseRepo = f.BaseRepo
    33  			opts.KeyID = args[0]
    34  
    35  			if runF != nil {
    36  				return runF(opts)
    37  			}
    38  			return deleteRun(opts)
    39  		},
    40  	}
    41  
    42  	return cmd
    43  }
    44  
    45  func deleteRun(opts *DeleteOptions) error {
    46  	httpClient, err := opts.HTTPClient()
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	repo, err := opts.BaseRepo()
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	if err := deleteDeployKey(httpClient, repo, opts.KeyID); err != nil {
    57  		return err
    58  	}
    59  
    60  	if !opts.IO.IsStdoutTTY() {
    61  		return nil
    62  	}
    63  
    64  	cs := opts.IO.ColorScheme()
    65  	_, err = fmt.Fprintf(opts.IO.Out, "%s Deploy key deleted from %s\n", cs.SuccessIconWithColor(cs.Red), cs.Bold(ghrepo.FullName(repo)))
    66  	return err
    67  }