show.go

 1package commands
 2
 3import (
 4	"errors"
 5	"fmt"
 6	"github.com/MichaelMure/git-bug/bug"
 7	"github.com/MichaelMure/git-bug/repository"
 8	"github.com/MichaelMure/git-bug/util"
 9	"strings"
10)
11
12var line = strings.Repeat("-", 50)
13
14func runShowBug(repo repository.Repo, args []string) error {
15	if len(args) > 1 {
16		return errors.New("Only showing one bug at a time is supported")
17	}
18
19	if len(args) == 0 {
20		return errors.New("You must provide a bug id")
21	}
22
23	prefix := args[0]
24
25	b, err := bug.FindBug(repo, prefix)
26	if err != nil {
27		return err
28	}
29
30	snapshot := b.Compile()
31
32	if len(snapshot.Comments) == 0 {
33		return errors.New("Invalid bug: no comment")
34	}
35
36	firstComment := snapshot.Comments[0]
37
38	// Header
39	fmt.Printf("[%s] %s %s\n\n",
40		util.Yellow(snapshot.Status),
41		util.Cyan(snapshot.HumanId()),
42		snapshot.Title,
43	)
44
45	fmt.Printf("%s opened this issue %s\n\n",
46		util.Magenta(firstComment.Author.Name),
47		firstComment.FormatTime(),
48	)
49
50	// Comments
51	indent := "  "
52
53	for i, comment := range snapshot.Comments {
54		fmt.Printf("%s#%d %s <%s>\n\n",
55			indent,
56			i,
57			comment.Author.Name,
58			comment.Author.Email,
59		)
60
61		fmt.Printf("%s%s\n\n\n",
62			indent,
63			comment.Message,
64		)
65	}
66
67	return nil
68}
69
70var showCmd = &Command{
71	Description: "Display the details of a bug",
72	Usage:       "<id>",
73	RunMethod:   runShowBug,
74}