ls.go

 1package commands
 2
 3import (
 4	"fmt"
 5
 6	"github.com/MichaelMure/git-bug/bug"
 7	"github.com/MichaelMure/git-bug/cache"
 8	"github.com/MichaelMure/git-bug/util"
 9	"github.com/spf13/cobra"
10)
11
12func runLsBug(cmd *cobra.Command, args []string) error {
13	backend, err := cache.NewRepoCache(repo)
14	if err != nil {
15		return err
16	}
17	defer backend.Close()
18
19	var query *cache.Query
20	if len(args) >= 1 {
21		query, err = cache.ParseQuery(args[0])
22
23		if err != nil {
24			return err
25		}
26	}
27
28	allIds := backend.QueryBugs(query)
29
30	for _, id := range allIds {
31		b, err := backend.ResolveBug(id)
32		if err != nil {
33			return err
34		}
35
36		snapshot := b.Snapshot()
37
38		var author bug.Person
39
40		if len(snapshot.Comments) > 0 {
41			create := snapshot.Comments[0]
42			author = create.Author
43		}
44
45		// truncate + pad if needed
46		titleFmt := fmt.Sprintf("%-50.50s", snapshot.Title)
47		authorFmt := fmt.Sprintf("%-15.15s", author.Name)
48
49		fmt.Printf("%s %s\t%s\t%s\t%s\n",
50			util.Cyan(b.HumanId()),
51			util.Yellow(snapshot.Status),
52			titleFmt,
53			util.Magenta(authorFmt),
54			snapshot.Summary(),
55		)
56	}
57
58	return nil
59}
60
61var lsCmd = &cobra.Command{
62	Use:   "ls <query>",
63	Short: "Display a summary of all bugs",
64	RunE:  runLsBug,
65}
66
67func init() {
68	RootCmd.AddCommand(lsCmd)
69}