1package commands
2
3import (
4 "encoding/json"
5 "errors"
6 "fmt"
7 "strings"
8
9 "github.com/spf13/cobra"
10
11 "github.com/MichaelMure/git-bug/bug"
12 "github.com/MichaelMure/git-bug/cache"
13 _select "github.com/MichaelMure/git-bug/commands/select"
14 "github.com/MichaelMure/git-bug/util/colors"
15 "github.com/MichaelMure/git-bug/util/interrupt"
16)
17
18var (
19 showFieldsQuery string
20 showOutputFormat string
21)
22
23func runShowBug(_ *cobra.Command, args []string) error {
24 backend, err := cache.NewRepoCache(repo)
25 if err != nil {
26 return err
27 }
28 defer backend.Close()
29 interrupt.RegisterCleaner(backend.Close)
30
31 b, args, err := _select.ResolveBug(backend, args)
32 if err != nil {
33 return err
34 }
35
36 snapshot := b.Snapshot()
37
38 if len(snapshot.Comments) == 0 {
39 return errors.New("invalid bug: no comment")
40 }
41
42 if showFieldsQuery != "" {
43 switch showFieldsQuery {
44 case "author":
45 fmt.Printf("%s\n", snapshot.Author.DisplayName())
46 case "authorEmail":
47 fmt.Printf("%s\n", snapshot.Author.Email())
48 case "createTime":
49 fmt.Printf("%s\n", snapshot.CreateTime.String())
50 case "lastEdit":
51 fmt.Printf("%s\n", snapshot.EditTime().String())
52 case "humanId":
53 fmt.Printf("%s\n", snapshot.Id().Human())
54 case "id":
55 fmt.Printf("%s\n", snapshot.Id())
56 case "labels":
57 for _, l := range snapshot.Labels {
58 fmt.Printf("%s\n", l.String())
59 }
60 case "actors":
61 for _, a := range snapshot.Actors {
62 fmt.Printf("%s\n", a.DisplayName())
63 }
64 case "participants":
65 for _, p := range snapshot.Participants {
66 fmt.Printf("%s\n", p.DisplayName())
67 }
68 case "shortId":
69 fmt.Printf("%s\n", snapshot.Id().Human())
70 case "status":
71 fmt.Printf("%s\n", snapshot.Status)
72 case "title":
73 fmt.Printf("%s\n", snapshot.Title)
74 default:
75 return fmt.Errorf("\nUnsupported field: %s\n", showFieldsQuery)
76 }
77
78 return nil
79 }
80
81 switch showOutputFormat {
82 case "org-mode":
83 return showOrgmodeFormatter(snapshot)
84 case "json":
85 return showJsonFormatter(snapshot)
86 case "default":
87 return showDefaultFormatter(snapshot)
88 default:
89 return fmt.Errorf("unknown format %s", showOutputFormat)
90 }
91}
92
93func showDefaultFormatter(snapshot *bug.Snapshot) error {
94 // Header
95 fmt.Printf("%s [%s] %s\n\n",
96 colors.Cyan(snapshot.Id().Human()),
97 colors.Yellow(snapshot.Status),
98 snapshot.Title,
99 )
100
101 fmt.Printf("%s opened this issue %s\n",
102 colors.Magenta(snapshot.Author.DisplayName()),
103 snapshot.CreateTime.String(),
104 )
105
106 fmt.Printf("This was last edited at %s\n\n",
107 snapshot.EditTime().String(),
108 )
109
110 // Labels
111 var labels = make([]string, len(snapshot.Labels))
112 for i := range snapshot.Labels {
113 labels[i] = string(snapshot.Labels[i])
114 }
115
116 fmt.Printf("labels: %s\n",
117 strings.Join(labels, ", "),
118 )
119
120 // Actors
121 var actors = make([]string, len(snapshot.Actors))
122 for i := range snapshot.Actors {
123 actors[i] = snapshot.Actors[i].DisplayName()
124 }
125
126 fmt.Printf("actors: %s\n",
127 strings.Join(actors, ", "),
128 )
129
130 // Participants
131 var participants = make([]string, len(snapshot.Participants))
132 for i := range snapshot.Participants {
133 participants[i] = snapshot.Participants[i].DisplayName()
134 }
135
136 fmt.Printf("participants: %s\n\n",
137 strings.Join(participants, ", "),
138 )
139
140 // Comments
141 indent := " "
142
143 for i, comment := range snapshot.Comments {
144 var message string
145 fmt.Printf("%s#%d %s <%s>\n\n",
146 indent,
147 i,
148 comment.Author.DisplayName(),
149 comment.Author.Email(),
150 )
151
152 if comment.Message == "" {
153 message = colors.GreyBold("No description provided.")
154 } else {
155 message = comment.Message
156 }
157
158 fmt.Printf("%s%s\n\n\n",
159 indent,
160 message,
161 )
162 }
163
164 return nil
165}
166
167type JSONBugSnapshot struct {
168 Id string `json:"id"`
169 HumanId string `json:"human_id"`
170 CreateTime JSONTime `json:"create_time"`
171 EditTime JSONTime `json:"edit_time"`
172 Status string `json:"status"`
173 Labels []bug.Label `json:"labels"`
174 Title string `json:"title"`
175 Author JSONIdentity `json:"author"`
176 Actors []JSONIdentity `json:"actors"`
177 Participants []JSONIdentity `json:"participants"`
178 Comments []JSONComment `json:"comments"`
179}
180
181type JSONComment struct {
182 Id string `json:"id"`
183 HumanId string `json:"human_id"`
184 Author JSONIdentity `json:"author"`
185 Message string `json:"message"`
186}
187
188func NewJSONComment(comment bug.Comment) JSONComment {
189 return JSONComment{
190 Id: comment.Id().String(),
191 HumanId: comment.Id().Human(),
192 Author: NewJSONIdentity(comment.Author),
193 Message: comment.Message,
194 }
195}
196
197func showJsonFormatter(snapshot *bug.Snapshot) error {
198 jsonBug := JSONBugSnapshot{
199 Id: snapshot.Id().String(),
200 HumanId: snapshot.Id().Human(),
201 CreateTime: NewJSONTime(snapshot.CreateTime, 0),
202 EditTime: NewJSONTime(snapshot.EditTime(), 0),
203 Status: snapshot.Status.String(),
204 Labels: snapshot.Labels,
205 Title: snapshot.Title,
206 Author: NewJSONIdentity(snapshot.Author),
207 }
208
209 jsonBug.Actors = make([]JSONIdentity, len(snapshot.Actors))
210 for i, element := range snapshot.Actors {
211 jsonBug.Actors[i] = NewJSONIdentity(element)
212 }
213
214 jsonBug.Participants = make([]JSONIdentity, len(snapshot.Participants))
215 for i, element := range snapshot.Participants {
216 jsonBug.Participants[i] = NewJSONIdentity(element)
217 }
218
219 jsonBug.Comments = make([]JSONComment, len(snapshot.Comments))
220 for i, comment := range snapshot.Comments {
221 jsonBug.Comments[i] = NewJSONComment(comment)
222 }
223
224 jsonObject, _ := json.MarshalIndent(jsonBug, "", " ")
225 fmt.Printf("%s\n", jsonObject)
226
227 return nil
228}
229
230func showOrgmodeFormatter(snapshot *bug.Snapshot) error {
231 // Header
232 fmt.Printf("%s [%s] %s\n",
233 snapshot.Id().Human(),
234 snapshot.Status,
235 snapshot.Title,
236 )
237
238 fmt.Printf("* Author: %s\n",
239 snapshot.Author.DisplayName(),
240 )
241
242 fmt.Printf("* Creation Time: %s\n",
243 snapshot.CreateTime.String(),
244 )
245
246 fmt.Printf("* Last Edit: %s\n",
247 snapshot.EditTime().String(),
248 )
249
250 // Labels
251 var labels = make([]string, len(snapshot.Labels))
252 for i, label := range snapshot.Labels {
253 labels[i] = string(label)
254 }
255
256 fmt.Printf("* Labels:\n")
257 if len(labels) > 0 {
258 fmt.Printf("** %s\n",
259 strings.Join(labels, "\n** "),
260 )
261 }
262
263 // Actors
264 var actors = make([]string, len(snapshot.Actors))
265 for i, actor := range snapshot.Actors {
266 actors[i] = fmt.Sprintf("%s %s",
267 actor.Id().Human(),
268 actor.DisplayName(),
269 )
270 }
271
272 fmt.Printf("* Actors:\n** %s\n",
273 strings.Join(actors, "\n** "),
274 )
275
276 // Participants
277 var participants = make([]string, len(snapshot.Participants))
278 for i, participant := range snapshot.Participants {
279 participants[i] = fmt.Sprintf("%s %s",
280 participant.Id().Human(),
281 participant.DisplayName(),
282 )
283 }
284
285 fmt.Printf("* Participants:\n** %s\n",
286 strings.Join(participants, "\n** "),
287 )
288
289 fmt.Printf("* Comments:\n")
290
291 for i, comment := range snapshot.Comments {
292 var message string
293 fmt.Printf("** #%d %s\n",
294 i,
295 comment.Author.DisplayName(),
296 )
297
298 if comment.Message == "" {
299 message = "No description provided."
300 } else {
301 message = strings.ReplaceAll(comment.Message, "\n", "\n: ")
302 }
303
304 fmt.Printf(": %s\n",
305 message,
306 )
307 }
308
309 return nil
310}
311
312var showCmd = &cobra.Command{
313 Use: "show [<id>]",
314 Short: "Display the details of a bug.",
315 PreRunE: loadRepo,
316 RunE: runShowBug,
317}
318
319func init() {
320 RootCmd.AddCommand(showCmd)
321 showCmd.Flags().StringVarP(&showFieldsQuery, "field", "", "",
322 "Select field to display. Valid values are [author,authorEmail,createTime,lastEdit,humanId,id,labels,shortId,status,title,actors,participants]")
323 showCmd.Flags().StringVarP(&showOutputFormat, "format", "f", "default",
324 "Select the output formatting style. Valid values are [default,json,org-mode]")
325}