1package commands
 2
 3import (
 4	text "github.com/MichaelMure/go-term-text"
 5	"github.com/spf13/cobra"
 6
 7	_select "github.com/MichaelMure/git-bug/commands/select"
 8	"github.com/MichaelMure/git-bug/util/colors"
 9)
10
11func newCommentCommand() *cobra.Command {
12	env := newEnv()
13
14	cmd := &cobra.Command{
15		Use:     "comment [ID]",
16		Short:   "Display or add comments to a bug.",
17		PreRunE: loadBackend(env),
18		RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
19			return runComment(env, args)
20		}),
21		ValidArgsFunction: completeBug(env),
22	}
23
24	cmd.AddCommand(newCommentAddCommand())
25	cmd.AddCommand(newCommentEditCommand())
26
27	return cmd
28}
29
30func runComment(env *Env, args []string) error {
31	b, args, err := _select.ResolveBug(env.backend, args)
32	if err != nil {
33		return err
34	}
35
36	snap := b.Snapshot()
37
38	for i, comment := range snap.Comments {
39		if i != 0 {
40			env.out.Println()
41		}
42
43		env.out.Printf("Author: %s\n", colors.Magenta(comment.Author.DisplayName()))
44		env.out.Printf("Id: %s\n", colors.Cyan(comment.Id().Human()))
45		env.out.Printf("Date: %s\n\n", comment.FormatTime())
46		env.out.Println(text.LeftPadLines(comment.Message, 4))
47	}
48
49	return nil
50}