comment.go

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