board_select.go

 1package boardcmd
 2
 3import (
 4	"errors"
 5
 6	"github.com/spf13/cobra"
 7
 8	"github.com/git-bug/git-bug/cache"
 9	"github.com/git-bug/git-bug/commands/execenv"
10	_select "github.com/git-bug/git-bug/commands/select"
11	"github.com/git-bug/git-bug/entities/board"
12)
13
14func ResolveSelected(repo *cache.RepoCache, args []string) (*cache.BoardCache, []string, error) {
15	return _select.Resolve[*cache.BoardCache](repo, board.Typename, board.Namespace, repo.Boards(), args)
16}
17
18func newBoardSelectCommand() *cobra.Command {
19	env := execenv.NewEnv()
20
21	cmd := &cobra.Command{
22		Use:   "select BOARD_ID",
23		Short: "Select a board for implicit use in future commands",
24		Long: `Select a board for implicit use in future commands.
25
26The complementary command is "git board deselect" performing the opposite operation.
27`,
28		PreRunE: execenv.LoadBackend(env),
29		RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
30			return runBoardSelect(env, args)
31		}),
32		ValidArgsFunction: BoardCompletion(env),
33	}
34
35	return cmd
36}
37
38func runBoardSelect(env *execenv.Env, args []string) error {
39	if len(args) == 0 {
40		return errors.New("You must provide a board id")
41	}
42
43	prefix := args[0]
44
45	b, err := env.Backend.Boards().ResolvePrefix(prefix)
46	if err != nil {
47		return err
48	}
49
50	err = _select.Select(env.Backend, board.Namespace, b.Id())
51	if err != nil {
52		return err
53	}
54
55	env.Out.Printf("selected board %s: %s\n", b.Id().Human(), b.Snapshot().Title)
56
57	return nil
58}