diff --git a/commands/add.go b/commands/add.go index ecc2381ea0141354827e92bff78a79f2da5f5111..54ede1260daaa6f3e0cd9d12725ec9ca970a7a3b 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 4576cd0aac1c5537157300aeeab7e4e97e9d007d..a473776d7eaa1692e93d5ddb355c1ffe3da4b0fc 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 ed18cae9df2f9707a1f74282f0a1ee2cebfb56a9..ef499f1ff59b97dee3fe05cc98fe6706ca228c5a 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 a90a533f03b088f52694829664e71527ea692d55..669a67133b0ddd6b26386c7bf23d7d0fda841368 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 2ebc17a7a47fce533a3db6f0940173ef3d1112b0..172fc0d8d2ff214789e69f80565542e787679ed6 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 fc4b6a6b24d566db65ce874aedcea2f2d69a57c2..89378da30271c5ad746d545356fc059ecd72c9af 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 d09128448ad5e73eb2fe21105dcb38cdc9fa512e..58408bc58115300d1fc5db36ce9b61dfd92fd371 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 a2e8d30d65057c6605c1478fcbcf74e555f5b829..210f158c1b7638d651c5a09aae669163ab8fe1dc 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 0221701ca2ac62a0531d2a250b9943c0967b7851..58ccc299f098c3a9638ace473f7f682474a91311 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 278d64721e977f2611af828d7ce7e1d56d1248de..f04ed7d67514f5e26b51e86aa1b72e4145e2537c 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 e53ac09a0997bfffb0a3fc37ad7d49a9f06fb459..36051ba197f1fd4fd72641ddba8cebba63c8d8a7 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 9dd94f081d6b6f09bc3c79183fb45092733609b5..ef2c95bcc3fc866551165ccae4ecce3c4944de61 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 1a759a264769f959860ebc7e72afdc467f7b42b0..2f621bc5aaf2f387f30cdbf315c732e56547cbe4 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 27c0953ba79ef8f8555495903a3483d4e04867ab..67c2a3398a0a8a7fab90d6f16097d28c411c6c88 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 11282ada4c42e5b6da4389daf90113c8fc1ed87d..0477be60a5545ac6a583aea0fbf67e25a263d254 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 0c50d1a6bd73676df18f0d2bbc27e41b7bc9b631..cc6883540cb7f7525b7c65d4e600f5803b82a6aa 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 c41af02daa2252796fe9fc1f236cf55286f0176a..86c01a176e3c7e2e890c8bb43a54e254205d1b55 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 6aed000a6fe42724734d6ec58b0d5864ffd727a7..7928628a751eab19cf6b8daa1faeeb319266d060 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 ec4e503eff736ce444b65a524b4262daa45755a2..2b4f9602c307fd9e7dc61a0f92fbb9784a876674 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 c8717cd2f90f7dae2812ec01628e92f9f6d70fae..5e3029e218313b7b9825878fa2dddc553fc913b8 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 8df5ba7c0bb9bcc37bbaff147a145db57b5bdf5f..4a029d6c256696544a6c0381c5013f16fdcb7c3a 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 5d7237551d62486e123911ace45757d6bef73fb1..c9157a7066201ebffbb66e55fcac934df459be5e 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 f3b93bb1d635b67e4c15f950ee070dfeb98ee55f..6bbd1b0a086f634f86f50ddbca5534d50af2a9cb 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 0000000000000000000000000000000000000000..76c9d04dcdf1218aa2bfd37b5a3411767856e568 --- /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 0000000000000000000000000000000000000000..c4e5c9b3c213a2ce6c577df362b5641aeb24cd26 --- /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") + } +}