pull.go

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