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