1package commands
2
3import (
4 "github.com/spf13/cobra"
5
6 _select "github.com/MichaelMure/git-bug/commands/select"
7 "github.com/MichaelMure/git-bug/input"
8)
9
10type commentAddOptions struct {
11 messageFile string
12 message string
13}
14
15func newCommentAddCommand() *cobra.Command {
16 env := newEnv()
17 options := commentAddOptions{}
18
19 cmd := &cobra.Command{
20 Use: "add [ID]",
21 Short: "Add a new comment to a bug.",
22 PreRunE: loadBackendEnsureUser(env),
23 PostRunE: closeBackend(env),
24 RunE: func(cmd *cobra.Command, args []string) error {
25 return runCommentAdd(env, options, args)
26 },
27 }
28
29 flags := cmd.Flags()
30 flags.SortFlags = false
31
32 flags.StringVarP(&options.messageFile, "file", "F", "",
33 "Take the message from the given file. Use - to read the message from the standard input")
34
35 flags.StringVarP(&options.message, "message", "m", "",
36 "Provide the new message from the command line")
37
38 return cmd
39}
40
41func runCommentAdd(env *Env, opts commentAddOptions, args []string) error {
42 b, args, err := _select.ResolveBug(env.backend, args)
43 if err != nil {
44 return err
45 }
46
47 if opts.messageFile != "" && opts.message == "" {
48 opts.message, err = input.BugCommentFileInput(opts.messageFile)
49 if err != nil {
50 return err
51 }
52 }
53
54 if opts.messageFile == "" && opts.message == "" {
55 opts.message, err = input.BugCommentEditorInput(env.backend, "")
56 if err == input.ErrEmptyMessage {
57 env.err.Println("Empty message, aborting.")
58 return nil
59 }
60 if err != nil {
61 return err
62 }
63 }
64
65 _, err = b.AddComment(opts.message)
66 if err != nil {
67 return err
68 }
69
70 return b.Commit()
71}