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.Id().Human(), 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 example:
52 git bug show
53instead of
54 git bug show 2f153ca
55
56The complementary command is "git bug deselect" performing the opposite operation.
57`,
58 PreRunE: loadRepo,
59 RunE: runSelect,
60}
61
62func init() {
63 RootCmd.AddCommand(selectCmd)
64 selectCmd.Flags().SortFlags = false
65}