ls.go

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