github.com/andrewhsu/cli/v2@v2.0.1-0.20210910131313-d4b4061f5b89/pkg/cmd/issue/delete/delete.go (about) 1 package delete 2 3 import ( 4 "fmt" 5 "net/http" 6 "strconv" 7 8 "github.com/AlecAivazis/survey/v2" 9 "github.com/andrewhsu/cli/v2/api" 10 "github.com/andrewhsu/cli/v2/internal/config" 11 "github.com/andrewhsu/cli/v2/internal/ghrepo" 12 "github.com/andrewhsu/cli/v2/pkg/cmd/issue/shared" 13 "github.com/andrewhsu/cli/v2/pkg/cmdutil" 14 "github.com/andrewhsu/cli/v2/pkg/iostreams" 15 "github.com/andrewhsu/cli/v2/pkg/prompt" 16 "github.com/spf13/cobra" 17 ) 18 19 type DeleteOptions struct { 20 HttpClient func() (*http.Client, error) 21 Config func() (config.Config, error) 22 IO *iostreams.IOStreams 23 BaseRepo func() (ghrepo.Interface, error) 24 25 SelectorArg string 26 } 27 28 func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Command { 29 opts := &DeleteOptions{ 30 IO: f.IOStreams, 31 HttpClient: f.HttpClient, 32 Config: f.Config, 33 } 34 35 cmd := &cobra.Command{ 36 Use: "delete {<number> | <url>}", 37 Short: "Delete issue", 38 Args: cobra.ExactArgs(1), 39 RunE: func(cmd *cobra.Command, args []string) error { 40 // support `-R, --repo` override 41 opts.BaseRepo = f.BaseRepo 42 43 if len(args) > 0 { 44 opts.SelectorArg = args[0] 45 } 46 47 if runF != nil { 48 return runF(opts) 49 } 50 return deleteRun(opts) 51 }, 52 } 53 54 return cmd 55 } 56 57 func deleteRun(opts *DeleteOptions) error { 58 cs := opts.IO.ColorScheme() 59 60 httpClient, err := opts.HttpClient() 61 if err != nil { 62 return err 63 } 64 apiClient := api.NewClientFromHTTP(httpClient) 65 66 issue, baseRepo, err := shared.IssueFromArg(apiClient, opts.BaseRepo, opts.SelectorArg) 67 if err != nil { 68 return err 69 } 70 71 // When executed in an interactive shell, require confirmation. Otherwise skip confirmation. 72 if opts.IO.CanPrompt() { 73 answer := "" 74 err = prompt.SurveyAskOne( 75 &survey.Input{ 76 Message: fmt.Sprintf("You're going to delete issue #%d. This action cannot be reversed. To confirm, type the issue number:", issue.Number), 77 }, 78 &answer, 79 ) 80 if err != nil { 81 return err 82 } 83 answerInt, err := strconv.Atoi(answer) 84 if err != nil || answerInt != issue.Number { 85 fmt.Fprintf(opts.IO.Out, "Issue #%d was not deleted.\n", issue.Number) 86 return nil 87 } 88 } 89 90 err = api.IssueDelete(apiClient, baseRepo, *issue) 91 if err != nil { 92 return err 93 } 94 95 fmt.Fprintf(opts.IO.ErrOut, "%s Deleted issue #%d (%s).\n", cs.Red("✔"), issue.Number, issue.Title) 96 97 return nil 98 }