1package commands
 2
 3import (
 4	"errors"
 5
 6	"github.com/spf13/cobra"
 7
 8	"github.com/MichaelMure/git-bug/entity"
 9)
10
11func newPullCommand() *cobra.Command {
12	env := newEnv()
13
14	cmd := &cobra.Command{
15		Use:      "pull [REMOTE]",
16		Short:    "Pull bugs update from a git remote.",
17		PreRunE:  loadBackend(env),
18		PostRunE: closeBackend(env),
19		RunE: func(cmd *cobra.Command, args []string) error {
20			return runPull(env, args)
21		},
22	}
23
24	return cmd
25}
26
27func runPull(env *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}