git-bug/commands/ls.go

193 lines
4.7 KiB
Go
Raw Normal View History

2018-07-14 23:19:05 +03:00
package commands
import (
"fmt"
"strings"
2018-08-13 19:32:11 +03:00
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/util/colors"
"github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
2018-07-14 23:19:05 +03:00
)
var (
lsStatusQuery []string
lsAuthorQuery []string
lsParticipantQuery []string
lsLabelQuery []string
lsTitleQuery []string
lsActorQuery []string
lsNoQuery []string
lsSortBy string
lsSortDirection string
)
func runLsBug(cmd *cobra.Command, args []string) error {
backend, err := cache.NewRepoCache(repo)
if err != nil {
return err
}
defer backend.Close()
interrupt.RegisterCleaner(backend.Close)
var query *cache.Query
if len(args) >= 1 {
query, err = cache.ParseQuery(strings.Join(args, " "))
if err != nil {
return err
}
} else {
query, err = lsQueryFromFlags()
if err != nil {
return err
}
}
allIds := backend.QueryBugs(query)
2018-07-14 23:19:05 +03:00
for _, id := range allIds {
b, err := backend.ResolveBugExcerpt(id)
if err != nil {
return err
2018-07-14 23:19:05 +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()
}
2018-07-17 21:23:14 +03:00
// truncate + pad if needed
titleFmt := fmt.Sprintf("%-50.50s", b.Title)
authorFmt := fmt.Sprintf("%-15.15s", name)
2018-07-17 21:23:14 +03:00
fmt.Printf("%s %s\t%s\t%s\tC:%d L:%d\n",
colors.Cyan(b.HumanId()),
colors.Yellow(b.Status),
2018-07-17 21:23:14 +03:00
titleFmt,
colors.Magenta(authorFmt),
b.LenComments,
len(b.Labels),
2018-07-17 21:23:14 +03:00
)
2018-07-14 23:19:05 +03:00
}
return nil
}
// Transform the command flags into a query
func lsQueryFromFlags() (*cache.Query, error) {
query := cache.NewQuery()
for _, status := range lsStatusQuery {
f, err := cache.StatusFilter(status)
if err != nil {
return nil, err
}
query.Status = append(query.Status, f)
}
for _, title := range lsTitleQuery {
f := cache.TitleFilter(title)
query.Title = append(query.Title, f)
}
for _, author := range lsAuthorQuery {
f := cache.AuthorFilter(author)
query.Author = append(query.Author, f)
}
for _, actor := range lsActorQuery {
f := cache.ActorFilter(actor)
query.Actor = append(query.Actor, f)
}
for _, participant := range lsParticipantQuery {
f := cache.ParticipantFilter(participant)
query.Participant = append(query.Participant, f)
}
for _, label := range lsLabelQuery {
f := cache.LabelFilter(label)
query.Label = append(query.Label, f)
}
for _, no := range lsNoQuery {
switch no {
case "label":
query.NoFilters = append(query.NoFilters, cache.NoLabelFilter())
default:
return nil, fmt.Errorf("unknown \"no\" filter %s", no)
}
}
switch lsSortBy {
case "id":
query.OrderBy = cache.OrderById
case "creation":
query.OrderBy = cache.OrderByCreation
case "edit":
query.OrderBy = cache.OrderByEdit
default:
return nil, fmt.Errorf("unknown sort flag %s", lsSortBy)
}
switch lsSortDirection {
case "asc":
query.OrderDirection = cache.OrderAscending
case "desc":
query.OrderDirection = cache.OrderDescending
default:
return nil, fmt.Errorf("unknown sort direction %s", lsSortDirection)
}
return query, 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() {
2018-07-20 16:46:14 +03:00
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(&lsAuthorQuery, "author", "a", nil,
"Filter by author")
lsCmd.Flags().StringSliceVarP(&lsParticipantQuery, "participant", "p", nil,
"Filter by participant")
lsCmd.Flags().StringSliceVarP(&lsActorQuery, "actor", "A", nil,
"Filter by actor")
lsCmd.Flags().StringSliceVarP(&lsLabelQuery, "label", "l", nil,
"Filter by label")
lsCmd.Flags().StringSliceVarP(&lsTitleQuery, "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]")
2018-07-14 23:19:05 +03:00
}