1package commands
2
3import (
4 "fmt"
5 "strings"
6
7 "github.com/MichaelMure/git-bug/cache"
8 "github.com/MichaelMure/git-bug/util/colors"
9 "github.com/MichaelMure/git-bug/util/interrupt"
10 "github.com/spf13/cobra"
11)
12
13var (
14 lsStatusQuery []string
15 lsAuthorQuery []string
16 lsTitleQuery []string
17 lsLabelQuery []string
18 lsNoQuery []string
19 lsSortBy string
20 lsSortDirection string
21)
22
23func runLsBug(cmd *cobra.Command, args []string) error {
24 backend, err := cache.NewRepoCache(repo)
25 if err != nil {
26 return err
27 }
28 defer backend.Close()
29 interrupt.RegisterCleaner(backend.Close)
30
31 var query *cache.Query
32 if len(args) >= 1 {
33 query, err = cache.ParseQuery(strings.Join(args, " "))
34
35 if err != nil {
36 return err
37 }
38 } else {
39 query, err = lsQueryFromFlags()
40 if err != nil {
41 return err
42 }
43 }
44
45 allIds := backend.QueryBugs(query)
46
47 for _, id := range allIds {
48 b, err := backend.ResolveBugExcerpt(id)
49 if err != nil {
50 return err
51 }
52
53 // truncate + pad if needed
54 titleFmt := fmt.Sprintf("%-50.50s", b.Title)
55 authorFmt := fmt.Sprintf("%-15.15s", b.Author.Name)
56
57 fmt.Printf("%s %s\t%s\t%s\tC:%d L:%d\n",
58<<<<<<< HEAD
59 colors.Cyan(b.HumanId()),
60=======
61 colors.Cyan(b.Id),
62>>>>>>> Made requested changes
63 colors.Yellow(b.Status),
64 titleFmt,
65 colors.Magenta(authorFmt),
66 b.LenComments,
67 len(b.Labels),
68 )
69 }
70
71 return nil
72}
73
74// Transform the command flags into a query
75func lsQueryFromFlags() (*cache.Query, error) {
76 query := cache.NewQuery()
77
78 for _, status := range lsStatusQuery {
79 f, err := cache.StatusFilter(status)
80 if err != nil {
81 return nil, err
82 }
83 query.Status = append(query.Status, f)
84 }
85
86 for _, title := range lsTitleQuery {
87 f := cache.TitleFilter(title)
88 query.Title = append(query.Title, f)
89 }
90
91 for _, author := range lsAuthorQuery {
92 f := cache.AuthorFilter(author)
93 query.Author = append(query.Author, f)
94 }
95
96 for _, label := range lsLabelQuery {
97 f := cache.LabelFilter(label)
98 query.Label = append(query.Label, f)
99 }
100
101 for _, no := range lsNoQuery {
102 switch no {
103 case "label":
104 query.NoFilters = append(query.NoFilters, cache.NoLabelFilter())
105 default:
106 return nil, fmt.Errorf("unknown \"no\" filter %s", no)
107 }
108 }
109
110 switch lsSortBy {
111 case "id":
112 query.OrderBy = cache.OrderById
113 case "creation":
114 query.OrderBy = cache.OrderByCreation
115 case "edit":
116 query.OrderBy = cache.OrderByEdit
117 default:
118 return nil, fmt.Errorf("unknown sort flag %s", lsSortBy)
119 }
120
121 switch lsSortDirection {
122 case "asc":
123 query.OrderDirection = cache.OrderAscending
124 case "desc":
125 query.OrderDirection = cache.OrderDescending
126 default:
127 return nil, fmt.Errorf("unknown sort direction %s", lsSortDirection)
128 }
129
130 return query, nil
131}
132
133var lsCmd = &cobra.Command{
134 Use: "ls [<query>]",
135 Short: "List bugs.",
136 Long: `Display a summary of each bugs.
137
138You 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.`,
139 Example: `List open bugs sorted by last edition with a query:
140git bug ls status:open sort:edit-desc
141
142List closed bugs sorted by creation with flags:
143git bug ls --status closed --by creation
144`,
145 PreRunE: loadRepo,
146 RunE: runLsBug,
147}
148
149func init() {
150 RootCmd.AddCommand(lsCmd)
151
152 lsCmd.Flags().SortFlags = false
153
154 lsCmd.Flags().StringSliceVarP(&lsStatusQuery, "status", "s", nil,
155 "Filter by status. Valid values are [open,closed]")
156 lsCmd.Flags().StringSliceVarP(&lsAuthorQuery, "author", "a", nil,
157 "Filter by author")
158 lsCmd.Flags().StringSliceVarP(&lsLabelQuery, "label", "l", nil,
159 "Filter by label")
160 lsCmd.Flags().StringSliceVarP(&lsTitleQuery, "title", "t", nil,
161 "Filter by title")
162 lsCmd.Flags().StringSliceVarP(&lsNoQuery, "no", "n", nil,
163 "Filter by absence of something. Valid values are [label]")
164 lsCmd.Flags().StringVarP(&lsSortBy, "by", "b", "creation",
165 "Sort the results by a characteristic. Valid values are [id,creation,edit]")
166 lsCmd.Flags().StringVarP(&lsSortDirection, "direction", "d", "asc",
167 "Select the sorting direction. Valid values are [asc,desc]")
168}