1package bugcmd
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/execenv"
10 _select "github.com/MichaelMure/git-bug/commands/select"
11 "github.com/MichaelMure/git-bug/entities/bug"
12)
13
14func ResolveSelected(repo *cache.RepoCache, args []string) (*cache.BugCache, []string, error) {
15 return _select.Resolve[*cache.BugCache](repo, bug.Typename, bug.Namespace, repo.Bugs(), args)
16}
17
18func newBugSelectCommand() *cobra.Command {
19 env := execenv.NewEnv()
20
21 cmd := &cobra.Command{
22 Use: "select BUG_ID",
23 Short: "Select a bug for implicit use in future commands",
24 Example: `git bug select 2f15
25git bug comment
26git bug status
27`,
28 Long: `Select a bug for implicit use in future commands.
29
30This command allows you to omit any bug ID argument, for example:
31 git bug show
32instead of
33 git bug show 2f153ca
34
35The complementary command is "git bug deselect" performing the opposite operation.
36`,
37 PreRunE: execenv.LoadBackend(env),
38 RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
39 return runBugSelect(env, args)
40 }),
41 ValidArgsFunction: BugCompletion(env),
42 }
43
44 return cmd
45}
46
47func runBugSelect(env *execenv.Env, args []string) error {
48 if len(args) == 0 {
49 return errors.New("You must provide a bug id")
50 }
51
52 prefix := args[0]
53
54 b, err := env.Backend.Bugs().ResolvePrefix(prefix)
55 if err != nil {
56 return err
57 }
58
59 err = _select.Select(env.Backend, bug.Namespace, b.Id())
60 if err != nil {
61 return err
62 }
63
64 env.Out.Printf("selected bug %s: %s\n", b.Id().Human(), b.Snapshot().Title)
65
66 return nil
67}