aboutsummaryrefslogtreecommitdiffstats
path: root/commands/pull.go
blob: 3ad4a0c91c67496acee05bd2e3a9d200121b7a9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package commands

import (
	"errors"

	"github.com/spf13/cobra"

	"github.com/MichaelMure/git-bug/entity"
)

func newPullCommand() *cobra.Command {
	env := newEnv()

	cmd := &cobra.Command{
		Use:      "pull [REMOTE]",
		Short:    "Pull bugs update from a git remote.",
		PreRunE:  loadBackend(env),
		PostRunE: closeBackend(env),
		RunE: func(cmd *cobra.Command, args []string) error {
			return runPull(env, args)
		},
	}

	return cmd
}

func runPull(env *Env, args []string) error {
	if len(args) > 1 {
		return errors.New("Only pulling from one remote at a time is supported")
	}

	remote := "origin"
	if len(args) == 1 {
		remote = args[0]
	}

	env.out.Println("Fetching remote ...")

	stdout, err := env.backend.Fetch(remote)
	if err != nil {
		return err
	}

	env.out.Println(stdout)

	env.out.Println("Merging data ...")

	for result := range env.backend.MergeAll(remote) {
		if result.Err != nil {
			env.err.Println(result.Err)
		}

		if result.Status != entity.MergeStatusNothing {
			env.out.Printf("%s: %s\n", result.Id.Human(), result)
		}
	}

	return nil
}