comment.go

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