pull.go

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