comment.go

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