2018-07-14 23:19:05 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-09-19 22:50:03 +03:00
|
|
|
"strings"
|
2018-08-13 19:32:11 +03:00
|
|
|
|
2019-11-03 16:00:35 +03:00
|
|
|
text "github.com/MichaelMure/go-term-text"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2020-03-14 18:47:38 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bug"
|
2018-09-02 16:46:43 +03:00
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
2020-03-14 18:47:38 +03:00
|
|
|
"github.com/MichaelMure/git-bug/query"
|
2018-09-11 23:04:16 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/colors"
|
2018-10-25 00:36:39 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/interrupt"
|
2018-07-14 23:19:05 +03:00
|
|
|
)
|
|
|
|
|
2018-09-10 19:16:16 +03:00
|
|
|
var (
|
2020-03-22 15:53:34 +03:00
|
|
|
lsQuery query.Query
|
|
|
|
|
|
|
|
lsStatusQuery []string
|
|
|
|
lsNoQuery []string
|
|
|
|
lsSortBy string
|
|
|
|
lsSortDirection string
|
2018-09-10 19:16:16 +03:00
|
|
|
)
|
|
|
|
|
2018-07-19 13:30:25 +03:00
|
|
|
func runLsBug(cmd *cobra.Command, args []string) error {
|
2018-09-02 16:46:43 +03:00
|
|
|
backend, err := cache.NewRepoCache(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-04 19:18:24 +03:00
|
|
|
defer backend.Close()
|
2018-10-25 00:36:39 +03:00
|
|
|
interrupt.RegisterCleaner(backend.Close)
|
2018-08-31 14:18:03 +03:00
|
|
|
|
2020-03-22 15:53:34 +03:00
|
|
|
var q *query.Query
|
2018-09-09 21:22:46 +03:00
|
|
|
if len(args) >= 1 {
|
2020-03-14 18:47:38 +03:00
|
|
|
q, err = query.Parse(strings.Join(args, " "))
|
2018-09-09 21:22:46 +03:00
|
|
|
|
2018-09-10 19:16:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2020-03-22 15:53:34 +03:00
|
|
|
err = completeQuery()
|
2018-09-09 21:22:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-22 15:53:34 +03:00
|
|
|
q = &lsQuery
|
2018-09-09 21:22:46 +03:00
|
|
|
}
|
|
|
|
|
2020-03-14 18:47:38 +03:00
|
|
|
allIds := backend.QueryBugs(q)
|
2018-07-14 23:19:05 +03:00
|
|
|
|
2018-09-02 16:46:43 +03:00
|
|
|
for _, id := range allIds {
|
2019-02-28 00:19:35 +03:00
|
|
|
b, err := backend.ResolveBugExcerpt(id)
|
2018-09-02 16:46:43 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-07-14 23:19:05 +03:00
|
|
|
}
|
|
|
|
|
2019-04-11 21:04:44 +03:00
|
|
|
var name string
|
|
|
|
if b.AuthorId != "" {
|
|
|
|
author, err := backend.ResolveIdentityExcerpt(b.AuthorId)
|
|
|
|
if err != nil {
|
|
|
|
name = "<missing author data>"
|
|
|
|
} else {
|
|
|
|
name = author.DisplayName()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
name = b.LegacyAuthor.DisplayName()
|
|
|
|
}
|
|
|
|
|
2019-11-03 16:00:35 +03:00
|
|
|
var labelsTxt strings.Builder
|
2019-10-28 14:43:24 +03:00
|
|
|
for _, l := range b.Labels {
|
2019-11-03 16:00:35 +03:00
|
|
|
lc256 := l.Color().Term256()
|
|
|
|
labelsTxt.WriteString(lc256.Escape())
|
|
|
|
labelsTxt.WriteString(" ◼")
|
|
|
|
labelsTxt.WriteString(lc256.Unescape())
|
2019-10-28 14:43:24 +03:00
|
|
|
}
|
|
|
|
|
2018-07-17 21:23:14 +03:00
|
|
|
// truncate + pad if needed
|
2019-11-03 16:00:35 +03:00
|
|
|
labelsFmt := text.TruncateMax(labelsTxt.String(), 10)
|
|
|
|
titleFmt := text.LeftPadMaxLine(b.Title, 50-text.Len(labelsFmt), 0)
|
2019-05-14 18:01:49 +03:00
|
|
|
authorFmt := text.LeftPadMaxLine(name, 15, 0)
|
2018-07-17 21:23:14 +03:00
|
|
|
|
2019-10-28 14:43:24 +03:00
|
|
|
comments := fmt.Sprintf("%4d 💬", b.LenComments)
|
|
|
|
if b.LenComments > 9999 {
|
|
|
|
comments = " ∞ 💬"
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%s %s\t%s\t%s\t%s\n",
|
2019-08-12 17:12:14 +03:00
|
|
|
colors.Cyan(b.Id.Human()),
|
2019-02-28 00:19:35 +03:00
|
|
|
colors.Yellow(b.Status),
|
2019-11-03 16:00:35 +03:00
|
|
|
titleFmt+labelsFmt,
|
2018-09-11 23:04:16 +03:00
|
|
|
colors.Magenta(authorFmt),
|
2019-10-28 14:43:24 +03:00
|
|
|
comments,
|
2018-07-17 21:23:14 +03:00
|
|
|
)
|
2018-07-14 23:19:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-22 15:53:34 +03:00
|
|
|
// Finish the command flags transformation into the query.Query
|
|
|
|
func completeQuery() error {
|
2020-03-14 18:47:38 +03:00
|
|
|
for _, str := range lsStatusQuery {
|
|
|
|
status, err := bug.StatusFromString(str)
|
2018-09-10 19:16:16 +03:00
|
|
|
if err != nil {
|
2020-03-22 15:53:34 +03:00
|
|
|
return err
|
2018-09-10 19:16:16 +03:00
|
|
|
}
|
2020-03-22 15:53:34 +03:00
|
|
|
lsQuery.Status = append(lsQuery.Status, status)
|
2018-09-10 19:16:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, no := range lsNoQuery {
|
|
|
|
switch no {
|
|
|
|
case "label":
|
2020-03-22 15:53:34 +03:00
|
|
|
lsQuery.NoLabel = true
|
2018-09-10 19:16:16 +03:00
|
|
|
default:
|
2020-03-22 15:53:34 +03:00
|
|
|
return fmt.Errorf("unknown \"no\" filter %s", no)
|
2018-09-10 19:16:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch lsSortBy {
|
|
|
|
case "id":
|
2020-03-22 15:53:34 +03:00
|
|
|
lsQuery.OrderBy = query.OrderById
|
2018-09-10 19:16:16 +03:00
|
|
|
case "creation":
|
2020-03-22 15:53:34 +03:00
|
|
|
lsQuery.OrderBy = query.OrderByCreation
|
2018-09-10 19:16:16 +03:00
|
|
|
case "edit":
|
2020-03-22 15:53:34 +03:00
|
|
|
lsQuery.OrderBy = query.OrderByEdit
|
2018-09-10 19:16:16 +03:00
|
|
|
default:
|
2020-03-22 15:53:34 +03:00
|
|
|
return fmt.Errorf("unknown sort flag %s", lsSortBy)
|
2018-09-10 19:16:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch lsSortDirection {
|
|
|
|
case "asc":
|
2020-03-22 15:53:34 +03:00
|
|
|
lsQuery.OrderDirection = query.OrderAscending
|
2018-09-10 19:16:16 +03:00
|
|
|
case "desc":
|
2020-03-22 15:53:34 +03:00
|
|
|
lsQuery.OrderDirection = query.OrderDescending
|
2018-09-10 19:16:16 +03:00
|
|
|
default:
|
2020-03-22 15:53:34 +03:00
|
|
|
return fmt.Errorf("unknown sort direction %s", lsSortDirection)
|
2018-09-10 19:16:16 +03:00
|
|
|
}
|
|
|
|
|
2020-03-22 15:53:34 +03:00
|
|
|
return nil
|
2018-09-10 19:16:16 +03:00
|
|
|
}
|
|
|
|
|
2018-07-19 13:30:25 +03:00
|
|
|
var lsCmd = &cobra.Command{
|
2018-09-10 19:16:16 +03:00
|
|
|
Use: "ls [<query>]",
|
2019-02-24 16:46:08 +03:00
|
|
|
Short: "List bugs.",
|
2018-09-10 19:16:16 +03:00
|
|
|
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:
|
2018-09-19 22:50:03 +03:00
|
|
|
git bug ls status:open sort:edit-desc
|
2018-09-10 19:16:16 +03:00
|
|
|
|
|
|
|
List closed bugs sorted by creation with flags:
|
|
|
|
git bug ls --status closed --by creation
|
|
|
|
`,
|
2018-10-17 21:38:10 +03:00
|
|
|
PreRunE: loadRepo,
|
|
|
|
RunE: runLsBug,
|
2018-07-19 13:30:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-07-20 16:46:14 +03:00
|
|
|
RootCmd.AddCommand(lsCmd)
|
2018-09-10 19:16:16 +03:00
|
|
|
|
|
|
|
lsCmd.Flags().SortFlags = false
|
|
|
|
|
|
|
|
lsCmd.Flags().StringSliceVarP(&lsStatusQuery, "status", "s", nil,
|
|
|
|
"Filter by status. Valid values are [open,closed]")
|
2020-03-22 15:53:34 +03:00
|
|
|
lsCmd.Flags().StringSliceVarP(&lsQuery.Author, "author", "a", nil,
|
2018-09-10 19:16:16 +03:00
|
|
|
"Filter by author")
|
2020-03-22 15:53:34 +03:00
|
|
|
lsCmd.Flags().StringSliceVarP(&lsQuery.Participant, "participant", "p", nil,
|
2019-04-05 17:42:45 +03:00
|
|
|
"Filter by participant")
|
2020-03-22 15:53:34 +03:00
|
|
|
lsCmd.Flags().StringSliceVarP(&lsQuery.Actor, "actor", "A", nil,
|
2019-04-05 17:42:45 +03:00
|
|
|
"Filter by actor")
|
2020-03-22 15:53:34 +03:00
|
|
|
lsCmd.Flags().StringSliceVarP(&lsQuery.Label, "label", "l", nil,
|
2018-09-10 19:16:16 +03:00
|
|
|
"Filter by label")
|
2020-03-22 15:53:34 +03:00
|
|
|
lsCmd.Flags().StringSliceVarP(&lsQuery.Title, "title", "t", nil,
|
2019-02-28 00:19:35 +03:00
|
|
|
"Filter by title")
|
2018-09-10 19:16:16 +03:00
|
|
|
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]")
|
2018-07-14 23:19:05 +03:00
|
|
|
}
|