github.com/andrewhsu/cli/v2@v2.0.1-0.20210910131313-d4b4061f5b89/pkg/cmd/issue/comment/comment.go (about)

     1  package comment
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/MakeNowJust/heredoc"
     7  	"github.com/andrewhsu/cli/v2/api"
     8  	"github.com/andrewhsu/cli/v2/internal/ghrepo"
     9  	issueShared "github.com/andrewhsu/cli/v2/pkg/cmd/issue/shared"
    10  	prShared "github.com/andrewhsu/cli/v2/pkg/cmd/pr/shared"
    11  	"github.com/andrewhsu/cli/v2/pkg/cmdutil"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  func NewCmdComment(f *cmdutil.Factory, runF func(*prShared.CommentableOptions) error) *cobra.Command {
    16  	opts := &prShared.CommentableOptions{
    17  		IO:                    f.IOStreams,
    18  		HttpClient:            f.HttpClient,
    19  		EditSurvey:            prShared.CommentableEditSurvey(f.Config, f.IOStreams),
    20  		InteractiveEditSurvey: prShared.CommentableInteractiveEditSurvey(f.Config, f.IOStreams),
    21  		ConfirmSubmitSurvey:   prShared.CommentableConfirmSubmitSurvey,
    22  		OpenInBrowser:         f.Browser.Browse,
    23  	}
    24  
    25  	var bodyFile string
    26  
    27  	cmd := &cobra.Command{
    28  		Use:   "comment {<number> | <url>}",
    29  		Short: "Create a new issue comment",
    30  		Example: heredoc.Doc(`
    31  			$ gh issue comment 22 --body "I was able to reproduce this issue, lets fix it."
    32  		`),
    33  		Args: cobra.ExactArgs(1),
    34  		PreRunE: func(cmd *cobra.Command, args []string) error {
    35  			opts.RetrieveCommentable = retrieveIssue(f.HttpClient, f.BaseRepo, args[0])
    36  			return prShared.CommentablePreRun(cmd, opts)
    37  		},
    38  		RunE: func(_ *cobra.Command, args []string) error {
    39  			if bodyFile != "" {
    40  				b, err := cmdutil.ReadFile(bodyFile, opts.IO.In)
    41  				if err != nil {
    42  					return err
    43  				}
    44  				opts.Body = string(b)
    45  			}
    46  
    47  			if runF != nil {
    48  				return runF(opts)
    49  			}
    50  			return prShared.CommentableRun(opts)
    51  		},
    52  	}
    53  
    54  	cmd.Flags().StringVarP(&opts.Body, "body", "b", "", "Supply a body. Will prompt for one otherwise.")
    55  	cmd.Flags().StringVarP(&bodyFile, "body-file", "F", "", "Read body text from `file`")
    56  	cmd.Flags().BoolP("editor", "e", false, "Add body using editor")
    57  	cmd.Flags().BoolP("web", "w", false, "Add body in browser")
    58  
    59  	return cmd
    60  }
    61  
    62  func retrieveIssue(httpClient func() (*http.Client, error),
    63  	baseRepo func() (ghrepo.Interface, error),
    64  	selector string) func() (prShared.Commentable, ghrepo.Interface, error) {
    65  	return func() (prShared.Commentable, ghrepo.Interface, error) {
    66  		httpClient, err := httpClient()
    67  		if err != nil {
    68  			return nil, nil, err
    69  		}
    70  		apiClient := api.NewClientFromHTTP(httpClient)
    71  
    72  		issue, repo, err := issueShared.IssueFromArg(apiClient, baseRepo, selector)
    73  		if err != nil {
    74  			return nil, nil, err
    75  		}
    76  
    77  		return issue, repo, nil
    78  	}
    79  }