1package commands
2
3import (
4 "fmt"
5
6 "github.com/MichaelMure/git-bug/cache"
7 "github.com/MichaelMure/git-bug/input"
8 "github.com/pkg/errors"
9 "github.com/spf13/cobra"
10)
11
12var (
13 commentAddMessageFile string
14 commentAddMessage string
15)
16
17func runCommentAdd(cmd *cobra.Command, args []string) error {
18 var err error
19
20 if len(args) > 1 {
21 return errors.New("Only one bug id is supported")
22 }
23
24 if len(args) == 0 {
25 return errors.New("You must provide a bug id")
26 }
27
28 backend, err := cache.NewRepoCache(repo)
29 if err != nil {
30 return err
31 }
32 defer backend.Close()
33
34 prefix := args[0]
35
36 if commentAddMessageFile != "" && commentAddMessage == "" {
37 commentAddMessage, err = input.FromFile(commentAddMessageFile)
38 if err != nil {
39 return err
40 }
41 }
42
43 if commentAddMessage == "" {
44 commentAddMessage, err = input.BugCommentEditorInput(backend.Repository())
45 if err == input.ErrEmptyMessage {
46 fmt.Println("Empty message, aborting.")
47 return nil
48 }
49 if err != nil {
50 return err
51 }
52 }
53
54 b, err := backend.ResolveBugPrefix(prefix)
55 if err != nil {
56 return err
57 }
58
59 err = b.AddComment(commentAddMessage)
60 if err != nil {
61 return err
62 }
63
64 return b.Commit()
65}
66
67var commentAddCmd = &cobra.Command{
68 Use: "add <id>",
69 Short: "Add a new comment to a bug",
70 RunE: runCommentAdd,
71}
72
73func init() {
74 commentCmd.AddCommand(commentAddCmd)
75
76 commentCmd.Flags().SortFlags = false
77
78 commentCmd.Flags().StringVarP(&commentAddMessageFile, "file", "F", "",
79 "Take the message from the given file. Use - to read the message from the standard input",
80 )
81
82 commentCmd.Flags().StringVarP(&commentAddMessage, "message", "m", "",
83 "Provide the new message from the command line",
84 )
85}