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
15var (
16 showFieldsQuery string
17)
18
19func runShowBug(cmd *cobra.Command, args []string) error {
20 backend, err := cache.NewRepoCache(repo)
21 if err != nil {
22 return err
23 }
24 defer backend.Close()
25 interrupt.RegisterCleaner(backend.Close)
26
27 b, args, err := _select.ResolveBug(backend, args)
28 if err != nil {
29 return err
30 }
31
32 snapshot := b.Snapshot()
33
34 if len(snapshot.Comments) == 0 {
35 return errors.New("Invalid bug: no comment")
36 }
37
38 firstComment := snapshot.Comments[0]
39
40 if showFieldsQuery != "" {
41 switch showFieldsQuery {
42 case "author":
43 fmt.Printf("%s\n", firstComment.Author.DisplayName())
44 case "authorEmail":
45 fmt.Printf("%s\n", firstComment.Author.Email())
46 case "createTime":
47 fmt.Printf("%s\n", firstComment.FormatTime())
48 case "id":
49 fmt.Printf("%s\n", snapshot.Id())
50 case "labels":
51 var labels = make([]string, len(snapshot.Labels))
52 fmt.Printf("%s\n", strings.Join(labels, ", "))
53 case "shortId":
54 fmt.Printf("%s\n", snapshot.HumanId())
55 case "status":
56 fmt.Printf("%s\n", snapshot.Status)
57 case "title":
58 fmt.Printf("%s\n", snapshot.Title)
59 default:
60 return fmt.Errorf("\nUnsupported field: %s\n", showFieldsQuery)
61 }
62
63 return nil
64 }
65
66 // Header
67 fmt.Printf("[%s] %s %s\n\n",
68 colors.Yellow(snapshot.Status),
69 colors.Cyan(snapshot.HumanId()),
70 snapshot.Title,
71 )
72
73 fmt.Printf("%s opened this issue %s\n\n",
74 colors.Magenta(firstComment.Author.DisplayName()),
75 firstComment.FormatTimeRel(),
76 )
77
78 var labels = make([]string, len(snapshot.Labels))
79 for i := range snapshot.Labels {
80 labels[i] = string(snapshot.Labels[i])
81 }
82
83 fmt.Printf("labels: %s\n\n",
84 strings.Join(labels, ", "),
85 )
86
87 // Comments
88 indent := " "
89
90 for i, comment := range snapshot.Comments {
91 var message string
92 fmt.Printf("%s#%d %s <%s>\n\n",
93 indent,
94 i,
95 comment.Author.DisplayName(),
96 comment.Author.Email(),
97 )
98
99 if comment.Message == "" {
100 message = colors.GreyBold("No description provided.")
101 } else {
102 message = comment.Message
103 }
104
105 fmt.Printf("%s%s\n\n\n",
106 indent,
107 message,
108 )
109 }
110
111 return nil
112}
113
114var showCmd = &cobra.Command{
115 Use: "show [<id>]",
116 Short: "Display the details of a bug",
117 PreRunE: loadRepo,
118 RunE: runShowBug,
119}
120
121func init() {
122 RootCmd.AddCommand(showCmd)
123 showCmd.Flags().StringVarP(&showFieldsQuery, "field", "f", "",
124 "Select field to display. Valid values are [author,authorEmail,createTime,id,labels,shortId,status,title]")
125}