select.go

 1package commands
 2
 3import (
 4	"errors"
 5
 6	"github.com/spf13/cobra"
 7
 8	"github.com/MichaelMure/git-bug/cache"
 9	"github.com/MichaelMure/git-bug/commands/select"
10	"github.com/MichaelMure/git-bug/util/interrupt"
11)
12
13func newSelectCommand() *cobra.Command {
14	env := newEnv()
15
16	cmd := &cobra.Command{
17		Use:   "select <id>",
18		Short: "Select a bug for implicit use in future commands.",
19		Example: `git bug select 2f15
20git bug comment
21git bug status
22`,
23		Long: `Select a bug for implicit use in future commands.
24
25This command allows you to omit any bug <id> argument, for example:
26  git bug show
27instead of
28  git bug show 2f153ca
29
30The complementary command is "git bug deselect" performing the opposite operation.
31`,
32		PreRunE: loadRepo(env),
33		RunE: func(cmd *cobra.Command, args []string) error {
34			return runSelect(env, args)
35		},
36	}
37
38	return cmd
39}
40
41func runSelect(env *Env, args []string) error {
42	if len(args) == 0 {
43		return errors.New("You must provide a bug id")
44	}
45
46	backend, err := cache.NewRepoCache(env.repo)
47	if err != nil {
48		return err
49	}
50	defer backend.Close()
51	interrupt.RegisterCleaner(backend.Close)
52
53	prefix := args[0]
54
55	b, err := backend.ResolveBugPrefix(prefix)
56	if err != nil {
57		return err
58	}
59
60	err = _select.Select(backend, b.Id())
61	if err != nil {
62		return err
63	}
64
65	env.out.Printf("selected bug %s: %s\n", b.Id().Human(), b.Snapshot().Title)
66
67	return nil
68}