aboutsummaryrefslogtreecommitdiffstats
path: root/commands/ls.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-09-10 18:16:16 +0200
committerMichael Muré <batolettre@gmail.com>2018-09-10 18:18:00 +0200
commit9bb980e9de1ec3764069ae70baf0c2458e7c35a4 (patch)
treeae6daa547ebcd37fcb93419da0e54d2187f0c667 /commands/ls.go
parentfd21de5632fda2bbc030d34c28e9dfc1403d2497 (diff)
downloadgit-bug-9bb980e9de1ec3764069ae70baf0c2458e7c35a4.tar.gz
ls: support expressing a query with flags as well
Diffstat (limited to 'commands/ls.go')
-rw-r--r--commands/ls.go98
1 files changed, 95 insertions, 3 deletions
diff --git a/commands/ls.go b/commands/ls.go
index 79124f7f..4ff4b315 100644
--- a/commands/ls.go
+++ b/commands/ls.go
@@ -9,6 +9,15 @@ import (
"github.com/spf13/cobra"
)
+var (
+ lsStatusQuery []string
+ lsAuthorQuery []string
+ lsLabelQuery []string
+ lsNoQuery []string
+ lsSortBy string
+ lsSortDirection string
+)
+
func runLsBug(cmd *cobra.Command, args []string) error {
backend, err := cache.NewRepoCache(repo)
if err != nil {
@@ -23,6 +32,11 @@ func runLsBug(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
+ } else {
+ query, err = lsQueryFromFlags()
+ if err != nil {
+ return err
+ }
}
allIds := backend.QueryBugs(query)
@@ -58,12 +72,90 @@ func runLsBug(cmd *cobra.Command, args []string) error {
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 _, author := range lsAuthorQuery {
+ f := cache.AuthorFilter(author)
+ query.Author = append(query.Author, 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: "Display a summary of all bugs",
- RunE: runLsBug,
+ 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
+`,
+ 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(&lsAuthorQuery, "author", "a", nil,
+ "Filter by author")
+ lsCmd.Flags().StringSliceVarP(&lsLabelQuery, "label", "l", nil,
+ "Filter by label")
+ 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]")
}