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		RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
19			return runPull(env, args)
20		}),
21	}
22
23	return cmd
24}
25
26func runPull(env *Env, args []string) error {
27	if len(args) > 1 {
28		return errors.New("Only pulling from one remote at a time is supported")
29	}
30
31	remote := "origin"
32	if len(args) == 1 {
33		remote = args[0]
34	}
35
36	env.out.Println("Fetching remote ...")
37
38	stdout, err := env.backend.Fetch(remote)
39	if err != nil {
40		return err
41	}
42
43	env.out.Println(stdout)
44
45	env.out.Println("Merging data ...")
46
47	for result := range env.backend.MergeAll(remote) {
48		if result.Err != nil {
49			env.err.Println(result.Err)
50		}
51
52		if result.Status != entity.MergeStatusNothing {
53			env.out.Printf("%s: %s\n", result.Id.Human(), result)
54		}
55	}
56
57	return nil
58}