github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/issue/comment/comment.go (about) 1 package comment 2 3 import ( 4 "github.com/MakeNowJust/heredoc" 5 "github.com/ungtb10d/cli/v2/internal/ghrepo" 6 issueShared "github.com/ungtb10d/cli/v2/pkg/cmd/issue/shared" 7 prShared "github.com/ungtb10d/cli/v2/pkg/cmd/pr/shared" 8 "github.com/ungtb10d/cli/v2/pkg/cmdutil" 9 "github.com/spf13/cobra" 10 ) 11 12 func NewCmdComment(f *cmdutil.Factory, runF func(*prShared.CommentableOptions) error) *cobra.Command { 13 opts := &prShared.CommentableOptions{ 14 IO: f.IOStreams, 15 HttpClient: f.HttpClient, 16 EditSurvey: prShared.CommentableEditSurvey(f.Config, f.IOStreams), 17 InteractiveEditSurvey: prShared.CommentableInteractiveEditSurvey(f.Config, f.IOStreams), 18 ConfirmSubmitSurvey: prShared.CommentableConfirmSubmitSurvey, 19 OpenInBrowser: f.Browser.Browse, 20 } 21 22 var bodyFile string 23 24 cmd := &cobra.Command{ 25 Use: "comment {<number> | <url>}", 26 Short: "Add a comment to an issue", 27 Long: heredoc.Doc(` 28 Add a comment to a GitHub issue. 29 30 Without the body text supplied through flags, the command will interactively 31 prompt for the comment text. 32 `), 33 Example: heredoc.Doc(` 34 $ gh issue comment 12 --body "Hi from GitHub CLI" 35 `), 36 Args: cobra.ExactArgs(1), 37 PreRunE: func(cmd *cobra.Command, args []string) error { 38 opts.RetrieveCommentable = func() (prShared.Commentable, ghrepo.Interface, error) { 39 httpClient, err := f.HttpClient() 40 if err != nil { 41 return nil, nil, err 42 } 43 fields := []string{"id", "url"} 44 if opts.EditLast { 45 fields = append(fields, "comments") 46 } 47 return issueShared.IssueFromArgWithFields(httpClient, f.BaseRepo, args[0], fields) 48 } 49 return prShared.CommentablePreRun(cmd, opts) 50 }, 51 RunE: func(_ *cobra.Command, args []string) error { 52 if bodyFile != "" { 53 b, err := cmdutil.ReadFile(bodyFile, opts.IO.In) 54 if err != nil { 55 return err 56 } 57 opts.Body = string(b) 58 } 59 60 if runF != nil { 61 return runF(opts) 62 } 63 return prShared.CommentableRun(opts) 64 }, 65 } 66 67 cmd.Flags().StringVarP(&opts.Body, "body", "b", "", "The comment body `text`") 68 cmd.Flags().StringVarP(&bodyFile, "body-file", "F", "", "Read body text from `file` (use \"-\" to read from standard input)") 69 cmd.Flags().BoolP("editor", "e", false, "Skip prompts and open the text editor to write the body in") 70 cmd.Flags().BoolP("web", "w", false, "Open the web browser to write the comment") 71 cmd.Flags().BoolVar(&opts.EditLast, "edit-last", false, "Edit the last comment of the same author") 72 73 return cmd 74 }