ls-id.go

 1package commands
 2
 3import (
 4	"github.com/spf13/cobra"
 5
 6	"github.com/MichaelMure/git-bug/cache"
 7	"github.com/MichaelMure/git-bug/util/interrupt"
 8)
 9
10func newLsIdCommand() *cobra.Command {
11	env := newEnv()
12
13	cmd := &cobra.Command{
14		Use:     "ls-id [<prefix>]",
15		Short:   "List bug identifiers.",
16		PreRunE: loadRepo(env),
17		RunE: func(cmd *cobra.Command, args []string) error {
18			return runLsId(env, args)
19		},
20	}
21
22	return cmd
23}
24
25func runLsId(env *Env, args []string) error {
26	backend, err := cache.NewRepoCache(env.repo)
27	if err != nil {
28		return err
29	}
30	defer backend.Close()
31	interrupt.RegisterCleaner(backend.Close)
32
33	var prefix = ""
34	if len(args) != 0 {
35		prefix = args[0]
36	}
37
38	for _, id := range backend.AllBugsIds() {
39		if prefix == "" || id.HasPrefix(prefix) {
40			env.out.Println(id)
41		}
42	}
43
44	return nil
45}