1package commands
2
3import (
4 "errors"
5 "fmt"
6 "strings"
7
8 "github.com/MichaelMure/git-bug/cache"
9 "github.com/MichaelMure/git-bug/util/colors"
10 "github.com/spf13/cobra"
11)
12
13func runShowBug(cmd *cobra.Command, args []string) error {
14 if len(args) > 1 {
15 return errors.New("Only showing one bug at a time is supported")
16 }
17
18 if len(args) == 0 {
19 return errors.New("You must provide a bug id")
20 }
21
22 backend, err := cache.NewRepoCache(repo)
23 if err != nil {
24 return err
25 }
26 defer backend.Close()
27
28 prefix := args[0]
29
30 b, err := backend.ResolveBugPrefix(prefix)
31 if err != nil {
32 return err
33 }
34
35 snapshot := b.Snapshot()
36
37 if len(snapshot.Comments) == 0 {
38 return errors.New("Invalid bug: no comment")
39 }
40
41 firstComment := snapshot.Comments[0]
42
43 // Header
44 fmt.Printf("[%s] %s %s\n\n",
45 colors.Yellow(snapshot.Status),
46 colors.Cyan(snapshot.HumanId()),
47 snapshot.Title,
48 )
49
50 fmt.Printf("%s opened this issue %s\n\n",
51 colors.Magenta(firstComment.Author.Name),
52 firstComment.FormatTimeRel(),
53 )
54
55 var labels = make([]string, len(snapshot.Labels))
56 for i := range snapshot.Labels {
57 labels[i] = string(snapshot.Labels[i])
58 }
59
60 fmt.Printf("labels: %s\n\n",
61 strings.Join(labels, ", "),
62 )
63
64 // Comments
65 indent := " "
66
67 for i, comment := range snapshot.Comments {
68 fmt.Printf("%s#%d %s <%s>\n\n",
69 indent,
70 i,
71 comment.Author.Name,
72 comment.Author.Email,
73 )
74
75 fmt.Printf("%s%s\n\n\n",
76 indent,
77 comment.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 RunE: runShowBug,
88}
89
90func init() {
91 RootCmd.AddCommand(showCmd)
92}