1package commands
2
3import (
4 "encoding/json"
5 "fmt"
6 "strings"
7 "time"
8
9 text "github.com/MichaelMure/go-term-text"
10 "github.com/spf13/cobra"
11
12 "github.com/MichaelMure/git-bug/bug"
13 "github.com/MichaelMure/git-bug/cache"
14 "github.com/MichaelMure/git-bug/query"
15 "github.com/MichaelMure/git-bug/util/colors"
16 "github.com/MichaelMure/git-bug/util/interrupt"
17)
18
19var (
20 lsQuery query.Query
21
22 lsStatusQuery []string
23 lsNoQuery []string
24 lsSortBy string
25 lsSortDirection string
26 lsOutputFormat string
27)
28
29func runLsBug(_ *cobra.Command, args []string) error {
30 backend, err := cache.NewRepoCache(repo)
31 if err != nil {
32 return err
33 }
34 defer backend.Close()
35 interrupt.RegisterCleaner(backend.Close)
36
37 var q *query.Query
38 if len(args) >= 1 {
39 q, err = query.Parse(strings.Join(args, " "))
40
41 if err != nil {
42 return err
43 }
44 } else {
45 err = completeQuery()
46 if err != nil {
47 return err
48 }
49 q = &lsQuery
50 }
51
52 allIds := backend.QueryBugs(q)
53
54 bugExcerpt := make([]*cache.BugExcerpt, len(allIds))
55 for i, id := range allIds {
56 b, err := backend.ResolveBugExcerpt(id)
57 if err != nil {
58 return err
59 }
60 bugExcerpt[i] = b
61 }
62
63 switch lsOutputFormat {
64 case "plain":
65 return lsPlainFormatter(backend, bugExcerpt)
66 case "json":
67 return lsJsonFormatter(backend, bugExcerpt)
68 case "default":
69 return lsDefaultFormatter(backend, bugExcerpt)
70 default:
71 return fmt.Errorf("unknown format %s", lsOutputFormat)
72 }
73}
74
75type JSONBug struct {
76 Id string `json:"id"`
77 HumanId string `json:"human_id"`
78 CreationTime time.Time `json:"creation_time"`
79 LastEdited time.Time `json:"last_edited"`
80
81 Status string `json:"status"`
82 Labels []bug.Label `json:"labels"`
83 Title string `json:"title"`
84 Actors []JSONIdentity `json:"actors"`
85 Participants []JSONIdentity `json:"participants"`
86 Author JSONIdentity `json:"author"`
87
88 Comments int `json:"comments"`
89 Metadata map[string]string `json:"metadata"`
90}
91
92type JSONIdentity struct {
93 Id string `json:"id"`
94 HumanId string `json:"human_id"`
95 Name string `json:"name"`
96 Login string `json:"login"`
97}
98
99func lsJsonFormatter(backend *cache.RepoCache, bugExcerpts []*cache.BugExcerpt) error {
100 for _, b := range bugExcerpts {
101 jsonBug := JSONBug{
102 b.Id.String(),
103 b.Id.Human(),
104 time.Unix(b.CreateUnixTime, 0),
105 time.Unix(b.EditUnixTime, 0),
106 b.Status.String(),
107 b.Labels,
108 b.Title,
109 []JSONIdentity{},
110 []JSONIdentity{},
111 JSONIdentity{},
112 b.LenComments,
113 b.CreateMetadata,
114 }
115
116 if b.AuthorId != "" {
117 author, err := backend.ResolveIdentityExcerpt(b.AuthorId)
118 if err != nil {
119 return err
120 }
121
122 jsonBug.Author.Name = author.DisplayName()
123 jsonBug.Author.Login = author.Login
124 jsonBug.Author.Id = author.Id.String()
125 jsonBug.Author.HumanId = author.Id.Human()
126 } else {
127 jsonBug.Author.Name = b.LegacyAuthor.DisplayName()
128 jsonBug.Author.Login = b.LegacyAuthor.Login
129 }
130
131 for _, element := range b.Actors {
132 actor, err := backend.ResolveIdentityExcerpt(element)
133 if err != nil {
134 return err
135 }
136
137 jsonBug.Actors = append(jsonBug.Actors, JSONIdentity{
138 actor.Id.String(),
139 actor.Id.Human(),
140 actor.Name,
141 actor.Login,
142 })
143 }
144
145 for _, element := range b.Participants {
146 participant, err := backend.ResolveIdentityExcerpt(element)
147 if err != nil {
148 return err
149 }
150 jsonBug.Participants = append(jsonBug.Participants, JSONIdentity{
151 participant.Id.String(),
152 participant.Id.Human(),
153 participant.DisplayName(),
154 participant.Login,
155 })
156 }
157
158 jsonObject, _ := json.MarshalIndent(jsonBug, "", " ")
159 fmt.Printf("%s\n", jsonObject)
160 }
161 return nil
162}
163
164func lsDefaultFormatter(backend *cache.RepoCache, bugExcerpts []*cache.BugExcerpt) error {
165 for _, b := range bugExcerpts {
166 var name string
167 if b.AuthorId != "" {
168 author, err := backend.ResolveIdentityExcerpt(b.AuthorId)
169 if err != nil {
170 return err
171 }
172 name = author.DisplayName()
173 } else {
174 name = b.LegacyAuthor.DisplayName()
175 }
176
177 var labelsTxt strings.Builder
178 for _, l := range b.Labels {
179 lc256 := l.Color().Term256()
180 labelsTxt.WriteString(lc256.Escape())
181 labelsTxt.WriteString(" ◼")
182 labelsTxt.WriteString(lc256.Unescape())
183 }
184
185 // truncate + pad if needed
186 labelsFmt := text.TruncateMax(labelsTxt.String(), 10)
187 titleFmt := text.LeftPadMaxLine(b.Title, 50-text.Len(labelsFmt), 0)
188 authorFmt := text.LeftPadMaxLine(name, 15, 0)
189
190 comments := fmt.Sprintf("%4d 💬", b.LenComments)
191 if b.LenComments > 9999 {
192 comments = " ∞ 💬"
193 }
194
195 fmt.Printf("%s %s\t%s\t%s\t%s\n",
196 colors.Cyan(b.Id.Human()),
197 colors.Yellow(b.Status),
198 titleFmt+labelsFmt,
199 colors.Magenta(authorFmt),
200 comments,
201 )
202 }
203 return nil
204}
205
206func lsPlainFormatter(backend *cache.RepoCache, bugExcerpts []*cache.BugExcerpt) error {
207 for _, b := range bugExcerpts {
208 fmt.Printf("[%s] %s\n", b.Status, b.Title)
209 }
210 return nil
211}
212
213// Finish the command flags transformation into the query.Query
214func completeQuery() error {
215 for _, str := range lsStatusQuery {
216 status, err := bug.StatusFromString(str)
217 if err != nil {
218 return err
219 }
220 lsQuery.Status = append(lsQuery.Status, status)
221 }
222
223 for _, no := range lsNoQuery {
224 switch no {
225 case "label":
226 lsQuery.NoLabel = true
227 default:
228 return fmt.Errorf("unknown \"no\" filter %s", no)
229 }
230 }
231
232 switch lsSortBy {
233 case "id":
234 lsQuery.OrderBy = query.OrderById
235 case "creation":
236 lsQuery.OrderBy = query.OrderByCreation
237 case "edit":
238 lsQuery.OrderBy = query.OrderByEdit
239 default:
240 return fmt.Errorf("unknown sort flag %s", lsSortBy)
241 }
242
243 switch lsSortDirection {
244 case "asc":
245 lsQuery.OrderDirection = query.OrderAscending
246 case "desc":
247 lsQuery.OrderDirection = query.OrderDescending
248 default:
249 return fmt.Errorf("unknown sort direction %s", lsSortDirection)
250 }
251
252 return nil
253}
254
255var lsCmd = &cobra.Command{
256 Use: "ls [<query>]",
257 Short: "List bugs.",
258 Long: `Display a summary of each bugs.
259
260You can pass an additional query to filter and order the list. This query can be expressed either with a simple query language or with flags.`,
261 Example: `List open bugs sorted by last edition with a query:
262git bug ls status:open sort:edit-desc
263
264List closed bugs sorted by creation with flags:
265git bug ls --status closed --by creation
266`,
267 PreRunE: loadRepo,
268 RunE: runLsBug,
269}
270
271func init() {
272 RootCmd.AddCommand(lsCmd)
273
274 lsCmd.Flags().SortFlags = false
275
276 lsCmd.Flags().StringSliceVarP(&lsStatusQuery, "status", "s", nil,
277 "Filter by status. Valid values are [open,closed]")
278 lsCmd.Flags().StringSliceVarP(&lsQuery.Author, "author", "a", nil,
279 "Filter by author")
280 lsCmd.Flags().StringSliceVarP(&lsQuery.Participant, "participant", "p", nil,
281 "Filter by participant")
282 lsCmd.Flags().StringSliceVarP(&lsQuery.Actor, "actor", "A", nil,
283 "Filter by actor")
284 lsCmd.Flags().StringSliceVarP(&lsQuery.Label, "label", "l", nil,
285 "Filter by label")
286 lsCmd.Flags().StringSliceVarP(&lsQuery.Title, "title", "t", nil,
287 "Filter by title")
288 lsCmd.Flags().StringSliceVarP(&lsNoQuery, "no", "n", nil,
289 "Filter by absence of something. Valid values are [label]")
290 lsCmd.Flags().StringVarP(&lsSortBy, "by", "b", "creation",
291 "Sort the results by a characteristic. Valid values are [id,creation,edit]")
292 lsCmd.Flags().StringVarP(&lsSortDirection, "direction", "d", "asc",
293 "Select the sorting direction. Valid values are [asc,desc]")
294 lsCmd.Flags().StringVarP(&lsOutputFormat, "format", "f", "default",
295 "Select the output formatting style. Valid values are [default, plain(text), json]")
296}