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/pull.go | |
parent | c0dbc149d5c0c3610476ba14a800c9ba803a2c2c (diff) | |
download | git-bug-26bd1dd11010b4d86cebe2510ad7085a6b316334.tar.gz |
commands: refactor to avoid globals
Diffstat (limited to 'commands/pull.go')
-rw-r--r-- | commands/pull.go | 42 |
1 files changed, 22 insertions, 20 deletions
diff --git a/commands/pull.go b/commands/pull.go index 0439ab41..fb50a03b 100644 --- a/commands/pull.go +++ b/commands/pull.go @@ -2,7 +2,6 @@ package commands import ( "errors" - "fmt" "github.com/spf13/cobra" @@ -11,7 +10,22 @@ import ( "github.com/MichaelMure/git-bug/util/interrupt" ) -func runPull(cmd *cobra.Command, args []string) error { +func newPullCommand() *cobra.Command { + env := newEnv() + + cmd := &cobra.Command{ + Use: "pull [<remote>]", + Short: "Pull bugs update from a git remote.", + PreRunE: loadRepo(env), + RunE: func(cmd *cobra.Command, args []string) error { + return runPull(env, args) + }, + } + + return cmd +} + +func runPull(env *Env, args []string) error { if len(args) > 1 { return errors.New("Only pulling from one remote at a time is supported") } @@ -21,45 +35,33 @@ func runPull(cmd *cobra.Command, args []string) error { remote = args[0] } - backend, err := cache.NewRepoCache(repo) + backend, err := cache.NewRepoCache(env.repo) if err != nil { return err } defer backend.Close() interrupt.RegisterCleaner(backend.Close) - fmt.Println("Fetching remote ...") + env.out.Println("Fetching remote ...") stdout, err := backend.Fetch(remote) if err != nil { return err } - fmt.Println(stdout) + env.out.Println(stdout) - fmt.Println("Merging data ...") + env.out.Println("Merging data ...") for result := range backend.MergeAll(remote) { if result.Err != nil { - fmt.Println(result.Err) + env.err.Println(result.Err) } if result.Status != entity.MergeStatusNothing { - fmt.Printf("%s: %s\n", result.Id.Human(), result) + env.out.Printf("%s: %s\n", result.Id.Human(), result) } } return nil } - -// showCmd defines the "push" subcommand. -var pullCmd = &cobra.Command{ - Use: "pull [<remote>]", - Short: "Pull bugs update from a git remote.", - PreRunE: loadRepo, - RunE: runPull, -} - -func init() { - RootCmd.AddCommand(pullCmd) -} |