diff options
author | Michael Muré <batolettre@gmail.com> | 2020-06-28 18:26:29 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-06-28 18:26:29 +0200 |
commit | 26bd1dd11010b4d86cebe2510ad7085a6b316334 (patch) | |
tree | f1fe939311c75bd615071e96f3d37822cccd77a7 /commands/bridge_pull.go | |
parent | c0dbc149d5c0c3610476ba14a800c9ba803a2c2c (diff) | |
download | git-bug-26bd1dd11010b4d86cebe2510ad7085a6b316334.tar.gz |
commands: refactor to avoid globals
Diffstat (limited to 'commands/bridge_pull.go')
-rw-r--r-- | commands/bridge_pull.go | 71 |
1 files changed, 40 insertions, 31 deletions
diff --git a/commands/bridge_pull.go b/commands/bridge_pull.go index 2dd3d93e..dc8825fa 100644 --- a/commands/bridge_pull.go +++ b/commands/bridge_pull.go @@ -17,17 +17,40 @@ import ( "github.com/MichaelMure/git-bug/util/interrupt" ) -var ( - bridgePullImportSince string - bridgePullNoResume bool -) +type bridgePullOptions struct { + importSince string + noResume bool +} + +func newBridgePullCommand() *cobra.Command { + env := newEnv() + options := bridgePullOptions{} + + cmd := &cobra.Command{ + Use: "pull [<name>]", + Short: "Pull updates.", + PreRunE: loadRepo(env), + RunE: func(cmd *cobra.Command, args []string) error { + return runBridgePull(env, options, args) + }, + Args: cobra.MaximumNArgs(1), + } + + flags := cmd.Flags() + flags.SortFlags = false + + flags.BoolVarP(&options.noResume, "no-resume", "n", false, "force importing all bugs") + flags.StringVarP(&options.importSince, "since", "s", "", "import only bugs updated after the given date (ex: \"200h\" or \"june 2 2019\")") -func runBridgePull(cmd *cobra.Command, args []string) error { - if bridgePullNoResume && bridgePullImportSince != "" { + return cmd +} + +func runBridgePull(env *Env, opts bridgePullOptions, args []string) error { + if opts.noResume && opts.importSince != "" { return fmt.Errorf("only one of --no-resume and --since flags should be used") } - backend, err := cache.NewRepoCache(repo) + backend, err := cache.NewRepoCache(env.repo) if err != nil { return err } @@ -58,14 +81,14 @@ func runBridgePull(cmd *cobra.Command, args []string) error { interrupt.RegisterCleaner(func() error { mu.Lock() if interruptCount > 0 { - fmt.Println("Received another interrupt before graceful stop, terminating...") + env.err.Println("Received another interrupt before graceful stop, terminating...") os.Exit(0) } interruptCount++ mu.Unlock() - fmt.Println("Received interrupt signal, stopping the import...\n(Hit ctrl-c again to kill the process.)") + env.err.Println("Received interrupt signal, stopping the import...\n(Hit ctrl-c again to kill the process.)") // send signal to stop the importer cancel() @@ -77,10 +100,10 @@ func runBridgePull(cmd *cobra.Command, args []string) error { var events <-chan core.ImportResult switch { - case bridgePullNoResume: + case opts.noResume: events, err = b.ImportAllSince(ctx, time.Time{}) - case bridgePullImportSince != "": - since, err2 := parseSince(bridgePullImportSince) + case opts.importSince != "": + since, err2 := parseSince(opts.importSince) if err2 != nil { return errors.Wrap(err2, "import time parsing") } @@ -102,23 +125,23 @@ func runBridgePull(cmd *cobra.Command, args []string) error { case core.ImportEventBug: importedIssues++ - fmt.Println(result.String()) + env.out.Println(result.String()) case core.ImportEventIdentity: importedIdentities++ - fmt.Println(result.String()) + env.out.Println(result.String()) case core.ImportEventError: if result.Err != context.Canceled { - fmt.Println(result.String()) + env.out.Println(result.String()) } default: - fmt.Println(result.String()) + env.out.Println(result.String()) } } - fmt.Printf("imported %d issues and %d identities with %s bridge\n", importedIssues, importedIdentities, b.Name) + env.out.Printf("imported %d issues and %d identities with %s bridge\n", importedIssues, importedIdentities, b.Name) // send done signal close(done) @@ -134,17 +157,3 @@ func parseSince(since string) (time.Time, error) { return dateparse.ParseLocal(since) } - -var bridgePullCmd = &cobra.Command{ - Use: "pull [<name>]", - Short: "Pull updates.", - PreRunE: loadRepo, - RunE: runBridgePull, - Args: cobra.MaximumNArgs(1), -} - -func init() { - bridgeCmd.AddCommand(bridgePullCmd) - bridgePullCmd.Flags().BoolVarP(&bridgePullNoResume, "no-resume", "n", false, "force importing all bugs") - bridgePullCmd.Flags().StringVarP(&bridgePullImportSince, "since", "s", "", "import only bugs updated after the given date (ex: \"200h\" or \"june 2 2019\")") -} |