Detailed changes
@@ -5,6 +5,7 @@ import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/input"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -22,6 +23,7 @@ func runAddBug(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
if addMessageFile != "" && addMessage == "" {
addTitle, addMessage, err = input.BugCreateFileInput(addMessageFile)
@@ -5,6 +5,7 @@ import (
"github.com/MichaelMure/git-bug/bridge"
"github.com/MichaelMure/git-bug/cache"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -14,6 +15,7 @@ func runBridge(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
configured, err := bridge.ConfiguredBridges(backend)
if err != nil {
@@ -9,6 +9,7 @@ import (
"github.com/MichaelMure/git-bug/bridge"
"github.com/MichaelMure/git-bug/cache"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -18,6 +19,7 @@ func runBridgeConfigure(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
target, err := promptTarget()
if err != nil {
@@ -4,6 +4,7 @@ import (
"github.com/MichaelMure/git-bug/bridge"
"github.com/MichaelMure/git-bug/bridge/core"
"github.com/MichaelMure/git-bug/cache"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -13,6 +14,7 @@ func runBridgePull(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
var b *core.Bridge
@@ -3,6 +3,7 @@ package commands
import (
"github.com/MichaelMure/git-bug/bridge"
"github.com/MichaelMure/git-bug/cache"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -12,6 +13,7 @@ func runBridgeRm(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
err = bridge.RemoveBridges(backend, args[0])
if err != nil {
@@ -7,6 +7,7 @@ import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/commands/select"
"github.com/MichaelMure/git-bug/util/colors"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/MichaelMure/git-bug/util/text"
"github.com/spf13/cobra"
)
@@ -17,6 +18,7 @@ func runComment(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
b, args, err := _select.ResolveBug(backend, args)
if err != nil {
@@ -6,6 +6,7 @@ import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/commands/select"
"github.com/MichaelMure/git-bug/input"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -20,6 +21,7 @@ func runCommentAdd(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
b, args, err := _select.ResolveBug(backend, args)
if err != nil {
@@ -3,6 +3,7 @@ package commands
import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/commands/select"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -12,6 +13,7 @@ func runDeselect(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
err = _select.Clear(backend)
if err != nil {
@@ -5,6 +5,7 @@ import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/commands/select"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -14,6 +15,7 @@ func runLabel(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
b, args, err := _select.ResolveBug(backend, args)
if err != nil {
@@ -5,6 +5,7 @@ import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/commands/select"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -14,6 +15,7 @@ func runLabelAdd(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
b, args, err := _select.ResolveBug(backend, args)
if err != nil {
@@ -5,6 +5,7 @@ import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/commands/select"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -14,6 +15,7 @@ func runLabelRm(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
b, args, err := _select.ResolveBug(backend, args)
if err != nil {
@@ -4,6 +4,7 @@ import (
"fmt"
"github.com/MichaelMure/git-bug/cache"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -13,6 +14,7 @@ func runLsLabel(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
labels := backend.ValidLabels()
@@ -7,6 +7,7 @@ import (
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/util/colors"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -25,6 +26,7 @@ func runLsBug(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
var query *cache.Query
if len(args) >= 1 {
@@ -6,6 +6,7 @@ import (
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/cache"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -24,6 +25,7 @@ func runPull(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
fmt.Println("Fetching remote ...")
@@ -5,6 +5,7 @@ import (
"fmt"
"github.com/MichaelMure/git-bug/cache"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -23,6 +24,7 @@ func runPush(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
stdout, err := backend.Push(remote)
if err != nil {
@@ -6,6 +6,7 @@ import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/commands/select"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -19,6 +20,7 @@ func runSelect(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
prefix := args[0]
@@ -8,6 +8,7 @@ import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/commands/select"
"github.com/MichaelMure/git-bug/util/colors"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -17,6 +18,7 @@ func runShowBug(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
b, args, err := _select.ResolveBug(backend, args)
if err != nil {
@@ -5,6 +5,7 @@ import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/commands/select"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -14,6 +15,7 @@ func runStatus(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
b, args, err := _select.ResolveBug(backend, args)
if err != nil {
@@ -3,6 +3,7 @@ package commands
import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/commands/select"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -12,6 +13,7 @@ func runStatusClose(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
b, args, err := _select.ResolveBug(backend, args)
if err != nil {
@@ -3,6 +3,7 @@ package commands
import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/commands/select"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -12,6 +13,7 @@ func runStatusOpen(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
b, args, err := _select.ResolveBug(backend, args)
if err != nil {
@@ -3,6 +3,7 @@ package commands
import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/termui"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -12,6 +13,7 @@ func runTermUI(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
return termui.Run(backend)
}
@@ -5,6 +5,7 @@ import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/commands/select"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -14,6 +15,7 @@ func runTitle(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
b, args, err := _select.ResolveBug(backend, args)
if err != nil {
@@ -6,6 +6,7 @@ import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/commands/select"
"github.com/MichaelMure/git-bug/input"
+ "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
)
@@ -19,6 +20,7 @@ func runTitleEdit(cmd *cobra.Command, args []string) error {
return err
}
defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
b, args, err := _select.ResolveBug(backend, args)
if err != nil {
@@ -0,0 +1,49 @@
+package interrupt
+
+import (
+ "fmt"
+ "os"
+ "os/signal"
+ "syscall"
+)
+
+// Cleaner type referes to a function with no inputs that returns an error
+type Cleaner func() error
+
+var cleaners []Cleaner
+var active = false
+
+// RegisterCleaner is responsible for regisreting a cleaner function. When a function is registered, the Signal watcher is started in a goroutine.
+func RegisterCleaner(f ...Cleaner) {
+ for _, fn := range f {
+ cleaners = append([]Cleaner{fn}, cleaners...)
+ if !active {
+ active = true
+ go func() {
+ ch := make(chan os.Signal, 1)
+ signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
+ <-ch
+ // Prevent un-terminated ^C character in terminal
+ fmt.Println()
+ fmt.Println("Cleaning")
+ errl := Clean()
+ for _, err := range errl {
+ fmt.Println(err)
+ }
+ os.Exit(1)
+ }()
+ }
+ }
+}
+
+// Clean invokes all registered cleanup functions, and returns a list of errors, if they exist.
+func Clean() (errorlist []error) {
+ for _, f := range cleaners {
+ err := f()
+ if err != nil {
+ errorlist = append(errorlist, err)
+ }
+ }
+ cleaners = []Cleaner{}
+ return
+}
@@ -0,0 +1,50 @@
+package interrupt
+
+import (
+ "errors"
+ "testing"
+)
+
+// TestRegisterAndErrorAtCleaning tests if the registered order was kept by checking the returned errors
+func TestRegisterAndErrorAtCleaning(t *testing.T) {
+ active = true // this prevents goroutine from being started during the tests
+
+ f := func() error {
+ return errors.New("X")
+ }
+ f2 := func() error {
+ return errors.New("Y")
+ }
+ f3 := func() error {
+ return nil
+ }
+ RegisterCleaner(f)
+ RegisterCleaner(f2, f3)
+ // count := 0
+
+ errl := Clean()
+ if len(errl) != 2 {
+ t.Fatalf("unexpected error count")
+ }
+ if errl[0].Error() != "Y" && errl[1].Error() != "X" {
+ t.Fatalf("unexpected error order")
+
+ }
+}
+
+func TestRegisterAndClean(t *testing.T) {
+ active = true // this prevents goroutine from being started during the tests
+
+ f := func() error {
+ return nil
+ }
+ f2 := func() error {
+ return nil
+ }
+ RegisterCleaner(f, f2)
+
+ errl := Clean()
+ if len(errl) != 0 {
+ t.Fatalf("unexpected error count")
+ }
+}