bridge_push.go

 1package bridgecmd
 2
 3import (
 4	"context"
 5	"os"
 6	"sync"
 7	"time"
 8
 9	"github.com/spf13/cobra"
10
11	"github.com/MichaelMure/git-bug/bridge"
12	"github.com/MichaelMure/git-bug/bridge/core"
13	"github.com/MichaelMure/git-bug/commands/completion"
14	"github.com/MichaelMure/git-bug/commands/execenv"
15	"github.com/MichaelMure/git-bug/util/interrupt"
16)
17
18func newBridgePushCommand() *cobra.Command {
19	env := execenv.NewEnv()
20
21	cmd := &cobra.Command{
22		Use:     "push [NAME]",
23		Short:   "Push updates to remote bug tracker",
24		PreRunE: execenv.LoadBackendEnsureUser(env),
25		RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
26			return runBridgePush(env, args)
27		}),
28		Args:              cobra.MaximumNArgs(1),
29		ValidArgsFunction: completion.Bridge(env),
30	}
31
32	return cmd
33}
34
35func runBridgePush(env *execenv.Env, args []string) error {
36	var b *core.Bridge
37	var err error
38
39	if len(args) == 0 {
40		b, err = bridge.DefaultBridge(env.Backend)
41	} else {
42		b, err = bridge.LoadBridge(env.Backend, args[0])
43	}
44
45	if err != nil {
46		return err
47	}
48
49	parentCtx := context.Background()
50	ctx, cancel := context.WithCancel(parentCtx)
51	defer cancel()
52
53	done := make(chan struct{}, 1)
54
55	var mu sync.Mutex
56	interruptCount := 0
57	interrupt.RegisterCleaner(func() error {
58		mu.Lock()
59		if interruptCount > 0 {
60			env.Err.Println("Received another interrupt before graceful stop, terminating...")
61			os.Exit(0)
62		}
63
64		interruptCount++
65		mu.Unlock()
66
67		env.Err.Println("Received interrupt signal, stopping the import...\n(Hit ctrl-c again to kill the process.)")
68
69		// send signal to stop the importer
70		cancel()
71
72		// block until importer gracefully shutdown
73		<-done
74		return nil
75	})
76
77	events, err := b.ExportAll(ctx, time.Time{})
78	if err != nil {
79		return err
80	}
81
82	exportedIssues := 0
83	for result := range events {
84		if result.Event != core.ExportEventNothing {
85			env.Out.Println(result.String())
86		}
87
88		switch result.Event {
89		case core.ExportEventBug:
90			exportedIssues++
91		}
92	}
93
94	env.Out.Printf("exported %d issues with %s bridge\n", exportedIssues, b.Name)
95
96	// send done signal
97	close(done)
98	return nil
99}