aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-10-27 11:46:10 +0200
committerGitHub <noreply@github.com>2018-10-27 11:46:10 +0200
commit8be1078dd6400765a9b9202a81c40ede3ee01981 (patch)
tree7d2d08b01bac008fcd3f6af8ff1e83c6593640e5
parente79476a9627226de3dee80e9a19b7f66c4ef68ff (diff)
parent31cbddd35b15519a2f8ffa8d93e6456323ea80ee (diff)
downloadgit-bug-8be1078dd6400765a9b9202a81c40ede3ee01981.tar.gz
Merge pull request #71 from auyer/master
Cleaning lock files on Interruption
-rw-r--r--commands/add.go2
-rw-r--r--commands/bridge.go2
-rw-r--r--commands/bridge_configure.go2
-rw-r--r--commands/bridge_pull.go2
-rw-r--r--commands/bridge_rm.go2
-rw-r--r--commands/comment.go2
-rw-r--r--commands/comment_add.go2
-rw-r--r--commands/deselect.go2
-rw-r--r--commands/label.go2
-rw-r--r--commands/label_add.go2
-rw-r--r--commands/label_rm.go2
-rw-r--r--commands/ls-labels.go2
-rw-r--r--commands/ls.go2
-rw-r--r--commands/pull.go2
-rw-r--r--commands/push.go2
-rw-r--r--commands/select.go2
-rw-r--r--commands/show.go2
-rw-r--r--commands/status.go2
-rw-r--r--commands/status_close.go2
-rw-r--r--commands/status_open.go2
-rw-r--r--commands/termui.go2
-rw-r--r--commands/title.go2
-rw-r--r--commands/title_edit.go2
-rw-r--r--util/interrupt/cleaner.go49
-rw-r--r--util/interrupt/cleaner_test.go50
25 files changed, 145 insertions, 0 deletions
diff --git a/commands/add.go b/commands/add.go
index ecc2381e..54ede126 100644
--- a/commands/add.go
+++ b/commands/add.go
@@ -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)
diff --git a/commands/bridge.go b/commands/bridge.go
index 4576cd0a..a473776d 100644
--- a/commands/bridge.go
+++ b/commands/bridge.go
@@ -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 {
diff --git a/commands/bridge_configure.go b/commands/bridge_configure.go
index ed18cae9..ef499f1f 100644
--- a/commands/bridge_configure.go
+++ b/commands/bridge_configure.go
@@ -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 {
diff --git a/commands/bridge_pull.go b/commands/bridge_pull.go
index a90a533f..669a6713 100644
--- a/commands/bridge_pull.go
+++ b/commands/bridge_pull.go
@@ -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
diff --git a/commands/bridge_rm.go b/commands/bridge_rm.go
index 2ebc17a7..172fc0d8 100644
--- a/commands/bridge_rm.go
+++ b/commands/bridge_rm.go
@@ -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 {
diff --git a/commands/comment.go b/commands/comment.go
index fc4b6a6b..89378da3 100644
--- a/commands/comment.go
+++ b/commands/comment.go
@@ -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 {
diff --git a/commands/comment_add.go b/commands/comment_add.go
index d0912844..58408bc5 100644
--- a/commands/comment_add.go
+++ b/commands/comment_add.go
@@ -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 {
diff --git a/commands/deselect.go b/commands/deselect.go
index a2e8d30d..210f158c 100644
--- a/commands/deselect.go
+++ b/commands/deselect.go
@@ -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 {
diff --git a/commands/label.go b/commands/label.go
index 0221701c..58ccc299 100644
--- a/commands/label.go
+++ b/commands/label.go
@@ -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 {
diff --git a/commands/label_add.go b/commands/label_add.go
index 278d6472..f04ed7d6 100644
--- a/commands/label_add.go
+++ b/commands/label_add.go
@@ -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 {
diff --git a/commands/label_rm.go b/commands/label_rm.go
index e53ac09a..36051ba1 100644
--- a/commands/label_rm.go
+++ b/commands/label_rm.go
@@ -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 {
diff --git a/commands/ls-labels.go b/commands/ls-labels.go
index 9dd94f08..ef2c95bc 100644
--- a/commands/ls-labels.go
+++ b/commands/ls-labels.go
@@ -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()
diff --git a/commands/ls.go b/commands/ls.go
index 1a759a26..2f621bc5 100644
--- a/commands/ls.go
+++ b/commands/ls.go
@@ -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 {
diff --git a/commands/pull.go b/commands/pull.go
index 27c0953b..67c2a339 100644
--- a/commands/pull.go
+++ b/commands/pull.go
@@ -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 ...")
diff --git a/commands/push.go b/commands/push.go
index 11282ada..0477be60 100644
--- a/commands/push.go
+++ b/commands/push.go
@@ -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 {
diff --git a/commands/select.go b/commands/select.go
index 0c50d1a6..cc688354 100644
--- a/commands/select.go
+++ b/commands/select.go
@@ -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]
diff --git a/commands/show.go b/commands/show.go
index c41af02d..86c01a17 100644
--- a/commands/show.go
+++ b/commands/show.go
@@ -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 {
diff --git a/commands/status.go b/commands/status.go
index 6aed000a..7928628a 100644
--- a/commands/status.go
+++ b/commands/status.go
@@ -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 {
diff --git a/commands/status_close.go b/commands/status_close.go
index ec4e503e..2b4f9602 100644
--- a/commands/status_close.go
+++ b/commands/status_close.go
@@ -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 {
diff --git a/commands/status_open.go b/commands/status_open.go
index c8717cd2..5e3029e2 100644
--- a/commands/status_open.go
+++ b/commands/status_open.go
@@ -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 {
diff --git a/commands/termui.go b/commands/termui.go
index 8df5ba7c..4a029d6c 100644
--- a/commands/termui.go
+++ b/commands/termui.go
@@ -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)
}
diff --git a/commands/title.go b/commands/title.go
index 5d723755..c9157a70 100644
--- a/commands/title.go
+++ b/commands/title.go
@@ -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 {
diff --git a/commands/title_edit.go b/commands/title_edit.go
index f3b93bb1..6bbd1b0a 100644
--- a/commands/title_edit.go
+++ b/commands/title_edit.go
@@ -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 {
diff --git a/util/interrupt/cleaner.go b/util/interrupt/cleaner.go
new file mode 100644
index 00000000..76c9d04d
--- /dev/null
+++ b/util/interrupt/cleaner.go
@@ -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
+}
diff --git a/util/interrupt/cleaner_test.go b/util/interrupt/cleaner_test.go
new file mode 100644
index 00000000..c4e5c9b3
--- /dev/null
+++ b/util/interrupt/cleaner_test.go
@@ -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")
+ }
+}