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