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