ls.go

  1package commands
  2
  3import (
  4	"encoding/json"
  5	"fmt"
  6	"regexp"
  7	"strings"
  8	"time"
  9
 10	text "github.com/MichaelMure/go-term-text"
 11	"github.com/spf13/cobra"
 12
 13	"github.com/MichaelMure/git-bug/bug"
 14	"github.com/MichaelMure/git-bug/cache"
 15	"github.com/MichaelMure/git-bug/query"
 16	"github.com/MichaelMure/git-bug/util/colors"
 17)
 18
 19type lsOptions struct {
 20	statusQuery      []string
 21	authorQuery      []string
 22	metadataQuery    []string
 23	participantQuery []string
 24	actorQuery       []string
 25	labelQuery       []string
 26	titleQuery       []string
 27	noQuery          []string
 28	sortBy           string
 29	sortDirection    string
 30	outputFormat     string
 31}
 32
 33func newLsCommand() *cobra.Command {
 34	env := newEnv()
 35	options := lsOptions{}
 36
 37	cmd := &cobra.Command{
 38		Use:   "ls [QUERY]",
 39		Short: "List bugs.",
 40		Long: `Display a summary of each bugs.
 41
 42You can pass an additional query to filter and order the list. This query can be expressed either with a simple query language, flags, a natural language full text search, or a combination of the aforementioned.`,
 43		Example: `List open bugs sorted by last edition with a query:
 44git bug ls status:open sort:edit-desc
 45
 46List closed bugs sorted by creation with flags:
 47git bug ls --status closed --by creation
 48
 49Do a full text search of all bugs:
 50git bug ls "foo bar" baz
 51
 52Use queries, flags, and full text search:
 53git bug ls status:open --by creation "foo bar" baz
 54`,
 55		PreRunE: loadBackend(env),
 56		RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
 57			return runLs(env, options, args)
 58		}),
 59	}
 60
 61	flags := cmd.Flags()
 62	flags.SortFlags = false
 63
 64	flags.StringSliceVarP(&options.statusQuery, "status", "s", nil,
 65		"Filter by status. Valid values are [open,closed]")
 66	flags.StringSliceVarP(&options.authorQuery, "author", "a", nil,
 67		"Filter by author")
 68	flags.StringSliceVarP(&options.metadataQuery, "metadata", "m", nil,
 69		"Filter by metadata. Example: github-url=URL")
 70	flags.StringSliceVarP(&options.participantQuery, "participant", "p", nil,
 71		"Filter by participant")
 72	flags.StringSliceVarP(&options.actorQuery, "actor", "A", nil,
 73		"Filter by actor")
 74	flags.StringSliceVarP(&options.labelQuery, "label", "l", nil,
 75		"Filter by label")
 76	flags.StringSliceVarP(&options.titleQuery, "title", "t", nil,
 77		"Filter by title")
 78	flags.StringSliceVarP(&options.noQuery, "no", "n", nil,
 79		"Filter by absence of something. Valid values are [label]")
 80	flags.StringVarP(&options.sortBy, "by", "b", "creation",
 81		"Sort the results by a characteristic. Valid values are [id,creation,edit]")
 82	flags.StringVarP(&options.sortDirection, "direction", "d", "asc",
 83		"Select the sorting direction. Valid values are [asc,desc]")
 84	flags.StringVarP(&options.outputFormat, "format", "f", "default",
 85		"Select the output formatting style. Valid values are [default,plain,json,org-mode]")
 86
 87	return cmd
 88}
 89
 90func runLs(env *Env, opts lsOptions, args []string) error {
 91	var q *query.Query
 92	var err error
 93
 94	if len(args) >= 1 {
 95		// either the shell or cobra remove the quotes, we need them back for the parsing
 96		for i, arg := range args {
 97			if strings.Contains(arg, " ") {
 98				args[i] = fmt.Sprintf("\"%s\"", arg)
 99			}
100		}
101		assembled := strings.Join(args, " ")
102		q, err = query.Parse(assembled)
103		if err != nil {
104			return err
105		}
106	} else {
107		q = query.NewQuery()
108	}
109
110	err = completeQuery(q, opts)
111	if err != nil {
112		return err
113	}
114
115	allIds, err := env.backend.QueryBugs(q)
116	if err != nil {
117		return err
118	}
119
120	bugExcerpt := make([]*cache.BugExcerpt, len(allIds))
121	for i, id := range allIds {
122		b, err := env.backend.ResolveBugExcerpt(id)
123		if err != nil {
124			return err
125		}
126		bugExcerpt[i] = b
127	}
128
129	switch opts.outputFormat {
130	case "org-mode":
131		return lsOrgmodeFormatter(env, bugExcerpt)
132	case "plain":
133		return lsPlainFormatter(env, bugExcerpt)
134	case "json":
135		return lsJsonFormatter(env, bugExcerpt)
136	case "default":
137		return lsDefaultFormatter(env, bugExcerpt)
138	default:
139		return fmt.Errorf("unknown format %s", opts.outputFormat)
140	}
141}
142
143type JSONBugExcerpt struct {
144	Id         string   `json:"id"`
145	HumanId    string   `json:"human_id"`
146	CreateTime JSONTime `json:"create_time"`
147	EditTime   JSONTime `json:"edit_time"`
148
149	Status       string         `json:"status"`
150	Labels       []bug.Label    `json:"labels"`
151	Title        string         `json:"title"`
152	Actors       []JSONIdentity `json:"actors"`
153	Participants []JSONIdentity `json:"participants"`
154	Author       JSONIdentity   `json:"author"`
155
156	Comments int               `json:"comments"`
157	Metadata map[string]string `json:"metadata"`
158}
159
160func lsJsonFormatter(env *Env, bugExcerpts []*cache.BugExcerpt) error {
161	jsonBugs := make([]JSONBugExcerpt, len(bugExcerpts))
162	for i, b := range bugExcerpts {
163		jsonBug := JSONBugExcerpt{
164			Id:         b.Id.String(),
165			HumanId:    b.Id.Human(),
166			CreateTime: NewJSONTime(b.CreateTime(), b.CreateLamportTime),
167			EditTime:   NewJSONTime(b.EditTime(), b.EditLamportTime),
168			Status:     b.Status.String(),
169			Labels:     b.Labels,
170			Title:      b.Title,
171			Comments:   b.LenComments,
172			Metadata:   b.CreateMetadata,
173		}
174
175		author, err := env.backend.ResolveIdentityExcerpt(b.AuthorId)
176		if err != nil {
177			return err
178		}
179		jsonBug.Author = NewJSONIdentityFromExcerpt(author)
180
181		jsonBug.Actors = make([]JSONIdentity, len(b.Actors))
182		for i, element := range b.Actors {
183			actor, err := env.backend.ResolveIdentityExcerpt(element)
184			if err != nil {
185				return err
186			}
187			jsonBug.Actors[i] = NewJSONIdentityFromExcerpt(actor)
188		}
189
190		jsonBug.Participants = make([]JSONIdentity, len(b.Participants))
191		for i, element := range b.Participants {
192			participant, err := env.backend.ResolveIdentityExcerpt(element)
193			if err != nil {
194				return err
195			}
196			jsonBug.Participants[i] = NewJSONIdentityFromExcerpt(participant)
197		}
198
199		jsonBugs[i] = jsonBug
200	}
201	jsonObject, _ := json.MarshalIndent(jsonBugs, "", "    ")
202	env.out.Printf("%s\n", jsonObject)
203	return nil
204}
205
206func lsDefaultFormatter(env *Env, bugExcerpts []*cache.BugExcerpt) error {
207	for _, b := range bugExcerpts {
208		author, err := env.backend.ResolveIdentityExcerpt(b.AuthorId)
209		if err != nil {
210			return err
211		}
212
213		var labelsTxt strings.Builder
214		for _, l := range b.Labels {
215			lc256 := l.Color().Term256()
216			labelsTxt.WriteString(lc256.Escape())
217			labelsTxt.WriteString(" ◼")
218			labelsTxt.WriteString(lc256.Unescape())
219		}
220
221		// truncate + pad if needed
222		labelsFmt := text.TruncateMax(labelsTxt.String(), 10)
223		titleFmt := text.LeftPadMaxLine(strings.TrimSpace(b.Title), 50-text.Len(labelsFmt), 0)
224		authorFmt := text.LeftPadMaxLine(author.DisplayName(), 15, 0)
225
226		comments := fmt.Sprintf("%3d 💬", b.LenComments-1)
227		if b.LenComments-1 <= 0 {
228			comments = ""
229		}
230		if b.LenComments-1 > 999 {
231			comments = "  ∞ 💬"
232		}
233
234		env.out.Printf("%s %s\t%s\t%s\t%s\n",
235			colors.Cyan(b.Id.Human()),
236			colors.Yellow(b.Status),
237			titleFmt+labelsFmt,
238			colors.Magenta(authorFmt),
239			comments,
240		)
241	}
242	return nil
243}
244
245func lsPlainFormatter(env *Env, bugExcerpts []*cache.BugExcerpt) error {
246	for _, b := range bugExcerpts {
247		env.out.Printf("%s [%s] %s\n", b.Id.Human(), b.Status, strings.TrimSpace(b.Title))
248	}
249	return nil
250}
251
252func lsOrgmodeFormatter(env *Env, bugExcerpts []*cache.BugExcerpt) error {
253	// see https://orgmode.org/manual/Tags.html
254	orgTagRe := regexp.MustCompile("[^[:alpha:]_@]")
255	formatTag := func(l bug.Label) string {
256		return orgTagRe.ReplaceAllString(l.String(), "_")
257	}
258
259	formatTime := func(time time.Time) string {
260		return time.Format("[2006-01-02 Mon 15:05]")
261	}
262
263	env.out.Println("#+TODO: OPEN | CLOSED")
264
265	for _, b := range bugExcerpts {
266		status := strings.ToUpper(b.Status.String())
267
268		var title string
269		if link, ok := b.CreateMetadata["github-url"]; ok {
270			title = fmt.Sprintf("[[%s][%s]]", link, b.Title)
271		} else {
272			title = b.Title
273		}
274
275		author, err := env.backend.ResolveIdentityExcerpt(b.AuthorId)
276		if err != nil {
277			return err
278		}
279
280		var labels strings.Builder
281		labels.WriteString(":")
282		for i, l := range b.Labels {
283			if i > 0 {
284				labels.WriteString(":")
285			}
286			labels.WriteString(formatTag(l))
287		}
288		labels.WriteString(":")
289
290		env.out.Printf("* %-6s %s %s %s: %s %s\n",
291			status,
292			b.Id.Human(),
293			formatTime(b.CreateTime()),
294			author.DisplayName(),
295			title,
296			labels.String(),
297		)
298
299		env.out.Printf("** Last Edited: %s\n", formatTime(b.EditTime()))
300
301		env.out.Printf("** Actors:\n")
302		for _, element := range b.Actors {
303			actor, err := env.backend.ResolveIdentityExcerpt(element)
304			if err != nil {
305				return err
306			}
307
308			env.out.Printf(": %s %s\n",
309				actor.Id.Human(),
310				actor.DisplayName(),
311			)
312		}
313
314		env.out.Printf("** Participants:\n")
315		for _, element := range b.Participants {
316			participant, err := env.backend.ResolveIdentityExcerpt(element)
317			if err != nil {
318				return err
319			}
320
321			env.out.Printf(": %s %s\n",
322				participant.Id.Human(),
323				participant.DisplayName(),
324			)
325		}
326	}
327
328	return nil
329}
330
331// Finish the command flags transformation into the query.Query
332func completeQuery(q *query.Query, opts lsOptions) error {
333	for _, str := range opts.statusQuery {
334		status, err := bug.StatusFromString(str)
335		if err != nil {
336			return err
337		}
338		q.Status = append(q.Status, status)
339	}
340
341	q.Author = append(q.Author, opts.authorQuery...)
342	for _, str := range opts.metadataQuery {
343		tokens := strings.Split(str, "=")
344		if len(tokens) < 2 {
345			return fmt.Errorf("no \"=\" in key=value metadata markup")
346		}
347		var pair query.StringPair
348		pair.Key = tokens[0]
349		pair.Value = tokens[1]
350		q.Metadata = append(q.Metadata, pair)
351	}
352	q.Participant = append(q.Participant, opts.participantQuery...)
353	q.Actor = append(q.Actor, opts.actorQuery...)
354	q.Label = append(q.Label, opts.labelQuery...)
355	q.Title = append(q.Title, opts.titleQuery...)
356
357	for _, no := range opts.noQuery {
358		switch no {
359		case "label":
360			q.NoLabel = true
361		default:
362			return fmt.Errorf("unknown \"no\" filter %s", no)
363		}
364	}
365
366	switch opts.sortBy {
367	case "id":
368		q.OrderBy = query.OrderById
369	case "creation":
370		q.OrderBy = query.OrderByCreation
371	case "edit":
372		q.OrderBy = query.OrderByEdit
373	default:
374		return fmt.Errorf("unknown sort flag %s", opts.sortBy)
375	}
376
377	switch opts.sortDirection {
378	case "asc":
379		q.OrderDirection = query.OrderAscending
380	case "desc":
381		q.OrderDirection = query.OrderDescending
382	default:
383		return fmt.Errorf("unknown sort direction %s", opts.sortDirection)
384	}
385
386	return nil
387}