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		fmt.Println("Query", args[0])
22		query, err = cache.ParseQuery(args[0])
23
24		if err != nil {
25			return err
26		}
27	}
28
29	allIds := backend.QueryBugs(query)
30
31	for _, id := range allIds {
32		b, err := backend.ResolveBug(id)
33		if err != nil {
34			return err
35		}
36
37		snapshot := b.Snapshot()
38
39		var author bug.Person
40
41		if len(snapshot.Comments) > 0 {
42			create := snapshot.Comments[0]
43			author = create.Author
44		}
45
46		// truncate + pad if needed
47		titleFmt := fmt.Sprintf("%-50.50s", snapshot.Title)
48		authorFmt := fmt.Sprintf("%-15.15s", author.Name)
49
50		fmt.Printf("%s %s\t%s\t%s\t%s\n",
51			util.Cyan(b.HumanId()),
52			util.Yellow(snapshot.Status),
53			titleFmt,
54			util.Magenta(authorFmt),
55			snapshot.Summary(),
56		)
57	}
58
59	return nil
60}
61
62var lsCmd = &cobra.Command{
63	Use:   "ls <query>",
64	Short: "Display a summary of all bugs",
65	RunE:  runLsBug,
66}
67
68func init() {
69	RootCmd.AddCommand(lsCmd)
70}