show.go

 1package commands
 2
 3import (
 4	"errors"
 5	"fmt"
 6	"strings"
 7
 8	"github.com/MichaelMure/git-bug/cache"
 9	"github.com/MichaelMure/git-bug/commands/select"
10	"github.com/MichaelMure/git-bug/util/colors"
11	"github.com/spf13/cobra"
12)
13
14func runShowBug(cmd *cobra.Command, args []string) error {
15	backend, err := cache.NewRepoCache(repo)
16	if err != nil {
17		return err
18	}
19	defer backend.Close()
20
21	b, args, err := _select.ResolveBug(backend, args)
22	if err != nil {
23		return err
24	}
25
26	snapshot := b.Snapshot()
27
28	if len(snapshot.Comments) == 0 {
29		return errors.New("Invalid bug: no comment")
30	}
31
32	firstComment := snapshot.Comments[0]
33
34	// Header
35	fmt.Printf("[%s] %s %s\n\n",
36		colors.Yellow(snapshot.Status),
37		colors.Cyan(snapshot.HumanId()),
38		snapshot.Title,
39	)
40
41	fmt.Printf("%s opened this issue %s\n\n",
42		colors.Magenta(firstComment.Author.DisplayName()),
43		firstComment.FormatTimeRel(),
44	)
45
46	var labels = make([]string, len(snapshot.Labels))
47	for i := range snapshot.Labels {
48		labels[i] = string(snapshot.Labels[i])
49	}
50
51	fmt.Printf("labels: %s\n\n",
52		strings.Join(labels, ", "),
53	)
54
55	// Comments
56	indent := "  "
57
58	for i, comment := range snapshot.Comments {
59		fmt.Printf("%s#%d %s <%s>\n\n",
60			indent,
61			i,
62			comment.Author.DisplayName(),
63			comment.Author.Email,
64		)
65
66		fmt.Printf("%s%s\n\n\n",
67			indent,
68			comment.Message,
69		)
70	}
71
72	return nil
73}
74
75var showCmd = &cobra.Command{
76	Use:     "show [<id>]",
77	Short:   "Display the details of a bug",
78	PreRunE: loadRepo,
79	RunE:    runShowBug,
80}
81
82func init() {
83	RootCmd.AddCommand(showCmd)
84}