bug_select.go

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