1package commands
 2
 3import (
 4	"errors"
 5	"fmt"
 6	"github.com/MichaelMure/git-bug/bug"
 7	"github.com/MichaelMure/git-bug/util"
 8	"github.com/spf13/cobra"
 9	"strings"
10)
11
12func runShowBug(cmd *cobra.Command, args []string) error {
13	if len(args) > 1 {
14		return errors.New("Only showing one bug at a time is supported")
15	}
16
17	if len(args) == 0 {
18		return errors.New("You must provide a bug id")
19	}
20
21	prefix := args[0]
22
23	b, err := bug.FindLocalBug(repo, prefix)
24	if err != nil {
25		return err
26	}
27
28	snapshot := b.Compile()
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		util.Yellow(snapshot.Status),
39		util.Cyan(snapshot.HumanId()),
40		snapshot.Title,
41	)
42
43	fmt.Printf("%s opened this issue %s\n\n",
44		util.Magenta(firstComment.Author.Name),
45		firstComment.FormatTime(),
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		fmt.Printf("%s#%d %s <%s>\n\n",
62			indent,
63			i,
64			comment.Author.Name,
65			comment.Author.Email,
66		)
67
68		fmt.Printf("%s%s\n\n\n",
69			indent,
70			comment.Message,
71		)
72	}
73
74	return nil
75}
76
77var showCmd = &cobra.Command{
78	Use:   "show <id>",
79	Short: "Display the details of a bug",
80	RunE:  runShowBug,
81}
82
83func init() {
84	RootCmd.AddCommand(showCmd)
85}