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