comment.go

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