aboutsummaryrefslogtreecommitdiffstats
path: root/commands/ls.go
blob: 7dbac96e8c490ceaf33f970593d31104d863df54 (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
package commands

import (
	"fmt"
	"strings"

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

var (
	lsStatusQuery   []string
	lsAuthorQuery   []string
	lsTitleQuery    []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 {
		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)

	for _, id := range allIds {
		b, err := backend.ResolveBugExcerpt(id)
		if err != nil {
			return err
		}

		// truncate + pad if needed
		titleFmt := fmt.Sprintf("%-50.50s", b.Title)
		authorFmt := fmt.Sprintf("%-15.15s", b.Author.Name)

		fmt.Printf("%s %s\t%s\t%s\tC:%d L:%d\n",
<<<<<<< HEAD
			colors.Cyan(b.HumanId()),
=======
			colors.Cyan(b.Id),
>>>>>>> Made requested changes
			colors.Yellow(b.Status),
			titleFmt,
			colors.Magenta(authorFmt),
			b.LenComments,
			len(b.Labels),
		)
	}

	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 _, 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() {
	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(&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]")
}