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		ValidArgsFunction: completeBug(env),
35	}
36
37	return cmd
38}
39
40func runSelect(env *Env, args []string) error {
41	if len(args) == 0 {
42		return errors.New("You must provide a bug id")
43	}
44
45	prefix := args[0]
46
47	b, err := env.backend.ResolveBugPrefix(prefix)
48	if err != nil {
49		return err
50	}
51
52	err = _select.Select(env.backend, b.Id())
53	if err != nil {
54		return err
55	}
56
57	env.out.Printf("selected bug %s: %s\n", b.Id().Human(), b.Snapshot().Title)
58
59	return nil
60}