git-bug/commands/pull.go

60 lines
1.0 KiB
Go
Raw Normal View History

package commands
2018-07-12 10:55:13 +03:00
import (
"errors"
2018-08-13 19:32:11 +03:00
"github.com/spf13/cobra"
"github.com/MichaelMure/git-bug/entity"
2018-07-12 10:55:13 +03:00
)
2020-06-28 19:26:29 +03:00
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),
2020-06-28 19:26:29 +03:00
RunE: func(cmd *cobra.Command, args []string) error {
return runPull(env, args)
},
}
return cmd
}
func runPull(env *Env, args []string) error {
2018-07-12 10:55:13 +03:00
if len(args) > 1 {
return errors.New("Only pulling from one remote at a time is supported")
2018-07-12 10:55:13 +03:00
}
remote := "origin"
if len(args) == 1 {
remote = args[0]
}
2020-06-28 19:26:29 +03:00
env.out.Println("Fetching remote ...")
stdout, err := env.backend.Fetch(remote)
if err != nil {
return err
}
2020-06-28 19:26:29 +03:00
env.out.Println(stdout)
2020-06-28 19:26:29 +03:00
env.out.Println("Merging data ...")
for result := range env.backend.MergeAll(remote) {
if result.Err != nil {
2020-06-28 19:26:29 +03:00
env.err.Println(result.Err)
}
if result.Status != entity.MergeStatusNothing {
2020-06-28 19:26:29 +03:00
env.out.Printf("%s: %s\n", result.Id.Human(), result)
}
}
return nil
2018-07-12 10:55:13 +03:00
}