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