select.go

 1package commands
 2
 3import (
 4	"errors"
 5	"fmt"
 6
 7	"github.com/MichaelMure/git-bug/cache"
 8	"github.com/MichaelMure/git-bug/commands/select"
 9	"github.com/MichaelMure/git-bug/util/interrupt"
10	"github.com/spf13/cobra"
11)
12
13func runSelect(cmd *cobra.Command, args []string) error {
14	if len(args) == 0 {
15		return errors.New("You must provide a bug id")
16	}
17
18	backend, err := cache.NewRepoCache(repo)
19	if err != nil {
20		return err
21	}
22	defer backend.Close()
23	interrupt.RegisterCleaner(backend.Close)
24
25	prefix := args[0]
26
27	b, err := backend.ResolveBugPrefix(prefix)
28	if err != nil {
29		return err
30	}
31
32	err = _select.Select(backend, b.Id())
33	if err != nil {
34		return err
35	}
36
37	fmt.Printf("selected bug %s: %s\n", b.HumanId(), b.Snapshot().Title)
38
39	return nil
40}
41
42var selectCmd = &cobra.Command{
43	Use:   "select <id>",
44	Short: "Select a bug for implicit use in future commands.",
45	Example: `git bug select 2f15
46git bug comment
47git bug status
48`,
49	Long: `Select a bug for implicit use in future commands.
50
51This command allows you to omit any bug <id> argument for the following commands:
52  - git bug comment
53  - git bug label
54  - git bug show
55  - git bug status
56  - git bug title
57
58The complementary command is "git bug deselect" performing the opposite operation.
59`,
60	PreRunE: loadRepo,
61	RunE:    runSelect,
62}
63
64func init() {
65	RootCmd.AddCommand(selectCmd)
66	selectCmd.Flags().SortFlags = false
67}