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/spf13/cobra"
10)
11
12func runSelect(cmd *cobra.Command, args []string) error {
13	if len(args) == 0 {
14		return errors.New("You must provide a bug id")
15	}
16
17	backend, err := cache.NewRepoCache(repo)
18	if err != nil {
19		return err
20	}
21	defer backend.Close()
22
23	prefix := args[0]
24
25	b, err := backend.ResolveBugPrefix(prefix)
26	if err != nil {
27		return err
28	}
29
30	err = _select.Select(backend, b.Id())
31	if err != nil {
32		return err
33	}
34
35	fmt.Printf("selected bug %s: %s\n", b.HumanId(), b.Snapshot().Title)
36
37	return nil
38}
39
40var selectCmd = &cobra.Command{
41	Use:   "select <id>",
42	Short: "Select a bug for implicit use in future commands",
43	Example: `git bug select 2f15
44git bug comment
45git bug status
46`,
47	RunE: runSelect,
48}
49
50func init() {
51	RootCmd.AddCommand(selectCmd)
52	selectCmd.Flags().SortFlags = false
53}