1package commands
2
3import (
4 "errors"
5
6 "github.com/spf13/cobra"
7
8 "github.com/MichaelMure/git-bug/commands/completion"
9 "github.com/MichaelMure/git-bug/commands/execenv"
10 "github.com/MichaelMure/git-bug/entity"
11)
12
13func newPullCommand(env *execenv.Env) *cobra.Command {
14 cmd := &cobra.Command{
15 Use: "pull [REMOTE]",
16 Short: "Pull updates from a git remote",
17 PreRunE: execenv.LoadBackend(env),
18 RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
19 return runPull(env, args)
20 }),
21 ValidArgsFunction: completion.GitRemote(env),
22 }
23
24 return cmd
25}
26
27func runPull(env *execenv.Env, args []string) error {
28 if len(args) > 1 {
29 return errors.New("Only pulling from one remote at a time is supported")
30 }
31
32 remote := "origin"
33 if len(args) == 1 {
34 remote = args[0]
35 }
36
37 env.Out.Println("Fetching remote ...")
38
39 stdout, err := env.Backend.Fetch(remote)
40 if err != nil {
41 return err
42 }
43
44 env.Out.Println(stdout)
45
46 env.Out.Println("Merging data ...")
47
48 for result := range env.Backend.MergeAll(remote) {
49 if result.Err != nil {
50 env.Err.Println(result.Err)
51 }
52
53 if result.Status != entity.MergeStatusNothing {
54 env.Out.Printf("%s: %s\n", result.Id.Human(), result)
55 }
56 }
57
58 return nil
59}