2018-07-12 13:44:46 +03:00
|
|
|
package commands
|
2018-07-12 10:55:13 +03:00
|
|
|
|
|
|
|
import (
|
2018-07-12 13:44:46 +03:00
|
|
|
"errors"
|
2018-08-13 19:32:11 +03:00
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2019-03-27 23:44:11 +03:00
|
|
|
"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{
|
2020-07-28 21:24:24 +03:00
|
|
|
Use: "pull [REMOTE]",
|
2020-06-28 20:09:32 +03:00
|
|
|
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 {
|
2018-07-12 13:44:46 +03:00
|
|
|
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 ...")
|
2018-09-07 18:10:40 +03:00
|
|
|
|
2020-06-28 20:09:32 +03:00
|
|
|
stdout, err := env.backend.Fetch(remote)
|
2018-09-07 18:10:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Println(stdout)
|
2018-09-07 18:10:40 +03:00
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Println("Merging data ...")
|
2018-09-07 18:10:40 +03:00
|
|
|
|
2020-06-28 20:09:32 +03:00
|
|
|
for result := range env.backend.MergeAll(remote) {
|
2019-03-27 23:44:11 +03:00
|
|
|
if result.Err != nil {
|
2020-06-28 19:26:29 +03:00
|
|
|
env.err.Println(result.Err)
|
2018-09-07 18:10:40 +03:00
|
|
|
}
|
|
|
|
|
2019-03-27 23:44:11 +03:00
|
|
|
if result.Status != entity.MergeStatusNothing {
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s: %s\n", result.Id.Human(), result)
|
2018-09-07 18:10:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-07-12 10:55:13 +03:00
|
|
|
}
|