Merge pull request #71 from auyer/master

Michael Muré created

Cleaning lock files on Interruption

Change summary

commands/add.go                |  2 +
commands/bridge.go             |  2 +
commands/bridge_configure.go   |  2 +
commands/bridge_pull.go        |  2 +
commands/bridge_rm.go          |  2 +
commands/comment.go            |  2 +
commands/comment_add.go        |  2 +
commands/deselect.go           |  2 +
commands/label.go              |  2 +
commands/label_add.go          |  2 +
commands/label_rm.go           |  2 +
commands/ls-labels.go          |  2 +
commands/ls.go                 |  2 +
commands/pull.go               |  2 +
commands/push.go               |  2 +
commands/select.go             |  2 +
commands/show.go               |  2 +
commands/status.go             |  2 +
commands/status_close.go       |  2 +
commands/status_open.go        |  2 +
commands/termui.go             |  2 +
commands/title.go              |  2 +
commands/title_edit.go         |  2 +
util/interrupt/cleaner.go      | 49 +++++++++++++++++++++++++++++++++++
util/interrupt/cleaner_test.go | 50 ++++++++++++++++++++++++++++++++++++
25 files changed, 145 insertions(+)

Detailed changes

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)

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 {

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 {

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
 

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 {

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 {

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 {

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 {

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 {

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 {

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 {

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

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 {

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

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 {

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]
 

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 {

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 {

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 {

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 {

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

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 {

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 {

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
+}

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