aboutsummaryrefslogtreecommitdiffstats
path: root/commands/bridge_pull.go
blob: 95eb2fe04b1373b1ce39179d3be90a32dcd32c2b (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package commands

import (
	"context"
	"fmt"
	"os"
	"sync"
	"time"

	"github.com/araddon/dateparse"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"

	"github.com/MichaelMure/git-bug/bridge"
	"github.com/MichaelMure/git-bug/bridge/core"
	"github.com/MichaelMure/git-bug/util/interrupt"
)

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:  loadBackend(env),
		PostRunE: closeBackend(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\")")

	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")
	}

	var b *core.Bridge
	var err error

	if len(args) == 0 {
		b, err = bridge.DefaultBridge(env.backend)
	} else {
		b, err = bridge.LoadBridge(env.backend, args[0])
	}

	if err != nil {
		return err
	}

	parentCtx := context.Background()
	ctx, cancel := context.WithCancel(parentCtx)
	defer cancel()

	// buffered channel to avoid send block at the end
	done := make(chan struct{}, 1)

	var mu sync.Mutex
	interruptCount := 0
	interrupt.RegisterCleaner(func() error {
		mu.Lock()
		if interruptCount > 0 {
			env.err.Println("Received another interrupt before graceful stop, terminating...")
			os.Exit(0)
		}

		interruptCount++
		mu.Unlock()

		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()

		// block until importer gracefully shutdown
		<-done
		return nil
	})

	var events <-chan core.ImportResult
	switch {
	case opts.noResume:
		events, err = b.ImportAllSince(ctx, time.Time{})
	case opts.importSince != "":
		since, err2 := parseSince(opts.importSince)
		if err2 != nil {
			return errors.Wrap(err2, "import time parsing")
		}
		events, err = b.ImportAllSince(ctx, since)
	default:
		events, err = b.ImportAll(ctx)
	}

	if err != nil {
		return err
	}

	importedIssues := 0
	importedIdentities := 0
	for result := range events {
		switch result.Event {
		case core.ImportEventNothing:
			// filtered

		case core.ImportEventBug:
			importedIssues++
			env.out.Println(result.String())

		case core.ImportEventIdentity:
			importedIdentities++
			env.out.Println(result.String())

		case core.ImportEventError:
			if result.Err != context.Canceled {
				env.out.Println(result.String())
			}

		default:
			env.out.Println(result.String())
		}
	}

	env.out.Printf("imported %d issues and %d identities with %s bridge\n", importedIssues, importedIdentities, b.Name)

	// send done signal
	close(done)

	return nil
}

func parseSince(since string) (time.Time, error) {
	duration, err := time.ParseDuration(since)
	if err == nil {
		return time.Now().Add(-duration), nil
	}

	return dateparse.ParseLocal(since)
}