github.com/andrewhsu/cli/v2@v2.0.1-0.20210910131313-d4b4061f5b89/pkg/cmd/issue/close/close.go (about) 1 package close 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/cmd/issue/shared" 11 "github.com/andrewhsu/cli/v2/pkg/cmdutil" 12 "github.com/andrewhsu/cli/v2/pkg/iostreams" 13 "github.com/spf13/cobra" 14 ) 15 16 type CloseOptions struct { 17 HttpClient func() (*http.Client, error) 18 Config func() (config.Config, error) 19 IO *iostreams.IOStreams 20 BaseRepo func() (ghrepo.Interface, error) 21 22 SelectorArg string 23 } 24 25 func NewCmdClose(f *cmdutil.Factory, runF func(*CloseOptions) error) *cobra.Command { 26 opts := &CloseOptions{ 27 IO: f.IOStreams, 28 HttpClient: f.HttpClient, 29 Config: f.Config, 30 } 31 32 cmd := &cobra.Command{ 33 Use: "close {<number> | <url>}", 34 Short: "Close issue", 35 Args: cobra.ExactArgs(1), 36 RunE: func(cmd *cobra.Command, args []string) error { 37 // support `-R, --repo` override 38 opts.BaseRepo = f.BaseRepo 39 40 if len(args) > 0 { 41 opts.SelectorArg = args[0] 42 } 43 44 if runF != nil { 45 return runF(opts) 46 } 47 return closeRun(opts) 48 }, 49 } 50 51 return cmd 52 } 53 54 func closeRun(opts *CloseOptions) error { 55 cs := opts.IO.ColorScheme() 56 57 httpClient, err := opts.HttpClient() 58 if err != nil { 59 return err 60 } 61 apiClient := api.NewClientFromHTTP(httpClient) 62 63 issue, baseRepo, err := shared.IssueFromArg(apiClient, opts.BaseRepo, opts.SelectorArg) 64 if err != nil { 65 return err 66 } 67 68 if issue.State == "CLOSED" { 69 fmt.Fprintf(opts.IO.ErrOut, "%s Issue #%d (%s) is already closed\n", cs.Yellow("!"), issue.Number, issue.Title) 70 return nil 71 } 72 73 err = api.IssueClose(apiClient, baseRepo, *issue) 74 if err != nil { 75 return err 76 } 77 78 fmt.Fprintf(opts.IO.ErrOut, "%s Closed issue #%d (%s)\n", cs.SuccessIconWithColor(cs.Red), issue.Number, issue.Title) 79 80 return nil 81 }