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