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