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