bug_comment.go

 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/bug/select"
 8	"github.com/MichaelMure/git-bug/commands/completion"
 9	"github.com/MichaelMure/git-bug/commands/execenv"
10	"github.com/MichaelMure/git-bug/util/colors"
11)
12
13func newBugCommentCommand() *cobra.Command {
14	env := execenv.NewEnv()
15
16	cmd := &cobra.Command{
17		Use:     "comment [BUG_ID]",
18		Short:   "List a bug's comments",
19		PreRunE: execenv.LoadBackend(env),
20		RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
21			return runBugComment(env, args)
22		}),
23		ValidArgsFunction: completion.Bug(env),
24	}
25
26	cmd.AddCommand(newBugCommentNewCommand())
27	cmd.AddCommand(newBugCommentEditCommand())
28
29	return cmd
30}
31
32func runBugComment(env *execenv.Env, args []string) error {
33	b, args, err := _select.ResolveBug(env.Backend, args)
34	if err != nil {
35		return err
36	}
37
38	snap := b.Snapshot()
39
40	for i, comment := range snap.Comments {
41		if i != 0 {
42			env.Out.Println()
43		}
44
45		env.Out.Printf("Author: %s\n", colors.Magenta(comment.Author.DisplayName()))
46		env.Out.Printf("Id: %s\n", colors.Cyan(comment.CombinedId().Human()))
47		env.Out.Printf("Date: %s\n\n", comment.FormatTime())
48		env.Out.Println(text.LeftPadLines(comment.Message, 4))
49	}
50
51	return nil
52}