comment.go

 1package commands
 2
 3import (
 4	"fmt"
 5
 6	"github.com/MichaelMure/git-bug/bug"
 7	"github.com/MichaelMure/git-bug/cache"
 8	"github.com/MichaelMure/git-bug/commands/select"
 9	"github.com/MichaelMure/git-bug/util/colors"
10	"github.com/MichaelMure/git-bug/util/text"
11	"github.com/spf13/cobra"
12)
13
14func runComment(cmd *cobra.Command, args []string) error {
15	backend, err := cache.NewRepoCache(repo)
16	if err != nil {
17		return err
18	}
19	defer backend.Close()
20
21	b, args, err := _select.ResolveBug(backend, args)
22	if err != nil {
23		return err
24	}
25
26	snap := b.Snapshot()
27
28	commentsTextOutput(snap.Comments)
29
30	return nil
31}
32
33func commentsTextOutput(comments []bug.Comment) {
34	for i, comment := range comments {
35		if i != 0 {
36			fmt.Println()
37		}
38
39		fmt.Printf("Author: %s\n", colors.Magenta(comment.Author))
40		fmt.Printf("Date: %s\n\n", comment.FormatTime())
41		fmt.Println(text.LeftPad(comment.Message, 4))
42	}
43}
44
45var commentCmd = &cobra.Command{
46	Use:   "comment [<id>]",
47	Short: "Display or add comments",
48	RunE:  runComment,
49}
50
51func init() {
52	RootCmd.AddCommand(commentCmd)
53
54	commentCmd.Flags().SortFlags = false
55}