1package bugcmd
2
3import (
4 "errors"
5
6 "github.com/spf13/cobra"
7
8 "github.com/MichaelMure/git-bug/commands/bug/select"
9 "github.com/MichaelMure/git-bug/commands/completion"
10 "github.com/MichaelMure/git-bug/commands/execenv"
11)
12
13func newBugSelectCommand() *cobra.Command {
14 env := execenv.NewEnv()
15
16 cmd := &cobra.Command{
17 Use: "select BUG_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: execenv.LoadBackend(env),
33 RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
34 return runBugSelect(env, args)
35 }),
36 ValidArgsFunction: completion.Bug(env),
37 }
38
39 return cmd
40}
41
42func runBugSelect(env *execenv.Env, args []string) error {
43 if len(args) == 0 {
44 return errors.New("You must provide a bug id")
45 }
46
47 prefix := args[0]
48
49 b, err := env.Backend.ResolveBugPrefix(prefix)
50 if err != nil {
51 return err
52 }
53
54 err = _select.Select(env.Backend, b.Id())
55 if err != nil {
56 return err
57 }
58
59 env.Out.Printf("selected bug %s: %s\n", b.Id().Human(), b.Snapshot().Title)
60
61 return nil
62}