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