From addeb9a0a1114136c5bc5773724a19d6be377c9d Mon Sep 17 00:00:00 2001 From: Rafael Passos Date: Tue, 23 Oct 2018 18:00:55 -0300 Subject: [PATCH 1/9] Created Cleaner package --- cleaner/cleaner.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 cleaner/cleaner.go diff --git a/cleaner/cleaner.go b/cleaner/cleaner.go new file mode 100644 index 0000000000000000000000000000000000000000..9bce4c03c6b1734e90d2b6307938dc6880ab818d --- /dev/null +++ b/cleaner/cleaner.go @@ -0,0 +1,38 @@ +package cleaner + +import ( + "fmt" + "os" + "os/signal" + "syscall" +) + +type t func() error + +var cleaners []t +var inactive bool + +// Register a cleaner function. When a function is registered, the Signal watcher is started in a goroutine. +func Register(f t) { + cleaners = append(cleaners, f) + if !inactive { + inactive = false + go func() { + ch := make(chan os.Signal, 1) + signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) + <-ch + fmt.Println("") + clean() + os.Exit(1) + }() + } +} + +// Clean invokes all registered cleanup functions +func clean() { + fmt.Println("Cleaning") + for _, f := range cleaners { + _ = f() + } + cleaners = []t{} +} From c38f8f1c307cf0320ed3d8556cd9dede443fb2a2 Mon Sep 17 00:00:00 2001 From: Rafael Passos Date: Tue, 23 Oct 2018 18:01:00 -0300 Subject: [PATCH 2/9] Registering lock clearing functions to Cleaner --- 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 ++ 23 files changed, 46 insertions(+) diff --git a/commands/add.go b/commands/add.go index ecc2381ea0141354827e92bff78a79f2da5f5111..e2650406b3be6165431e6a007ddc13719291d292 100644 --- a/commands/add.go +++ b/commands/add.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/input" "github.com/spf13/cobra" ) @@ -22,6 +23,7 @@ func runAddBug(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(backend.Close) if addMessageFile != "" && addMessage == "" { addTitle, addMessage, err = input.BugCreateFileInput(addMessageFile) diff --git a/commands/bridge.go b/commands/bridge.go index 4576cd0aac1c5537157300aeeab7e4e97e9d007d..ffba2b9d8c94d24b98f4734cd1b6cfae3ba7fdbd 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/cleaner" "github.com/spf13/cobra" ) @@ -14,6 +15,7 @@ func runBridge(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(backend.Close) configured, err := bridge.ConfiguredBridges(backend) if err != nil { diff --git a/commands/bridge_configure.go b/commands/bridge_configure.go index ed18cae9df2f9707a1f74282f0a1ee2cebfb56a9..c4d41c742266104851b23cf7850d1e764748f6a7 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/cleaner" "github.com/spf13/cobra" ) @@ -18,6 +19,7 @@ func runBridgeConfigure(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(backend.Close) target, err := promptTarget() if err != nil { diff --git a/commands/bridge_pull.go b/commands/bridge_pull.go index a90a533f03b088f52694829664e71527ea692d55..f7e2acf050bc02d134aed21c18aae4390e83600a 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/cleaner" "github.com/spf13/cobra" ) @@ -13,6 +14,7 @@ func runBridgePull(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(backend.Close) var b *core.Bridge diff --git a/commands/bridge_rm.go b/commands/bridge_rm.go index 2ebc17a7a47fce533a3db6f0940173ef3d1112b0..92f1b3c50713f242d03a78bf97a10293730ca91c 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/cleaner" "github.com/spf13/cobra" ) @@ -12,6 +13,7 @@ func runBridgeRm(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(backend.Close) err = bridge.RemoveBridges(backend, args[0]) if err != nil { diff --git a/commands/comment.go b/commands/comment.go index fc4b6a6b24d566db65ce874aedcea2f2d69a57c2..f644a23f35f1ec91711e6887316bee844d52413c 100644 --- a/commands/comment.go +++ b/commands/comment.go @@ -5,6 +5,7 @@ import ( "github.com/MichaelMure/git-bug/bug" "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" "github.com/MichaelMure/git-bug/util/colors" "github.com/MichaelMure/git-bug/util/text" @@ -17,6 +18,7 @@ func runComment(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(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..3266e9414f95595ec92f75e4fa65df167eba9666 100644 --- a/commands/comment_add.go +++ b/commands/comment_add.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" "github.com/MichaelMure/git-bug/input" "github.com/spf13/cobra" @@ -20,6 +21,7 @@ func runCommentAdd(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(backend.Close) b, args, err := _select.ResolveBug(backend, args) if err != nil { diff --git a/commands/deselect.go b/commands/deselect.go index a2e8d30d65057c6605c1478fcbcf74e555f5b829..d04e7bc1a5653927cb917b00bfc36f92d2464141 100644 --- a/commands/deselect.go +++ b/commands/deselect.go @@ -2,6 +2,7 @@ package commands import ( "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" "github.com/spf13/cobra" ) @@ -12,6 +13,7 @@ func runDeselect(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(backend.Close) err = _select.Clear(backend) if err != nil { diff --git a/commands/label.go b/commands/label.go index 0221701ca2ac62a0531d2a250b9943c0967b7851..75a034f84ffd283fa2188d44a03ea386252f4cdd 100644 --- a/commands/label.go +++ b/commands/label.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" "github.com/spf13/cobra" ) @@ -14,6 +15,7 @@ func runLabel(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(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..4fe6b3279366bd0e74ee5892b3c5b13ad84880bd 100644 --- a/commands/label_add.go +++ b/commands/label_add.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" "github.com/spf13/cobra" ) @@ -14,6 +15,7 @@ func runLabelAdd(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(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..b8cc9620854c874119370216f78d876422ce8f98 100644 --- a/commands/label_rm.go +++ b/commands/label_rm.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" "github.com/spf13/cobra" ) @@ -14,6 +15,7 @@ func runLabelRm(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(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..525303e00aeb31521286abf5ff0067b101e53098 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/cleaner" "github.com/spf13/cobra" ) @@ -13,6 +14,7 @@ func runLsLabel(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(backend.Close) labels := backend.ValidLabels() diff --git a/commands/ls.go b/commands/ls.go index 1a759a264769f959860ebc7e72afdc467f7b42b0..a25aa5829afc4f304b04525391bd8d4616d664d3 100644 --- a/commands/ls.go +++ b/commands/ls.go @@ -6,6 +6,7 @@ import ( "github.com/MichaelMure/git-bug/bug" "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/util/colors" "github.com/spf13/cobra" ) @@ -25,6 +26,7 @@ func runLsBug(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(backend.Close) var query *cache.Query if len(args) >= 1 { diff --git a/commands/pull.go b/commands/pull.go index 27c0953ba79ef8f8555495903a3483d4e04867ab..cfdcd7719576e72ce9d035a313826e09e5f851ce 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/cleaner" "github.com/spf13/cobra" ) @@ -24,6 +25,7 @@ func runPull(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(backend.Close) fmt.Println("Fetching remote ...") diff --git a/commands/push.go b/commands/push.go index 11282ada4c42e5b6da4389daf90113c8fc1ed87d..599b55574a66e6d7105d26633b9a18df9d5efb44 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/cleaner" "github.com/spf13/cobra" ) @@ -23,6 +24,7 @@ func runPush(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(backend.Close) stdout, err := backend.Push(remote) if err != nil { diff --git a/commands/select.go b/commands/select.go index 0c50d1a6bd73676df18f0d2bbc27e41b7bc9b631..a519d921da3fdb32f5a6e2dbcadb1f58bd2cdd92 100644 --- a/commands/select.go +++ b/commands/select.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" "github.com/spf13/cobra" ) @@ -19,6 +20,7 @@ func runSelect(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(backend.Close) prefix := args[0] diff --git a/commands/show.go b/commands/show.go index c41af02daa2252796fe9fc1f236cf55286f0176a..d6c05d87f6c5394de58e40fa96e919f844d2f2ae 100644 --- a/commands/show.go +++ b/commands/show.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" "github.com/MichaelMure/git-bug/util/colors" "github.com/spf13/cobra" @@ -17,6 +18,7 @@ func runShowBug(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(backend.Close) b, args, err := _select.ResolveBug(backend, args) if err != nil { diff --git a/commands/status.go b/commands/status.go index 6aed000a6fe42724734d6ec58b0d5864ffd727a7..bdd6dd4a8222ed67d99a7d7fa5054a6a697439e2 100644 --- a/commands/status.go +++ b/commands/status.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" "github.com/spf13/cobra" ) @@ -14,6 +15,7 @@ func runStatus(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(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..823d98b5d06aca153cd33838e3d153999eb42f44 100644 --- a/commands/status_close.go +++ b/commands/status_close.go @@ -2,6 +2,7 @@ package commands import ( "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" "github.com/spf13/cobra" ) @@ -12,6 +13,7 @@ func runStatusClose(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(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..0413d2b708a32c9f79bcebe46c084d1baf4e133e 100644 --- a/commands/status_open.go +++ b/commands/status_open.go @@ -2,6 +2,7 @@ package commands import ( "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" "github.com/spf13/cobra" ) @@ -12,6 +13,7 @@ func runStatusOpen(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(backend.Close) b, args, err := _select.ResolveBug(backend, args) if err != nil { diff --git a/commands/termui.go b/commands/termui.go index 8df5ba7c0bb9bcc37bbaff147a145db57b5bdf5f..d3dbc7330a3bf41b671493be48edef229ad1a8a6 100644 --- a/commands/termui.go +++ b/commands/termui.go @@ -2,6 +2,7 @@ package commands import ( "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/termui" "github.com/spf13/cobra" ) @@ -12,6 +13,7 @@ func runTermUI(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(backend.Close) return termui.Run(backend) } diff --git a/commands/title.go b/commands/title.go index 5d7237551d62486e123911ace45757d6bef73fb1..37db03ddeb3ad8b413d63db288c54df25e994542 100644 --- a/commands/title.go +++ b/commands/title.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" "github.com/spf13/cobra" ) @@ -14,6 +15,7 @@ func runTitle(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(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..de9a55ab916a822033966dacef309a7c5ab30d00 100644 --- a/commands/title_edit.go +++ b/commands/title_edit.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" "github.com/MichaelMure/git-bug/input" "github.com/spf13/cobra" @@ -19,6 +20,7 @@ func runTitleEdit(cmd *cobra.Command, args []string) error { return err } defer backend.Close() + cleaner.Register(backend.Close) b, args, err := _select.ResolveBug(backend, args) if err != nil { From a405c33ed695736495c80ea7d9a0f570775c17be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Wed, 24 Oct 2018 18:11:36 -0300 Subject: [PATCH 3/9] Renamed struct from t to Cleaner Co-Authored-By: auyer --- cleaner/cleaner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cleaner/cleaner.go b/cleaner/cleaner.go index 9bce4c03c6b1734e90d2b6307938dc6880ab818d..5937718b75af237bfa369cc6b6c3fc390cf1547c 100644 --- a/cleaner/cleaner.go +++ b/cleaner/cleaner.go @@ -7,7 +7,7 @@ import ( "syscall" ) -type t func() error +type Cleaner func() error var cleaners []t var inactive bool From 3fa967336077bdd5a3ee1de8d9c91235e498ef7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Wed, 24 Oct 2018 18:12:50 -0300 Subject: [PATCH 4/9] removed unnecessary empty string from print Co-Authored-By: auyer --- cleaner/cleaner.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cleaner/cleaner.go b/cleaner/cleaner.go index 5937718b75af237bfa369cc6b6c3fc390cf1547c..70d3d5e81ca42b13c261b4ff5741fdc5d3cfb424 100644 --- a/cleaner/cleaner.go +++ b/cleaner/cleaner.go @@ -21,7 +21,8 @@ func Register(f t) { ch := make(chan os.Signal, 1) signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) <-ch - fmt.Println("") + // Prevent un-terminated ^C character in terminal + fmt.Println() clean() os.Exit(1) }() From 4f4af4584f21f294cb77b4adbd4a2ae054b49be9 Mon Sep 17 00:00:00 2001 From: Rafael Passos Date: Wed, 24 Oct 2018 18:27:43 -0300 Subject: [PATCH 5/9] changing t type where used --- cleaner/cleaner.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cleaner/cleaner.go b/cleaner/cleaner.go index 70d3d5e81ca42b13c261b4ff5741fdc5d3cfb424..595c9fb01c285676d46b48710bba2b433d60c205 100644 --- a/cleaner/cleaner.go +++ b/cleaner/cleaner.go @@ -9,11 +9,11 @@ import ( type Cleaner func() error -var cleaners []t +var cleaners []Cleaner var inactive bool // Register a cleaner function. When a function is registered, the Signal watcher is started in a goroutine. -func Register(f t) { +func Register(f Cleaner) { cleaners = append(cleaners, f) if !inactive { inactive = false @@ -21,7 +21,7 @@ func Register(f t) { ch := make(chan os.Signal, 1) signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) <-ch - // Prevent un-terminated ^C character in terminal + // Prevent un-terminated ^C character in terminal fmt.Println() clean() os.Exit(1) @@ -35,5 +35,5 @@ func clean() { for _, f := range cleaners { _ = f() } - cleaners = []t{} + cleaners = []Cleaner{} } From f72b18496b508c23cee6fa99d764cfe59208515c Mon Sep 17 00:00:00 2001 From: Rafael Passos Date: Wed, 24 Oct 2018 18:36:39 -0300 Subject: [PATCH 6/9] moved cleaner function to util/interrupt --- commands/add.go | 4 ++-- commands/bridge.go | 4 ++-- commands/bridge_configure.go | 4 ++-- commands/bridge_pull.go | 4 ++-- commands/bridge_rm.go | 4 ++-- commands/comment.go | 4 ++-- commands/comment_add.go | 4 ++-- commands/deselect.go | 4 ++-- commands/label.go | 4 ++-- commands/label_add.go | 4 ++-- commands/label_rm.go | 4 ++-- commands/ls-labels.go | 4 ++-- commands/ls.go | 4 ++-- commands/pull.go | 4 ++-- commands/push.go | 4 ++-- commands/select.go | 4 ++-- commands/show.go | 4 ++-- commands/status.go | 4 ++-- commands/status_close.go | 4 ++-- commands/status_open.go | 4 ++-- commands/termui.go | 4 ++-- commands/title.go | 4 ++-- commands/title_edit.go | 4 ++-- {cleaner => util/interrupt}/cleaner.go | 6 +++--- 24 files changed, 49 insertions(+), 49 deletions(-) rename {cleaner => util/interrupt}/cleaner.go (74%) diff --git a/commands/add.go b/commands/add.go index e2650406b3be6165431e6a007ddc13719291d292..54ede1260daaa6f3e0cd9d12725ec9ca970a7a3b 100644 --- a/commands/add.go +++ b/commands/add.go @@ -4,8 +4,8 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/input" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -23,7 +23,7 @@ func runAddBug(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(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 ffba2b9d8c94d24b98f4734cd1b6cfae3ba7fdbd..a473776d7eaa1692e93d5ddb355c1ffe3da4b0fc 100644 --- a/commands/bridge.go +++ b/commands/bridge.go @@ -5,7 +5,7 @@ import ( "github.com/MichaelMure/git-bug/bridge" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -15,7 +15,7 @@ func runBridge(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(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 c4d41c742266104851b23cf7850d1e764748f6a7..ef499f1ff59b97dee3fe05cc98fe6706ca228c5a 100644 --- a/commands/bridge_configure.go +++ b/commands/bridge_configure.go @@ -9,7 +9,7 @@ import ( "github.com/MichaelMure/git-bug/bridge" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -19,7 +19,7 @@ func runBridgeConfigure(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(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 f7e2acf050bc02d134aed21c18aae4390e83600a..669a67133b0ddd6b26386c7bf23d7d0fda841368 100644 --- a/commands/bridge_pull.go +++ b/commands/bridge_pull.go @@ -4,7 +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/cleaner" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -14,7 +14,7 @@ func runBridgePull(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(backend.Close) + interrupt.RegisterCleaner(backend.Close) var b *core.Bridge diff --git a/commands/bridge_rm.go b/commands/bridge_rm.go index 92f1b3c50713f242d03a78bf97a10293730ca91c..172fc0d8d2ff214789e69f80565542e787679ed6 100644 --- a/commands/bridge_rm.go +++ b/commands/bridge_rm.go @@ -3,7 +3,7 @@ package commands import ( "github.com/MichaelMure/git-bug/bridge" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -13,7 +13,7 @@ func runBridgeRm(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(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 f644a23f35f1ec91711e6887316bee844d52413c..89378da30271c5ad746d545356fc059ecd72c9af 100644 --- a/commands/comment.go +++ b/commands/comment.go @@ -5,9 +5,9 @@ import ( "github.com/MichaelMure/git-bug/bug" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" "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" ) @@ -18,7 +18,7 @@ func runComment(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(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 3266e9414f95595ec92f75e4fa65df167eba9666..58408bc58115300d1fc5db36ce9b61dfd92fd371 100644 --- a/commands/comment_add.go +++ b/commands/comment_add.go @@ -4,9 +4,9 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" "github.com/MichaelMure/git-bug/input" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -21,7 +21,7 @@ func runCommentAdd(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(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 d04e7bc1a5653927cb917b00bfc36f92d2464141..210f158c1b7638d651c5a09aae669163ab8fe1dc 100644 --- a/commands/deselect.go +++ b/commands/deselect.go @@ -2,8 +2,8 @@ package commands import ( "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -13,7 +13,7 @@ func runDeselect(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(backend.Close) + interrupt.RegisterCleaner(backend.Close) err = _select.Clear(backend) if err != nil { diff --git a/commands/label.go b/commands/label.go index 75a034f84ffd283fa2188d44a03ea386252f4cdd..58ccc299f098c3a9638ace473f7f682474a91311 100644 --- a/commands/label.go +++ b/commands/label.go @@ -4,8 +4,8 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -15,7 +15,7 @@ func runLabel(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(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 4fe6b3279366bd0e74ee5892b3c5b13ad84880bd..f04ed7d67514f5e26b51e86aa1b72e4145e2537c 100644 --- a/commands/label_add.go +++ b/commands/label_add.go @@ -4,8 +4,8 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -15,7 +15,7 @@ func runLabelAdd(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(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 b8cc9620854c874119370216f78d876422ce8f98..36051ba197f1fd4fd72641ddba8cebba63c8d8a7 100644 --- a/commands/label_rm.go +++ b/commands/label_rm.go @@ -4,8 +4,8 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -15,7 +15,7 @@ func runLabelRm(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(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 525303e00aeb31521286abf5ff0067b101e53098..ef2c95bcc3fc866551165ccae4ecce3c4944de61 100644 --- a/commands/ls-labels.go +++ b/commands/ls-labels.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -14,7 +14,7 @@ func runLsLabel(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(backend.Close) + interrupt.RegisterCleaner(backend.Close) labels := backend.ValidLabels() diff --git a/commands/ls.go b/commands/ls.go index a25aa5829afc4f304b04525391bd8d4616d664d3..2f621bc5aaf2f387f30cdbf315c732e56547cbe4 100644 --- a/commands/ls.go +++ b/commands/ls.go @@ -6,8 +6,8 @@ import ( "github.com/MichaelMure/git-bug/bug" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/util/colors" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -26,7 +26,7 @@ func runLsBug(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(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 cfdcd7719576e72ce9d035a313826e09e5f851ce..67c2a3398a0a8a7fab90d6f16097d28c411c6c88 100644 --- a/commands/pull.go +++ b/commands/pull.go @@ -6,7 +6,7 @@ import ( "github.com/MichaelMure/git-bug/bug" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -25,7 +25,7 @@ func runPull(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(backend.Close) + interrupt.RegisterCleaner(backend.Close) fmt.Println("Fetching remote ...") diff --git a/commands/push.go b/commands/push.go index 599b55574a66e6d7105d26633b9a18df9d5efb44..0477be60a5545ac6a583aea0fbf67e25a263d254 100644 --- a/commands/push.go +++ b/commands/push.go @@ -5,7 +5,7 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -24,7 +24,7 @@ func runPush(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(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 a519d921da3fdb32f5a6e2dbcadb1f58bd2cdd92..cc6883540cb7f7525b7c65d4e600f5803b82a6aa 100644 --- a/commands/select.go +++ b/commands/select.go @@ -5,8 +5,8 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -20,7 +20,7 @@ func runSelect(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(backend.Close) + interrupt.RegisterCleaner(backend.Close) prefix := args[0] diff --git a/commands/show.go b/commands/show.go index d6c05d87f6c5394de58e40fa96e919f844d2f2ae..86c01a176e3c7e2e890c8bb43a54e254205d1b55 100644 --- a/commands/show.go +++ b/commands/show.go @@ -6,9 +6,9 @@ import ( "strings" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" "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" ) @@ -18,7 +18,7 @@ func runShowBug(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(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 bdd6dd4a8222ed67d99a7d7fa5054a6a697439e2..7928628a751eab19cf6b8daa1faeeb319266d060 100644 --- a/commands/status.go +++ b/commands/status.go @@ -4,8 +4,8 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -15,7 +15,7 @@ func runStatus(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(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 823d98b5d06aca153cd33838e3d153999eb42f44..2b4f9602c307fd9e7dc61a0f92fbb9784a876674 100644 --- a/commands/status_close.go +++ b/commands/status_close.go @@ -2,8 +2,8 @@ package commands import ( "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -13,7 +13,7 @@ func runStatusClose(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(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 0413d2b708a32c9f79bcebe46c084d1baf4e133e..5e3029e218313b7b9825878fa2dddc553fc913b8 100644 --- a/commands/status_open.go +++ b/commands/status_open.go @@ -2,8 +2,8 @@ package commands import ( "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -13,7 +13,7 @@ func runStatusOpen(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(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 d3dbc7330a3bf41b671493be48edef229ad1a8a6..4a029d6c256696544a6c0381c5013f16fdcb7c3a 100644 --- a/commands/termui.go +++ b/commands/termui.go @@ -2,8 +2,8 @@ package commands import ( "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/termui" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -13,7 +13,7 @@ func runTermUI(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(backend.Close) + interrupt.RegisterCleaner(backend.Close) return termui.Run(backend) } diff --git a/commands/title.go b/commands/title.go index 37db03ddeb3ad8b413d63db288c54df25e994542..c9157a7066201ebffbb66e55fcac934df459be5e 100644 --- a/commands/title.go +++ b/commands/title.go @@ -4,8 +4,8 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" "github.com/MichaelMure/git-bug/commands/select" + "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) @@ -15,7 +15,7 @@ func runTitle(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(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 de9a55ab916a822033966dacef309a7c5ab30d00..6bbd1b0a086f634f86f50ddbca5534d50af2a9cb 100644 --- a/commands/title_edit.go +++ b/commands/title_edit.go @@ -4,9 +4,9 @@ import ( "fmt" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/cleaner" "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,7 +20,7 @@ func runTitleEdit(cmd *cobra.Command, args []string) error { return err } defer backend.Close() - cleaner.Register(backend.Close) + interrupt.RegisterCleaner(backend.Close) b, args, err := _select.ResolveBug(backend, args) if err != nil { diff --git a/cleaner/cleaner.go b/util/interrupt/cleaner.go similarity index 74% rename from cleaner/cleaner.go rename to util/interrupt/cleaner.go index 595c9fb01c285676d46b48710bba2b433d60c205..dfb8e25b4599064047a7ece62ef404adee764e23 100644 --- a/cleaner/cleaner.go +++ b/util/interrupt/cleaner.go @@ -1,4 +1,4 @@ -package cleaner +package interrupt import ( "fmt" @@ -12,8 +12,8 @@ type Cleaner func() error var cleaners []Cleaner var inactive bool -// Register a cleaner function. When a function is registered, the Signal watcher is started in a goroutine. -func Register(f Cleaner) { +// 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) { cleaners = append(cleaners, f) if !inactive { inactive = false From 85032d4cdc43b23da620d9e6b81cb961127566d5 Mon Sep 17 00:00:00 2001 From: Rafael Passos Date: Wed, 24 Oct 2018 18:55:02 -0300 Subject: [PATCH 7/9] Inverted boolean check --- util/interrupt/cleaner.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/util/interrupt/cleaner.go b/util/interrupt/cleaner.go index dfb8e25b4599064047a7ece62ef404adee764e23..58dd6b070c86cafbdf55a6b194c6511d53b804be 100644 --- a/util/interrupt/cleaner.go +++ b/util/interrupt/cleaner.go @@ -7,16 +7,17 @@ import ( "syscall" ) +// Cleaner type referes to a function with no inputs that returns an error type Cleaner func() error var cleaners []Cleaner -var inactive bool +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) { cleaners = append(cleaners, f) - if !inactive { - inactive = false + if !active { + active = true go func() { ch := make(chan os.Signal, 1) signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) From 8f012e2cab7ddc8ce36fac74be824a207d9b0d97 Mon Sep 17 00:00:00 2001 From: Rafael Passos Date: Wed, 24 Oct 2018 19:06:10 -0300 Subject: [PATCH 8/9] RegisterCleaner now uses Variadic input + tests --- util/interrupt/cleaner.go | 28 +++++++++++++++------------- util/interrupt/cleaner_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 util/interrupt/cleaner_test.go diff --git a/util/interrupt/cleaner.go b/util/interrupt/cleaner.go index 58dd6b070c86cafbdf55a6b194c6511d53b804be..3f6c3afb8800bf4dd4900b6c929fb646081e64b9 100644 --- a/util/interrupt/cleaner.go +++ b/util/interrupt/cleaner.go @@ -14,19 +14,21 @@ 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) { - cleaners = append(cleaners, f) - 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() - clean() - os.Exit(1) - }() +func RegisterCleaner(f ...Cleaner) { + for _, fn := range f { + cleaners = append(cleaners, fn) + 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() + clean() + os.Exit(1) + }() + } } } diff --git a/util/interrupt/cleaner_test.go b/util/interrupt/cleaner_test.go new file mode 100644 index 0000000000000000000000000000000000000000..f839c0dcbf3175e8111118be978075ca8ba2899a --- /dev/null +++ b/util/interrupt/cleaner_test.go @@ -0,0 +1,33 @@ +package interrupt + +import ( + "testing" +) + +func TestRegister(t *testing.T) { + active = true // this prevents goroutine from being started during the tests + + f := func() error { + return nil + } + f2 := func() error { + return nil + } + f3 := func() error { + return nil + } + RegisterCleaner(f) + RegisterCleaner(f2, f3) + count := 0 + for _, fn := range cleaners { + errt := fn() + count++ + if errt != nil { + t.Fatalf("bad err value") + } + } + if count != 3 { + t.Fatalf("different number of errors") + } + +} From 31cbddd35b15519a2f8ffa8d93e6456323ea80ee Mon Sep 17 00:00:00 2001 From: Rafael Passos Date: Thu, 25 Oct 2018 18:07:02 -0300 Subject: [PATCH 9/9] Iverted order, moved print, and new tests --- util/interrupt/cleaner.go | 19 +++++++++++----- util/interrupt/cleaner_test.go | 41 ++++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/util/interrupt/cleaner.go b/util/interrupt/cleaner.go index 3f6c3afb8800bf4dd4900b6c929fb646081e64b9..76c9d04dcdf1218aa2bfd37b5a3411767856e568 100644 --- a/util/interrupt/cleaner.go +++ b/util/interrupt/cleaner.go @@ -16,7 +16,7 @@ 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(cleaners, fn) + cleaners = append([]Cleaner{fn}, cleaners...) if !active { active = true go func() { @@ -25,18 +25,25 @@ func RegisterCleaner(f ...Cleaner) { <-ch // Prevent un-terminated ^C character in terminal fmt.Println() - clean() + fmt.Println("Cleaning") + errl := Clean() + for _, err := range errl { + fmt.Println(err) + } os.Exit(1) }() } } } -// Clean invokes all registered cleanup functions -func clean() { - fmt.Println("Cleaning") +// Clean invokes all registered cleanup functions, and returns a list of errors, if they exist. +func Clean() (errorlist []error) { for _, f := range cleaners { - _ = f() + 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 index f839c0dcbf3175e8111118be978075ca8ba2899a..c4e5c9b3c213a2ce6c577df362b5641aeb24cd26 100644 --- a/util/interrupt/cleaner_test.go +++ b/util/interrupt/cleaner_test.go @@ -1,33 +1,50 @@ package interrupt import ( + "errors" "testing" ) -func TestRegister(t *testing.T) { +// 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 nil + return errors.New("X") } f2 := func() error { - return nil + return errors.New("Y") } f3 := func() error { return nil } RegisterCleaner(f) RegisterCleaner(f2, f3) - count := 0 - for _, fn := range cleaners { - errt := fn() - count++ - if errt != nil { - t.Fatalf("bad err value") - } + // count := 0 + + errl := Clean() + if len(errl) != 2 { + t.Fatalf("unexpected error count") } - if count != 3 { - t.Fatalf("different number of errors") + 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") + } }