aboutsummaryrefslogtreecommitdiffstats
path: root/commands/ls.go
blob: 68cb0cfc7f13a80d49281de4a5f1c7bd430c7eae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package commands

import (
	"encoding/json"
	"fmt"
	"strings"
	"time"

	text "github.com/MichaelMure/go-term-text"
	"github.com/spf13/cobra"

	"github.com/MichaelMure/git-bug/bug"
	"github.com/MichaelMure/git-bug/cache"
	"github.com/MichaelMure/git-bug/query"
	"github.com/MichaelMure/git-bug/util/colors"
	"github.com/MichaelMure/git-bug/util/interrupt"
)

var (
	lsQuery query.Query

	lsStatusQuery   []string
	lsNoQuery       []string
	lsSortBy        string
	lsSortDirection string
	lsOutputFormat  string
)

func runLsBug(_ *cobra.Command, args []string) error {
	backend, err := cache.NewRepoCache(repo)
	if err != nil {
		return err
	}
	defer backend.Close()
	interrupt.RegisterCleaner(backend.Close)

	var q *query.Query
	if len(args) >= 1 {
		q, err = query.Parse(strings.Join(args, " "))

		if err != nil {
			return err
		}
	} else {
		err = completeQuery()
		if err != nil {
			return err
		}
		q = &lsQuery
	}

	allIds := backend.QueryBugs(q)

	bugExcerpt := make([]*cache.BugExcerpt, len(allIds))
	for i, id := range allIds {
		b, err := backend.ResolveBugExcerpt(id)
		if err != nil {
			return err
		}
		bugExcerpt[i] = b
	}

	switch lsOutputFormat {
	case "plain":
		return lsPlainFormatter(backend, bugExcerpt)
	case "json":
		return lsJsonFormatter(backend, bugExcerpt)
	case "default":
		return lsDefaultFormatter(backend, bugExcerpt)
	default:
		return fmt.Errorf("unknown format %s", lsOutputFormat)
	}
}

type JSONBug struct {
	Id           string    `json:"id"`
	HumanId      string    `json:"human_id"`
	CreationTime time.Time `json:"creation_time"`
	LastEdited   time.Time `json:"last_edited"`

	Status       string         `json:"status"`
	Labels       []bug.Label    `json:"labels"`
	Title        string         `json:"title"`
	Actors       []JSONIdentity `json:"actors"`
	Participants []JSONIdentity `json:"participants"`
	Author       JSONIdentity   `json:"author"`

	Comments int               `json:"comments"`
	Metadata map[string]string `json:"metadata"`
}

type JSONIdentity struct {
	Id      string `json:"id"`
	HumanId string `json:"human_id"`
	Name    string `json:"name"`
	Login   string `json:"login"`
}

func lsJsonFormatter(backend *cache.RepoCache, bugExcerpts []*cache.BugExcerpt) error {
	jsonBugs := make([]JSONBug, len(bugExcerpts))
	for i, b := range bugExcerpts {
		jsonBug := JSONBug{
			b.Id.String(),
			b.Id.Human(),
			time.Unix(b.CreateUnixTime, 0),
			time.Unix(b.EditUnixTime, 0),
			b.Status.String(),
			b.Labels,
			b.Title,
			[]JSONIdentity{},
			[]JSONIdentity{},
			JSONIdentity{},
			b.LenComments,
			b.CreateMetadata,
		}

		if b.AuthorId != "" {
			author, err := backend.ResolveIdentityExcerpt(b.AuthorId)
			if err != nil {
				return err
			}

			jsonBug.Author.Name = author.DisplayName()
			jsonBug.Author.Login = author.Login
			jsonBug.Author.Id = author.Id.String()
			jsonBug.Author.HumanId = author.Id.Human()
		} else {
			jsonBug.Author.Name = b.LegacyAuthor.DisplayName()
			jsonBug.Author.Login = b.LegacyAuthor.Login
		}

		for _, element := range b.Actors {
			actor, err := backend.ResolveIdentityExcerpt(element)
			if err != nil {
				return err
			}

			jsonBug.Actors = append(jsonBug.Actors, JSONIdentity{
				actor.Id.String(),
				actor.Id.Human(),
				actor.Name,
				actor.Login,
			})
		}

		for _, element := range b.Participants {
			participant, err := backend.ResolveIdentityExcerpt(element)
			if err != nil {
				return err
			}
			jsonBug.Participants = append(jsonBug.Participants, JSONIdentity{
				participant.Id.String(),
				participant.Id.Human(),
				participant.DisplayName(),
				participant.Login,
			})
		}

		jsonBugs[i] = jsonBug
	}
	jsonObject, _ := json.MarshalIndent(jsonBugs, "", "    ")
	fmt.Printf("%s\n", jsonObject)
	return nil
}

func lsDefaultFormatter(backend *cache.RepoCache, bugExcerpts []*cache.BugExcerpt) error {
	for _, b := range bugExcerpts {
		var name string
		if b.AuthorId != "" {
			author, err := backend.ResolveIdentityExcerpt(b.AuthorId)
			if err != nil {
				return err
			}
			name = author.DisplayName()
		} else {
			name = b.LegacyAuthor.DisplayName()
		}

		var labelsTxt strings.Builder
		for _, l := range b.Labels {
			lc256 := l.Color().Term256()
			labelsTxt.WriteString(lc256.Escape())
			labelsTxt.WriteString(" ◼")
			labelsTxt.WriteString(lc256.Unescape())
		}

		// truncate + pad if needed
		labelsFmt := text.TruncateMax(labelsTxt.String(), 10)
		titleFmt := text.LeftPadMaxLine(b.Title, 50-text.Len(labelsFmt), 0)
		authorFmt := text.LeftPadMaxLine(name, 15, 0)

		comments := fmt.Sprintf("%4d 💬", b.LenComments)
		if b.LenComments > 9999 {
			comments = "    ∞ 💬"
		}

		fmt.Printf("%s %s\t%s\t%s\t%s\n",
			colors.Cyan(b.Id.Human()),
			colors.Yellow(b.Status),
			titleFmt+labelsFmt,
			colors.Magenta(authorFmt),
			comments,
		)
	}
	return nil
}

func lsPlainFormatter(_ *cache.RepoCache, bugExcerpts []*cache.BugExcerpt) error {
	for _, b := range bugExcerpts {
		fmt.Printf("[%s] %s\n", b.Status, b.Title)
	}
	return nil
}

// Finish the command flags transformation into the query.Query
func completeQuery() error {
	for _, str := range lsStatusQuery {
		status, err := bug.StatusFromString(str)
		if err != nil {
			return err
		}
		lsQuery.Status = append(lsQuery.Status, status)
	}

	for _, no := range lsNoQuery {
		switch no {
		case "label":
			lsQuery.NoLabel = true
		default:
			return fmt.Errorf("unknown \"no\" filter %s", no)
		}
	}

	switch lsSortBy {
	case "id":
		lsQuery.OrderBy = query.OrderById
	case "creation":
		lsQuery.OrderBy = query.OrderByCreation
	case "edit":
		lsQuery.OrderBy = query.OrderByEdit
	default:
		return fmt.Errorf("unknown sort flag %s", lsSortBy)
	}

	switch lsSortDirection {
	case "asc":
		lsQuery.OrderDirection = query.OrderAscending
	case "desc":
		lsQuery.OrderDirection = query.OrderDescending
	default:
		return fmt.Errorf("unknown sort direction %s", lsSortDirection)
	}

	return nil
}

var lsCmd = &cobra.Command{
	Use:   "ls [<query>]",
	Short: "List bugs.",
	Long: `Display a summary of each bugs.

You 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.`,
	Example: `List open bugs sorted by last edition with a query:
git bug ls status:open sort:edit-desc

List closed bugs sorted by creation with flags:
git bug ls --status closed --by creation
`,
	PreRunE: loadRepo,
	RunE:    runLsBug,
}

func init() {
	RootCmd.AddCommand(lsCmd)

	lsCmd.Flags().SortFlags = false

	lsCmd.Flags().StringSliceVarP(&lsStatusQuery, "status", "s", nil,
		"Filter by status. Valid values are [open,closed]")
	lsCmd.Flags().StringSliceVarP(&lsQuery.Author, "author", "a", nil,
		"Filter by author")
	lsCmd.Flags().StringSliceVarP(&lsQuery.Participant, "participant", "p", nil,
		"Filter by participant")
	lsCmd.Flags().StringSliceVarP(&lsQuery.Actor, "actor", "A", nil,
		"Filter by actor")
	lsCmd.Flags().StringSliceVarP(&lsQuery.Label, "label", "l", nil,
		"Filter by label")
	lsCmd.Flags().StringSliceVarP(&lsQuery.Title, "title", "t", nil,
		"Filter by title")
	lsCmd.Flags().StringSliceVarP(&lsNoQuery, "no", "n", nil,
		"Filter by absence of something. Valid values are [label]")
	lsCmd.Flags().StringVarP(&lsSortBy, "by", "b", "creation",
		"Sort the results by a characteristic. Valid values are [id,creation,edit]")
	lsCmd.Flags().StringVarP(&lsSortDirection, "direction", "d", "asc",
		"Select the sorting direction. Valid values are [asc,desc]")
	lsCmd.Flags().StringVarP(&lsOutputFormat, "format", "f", "default",
		"Select the output formatting style. Valid values are [default, plain(text), json]")
}