diff options
author | Michael Muré <batolettre@gmail.com> | 2020-06-28 18:26:29 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-06-28 18:26:29 +0200 |
commit | 26bd1dd11010b4d86cebe2510ad7085a6b316334 (patch) | |
tree | f1fe939311c75bd615071e96f3d37822cccd77a7 /commands/select.go | |
parent | c0dbc149d5c0c3610476ba14a800c9ba803a2c2c (diff) | |
download | git-bug-26bd1dd11010b4d86cebe2510ad7085a6b316334.tar.gz git-bug-26bd1dd11010b4d86cebe2510ad7085a6b316334.zip |
commands: refactor to avoid globals
Diffstat (limited to 'commands/select.go')
-rw-r--r-- | commands/select.go | 60 |
1 files changed, 31 insertions, 29 deletions
diff --git a/commands/select.go b/commands/select.go index f2ae33ca..e4916650 100644 --- a/commands/select.go +++ b/commands/select.go @@ -2,7 +2,6 @@ package commands import ( "errors" - "fmt" "github.com/spf13/cobra" @@ -11,12 +10,40 @@ import ( "github.com/MichaelMure/git-bug/util/interrupt" ) -func runSelect(cmd *cobra.Command, args []string) error { +func newSelectCommand() *cobra.Command { + env := newEnv() + + cmd := &cobra.Command{ + Use: "select <id>", + Short: "Select a bug for implicit use in future commands.", + Example: `git bug select 2f15 +git bug comment +git bug status +`, + Long: `Select a bug for implicit use in future commands. + +This command allows you to omit any bug <id> argument, for example: + git bug show +instead of + git bug show 2f153ca + +The complementary command is "git bug deselect" performing the opposite operation. +`, + PreRunE: loadRepo(env), + RunE: func(cmd *cobra.Command, args []string) error { + return runSelect(env, args) + }, + } + + return cmd +} + +func runSelect(env *Env, args []string) error { if len(args) == 0 { return errors.New("You must provide a bug id") } - backend, err := cache.NewRepoCache(repo) + backend, err := cache.NewRepoCache(env.repo) if err != nil { return err } @@ -35,32 +62,7 @@ func runSelect(cmd *cobra.Command, args []string) error { return err } - fmt.Printf("selected bug %s: %s\n", b.Id().Human(), b.Snapshot().Title) + env.out.Printf("selected bug %s: %s\n", b.Id().Human(), b.Snapshot().Title) return nil } - -var selectCmd = &cobra.Command{ - Use: "select <id>", - Short: "Select a bug for implicit use in future commands.", - Example: `git bug select 2f15 -git bug comment -git bug status -`, - Long: `Select a bug for implicit use in future commands. - -This command allows you to omit any bug <id> argument, for example: - git bug show -instead of - git bug show 2f153ca - -The complementary command is "git bug deselect" performing the opposite operation. -`, - PreRunE: loadRepo, - RunE: runSelect, -} - -func init() { - RootCmd.AddCommand(selectCmd) - selectCmd.Flags().SortFlags = false -} |