From 26bd1dd11010b4d86cebe2510ad7085a6b316334 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 28 Jun 2020 18:26:29 +0200 Subject: commands: refactor to avoid globals --- commands/ls-id.go | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'commands/ls-id.go') diff --git a/commands/ls-id.go b/commands/ls-id.go index 22357eb4..6624793d 100644 --- a/commands/ls-id.go +++ b/commands/ls-id.go @@ -1,17 +1,29 @@ package commands import ( - "fmt" - "github.com/spf13/cobra" "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/util/interrupt" ) -func runLsID(cmd *cobra.Command, args []string) error { +func newLsIdCommand() *cobra.Command { + env := newEnv() + + cmd := &cobra.Command{ + Use: "ls-id []", + Short: "List bug identifiers.", + PreRunE: loadRepo(env), + RunE: func(cmd *cobra.Command, args []string) error { + return runLsId(env, args) + }, + } + + return cmd +} - backend, err := cache.NewRepoCache(repo) +func runLsId(env *Env, args []string) error { + backend, err := cache.NewRepoCache(env.repo) if err != nil { return err } @@ -25,20 +37,9 @@ func runLsID(cmd *cobra.Command, args []string) error { for _, id := range backend.AllBugsIds() { if prefix == "" || id.HasPrefix(prefix) { - fmt.Println(id) + env.out.Println(id) } } return nil } - -var listBugIDCmd = &cobra.Command{ - Use: "ls-id []", - Short: "List bug identifiers.", - PreRunE: loadRepo, - RunE: runLsID, -} - -func init() { - RootCmd.AddCommand(listBugIDCmd) -} -- cgit