1package bugcmd
 2
 3import (
 4	text "github.com/MichaelMure/go-term-text"
 5	"github.com/spf13/cobra"
 6
 7	"github.com/MichaelMure/git-bug/commands/execenv"
 8	"github.com/MichaelMure/git-bug/util/colors"
 9)
10
11func newBugCommentCommand() *cobra.Command {
12	env := execenv.NewEnv()
13
14	cmd := &cobra.Command{
15		Use:     "comment [BUG_ID]",
16		Short:   "List a bug's comments",
17		PreRunE: execenv.LoadBackend(env),
18		RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
19			return runBugComment(env, args)
20		}),
21		ValidArgsFunction: BugCompletion(env),
22	}
23
24	cmd.AddCommand(newBugCommentNewCommand())
25	cmd.AddCommand(newBugCommentEditCommand())
26
27	return cmd
28}
29
30func runBugComment(env *execenv.Env, args []string) error {
31	b, args, err := ResolveSelected(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.CombinedId().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}