interrupt: minor cleaning

This commit is contained in:
Michael Muré 2018-10-27 11:51:50 +02:00
parent 8be1078dd6
commit 5653ae98e0
No known key found for this signature in database
GPG Key ID: A4457C029293126F
2 changed files with 7 additions and 8 deletions

View File

@ -7,13 +7,13 @@ import (
"syscall" "syscall"
) )
// Cleaner type referes to a function with no inputs that returns an error // Cleaner type refers to a function with no inputs that returns an error
type Cleaner func() error type Cleaner func() error
var cleaners []Cleaner var cleaners []Cleaner
var active = false var active = false
// RegisterCleaner is responsible for regisreting a cleaner function. When a function is registered, the Signal watcher is started in a goroutine. // RegisterCleaner is responsible for registering a cleaner function. When a function is registered, the Signal watcher is started in a goroutine.
func RegisterCleaner(f ...Cleaner) { func RegisterCleaner(f ...Cleaner) {
for _, fn := range f { for _, fn := range f {
cleaners = append([]Cleaner{fn}, cleaners...) cleaners = append([]Cleaner{fn}, cleaners...)
@ -25,8 +25,7 @@ func RegisterCleaner(f ...Cleaner) {
<-ch <-ch
// Prevent un-terminated ^C character in terminal // Prevent un-terminated ^C character in terminal
fmt.Println() fmt.Println()
fmt.Println("Cleaning") errl := clean()
errl := Clean()
for _, err := range errl { for _, err := range errl {
fmt.Println(err) fmt.Println(err)
} }
@ -36,8 +35,8 @@ func RegisterCleaner(f ...Cleaner) {
} }
} }
// Clean invokes all registered cleanup functions, and returns a list of errors, if they exist. // clean invokes all registered cleanup functions, and returns a list of errors, if they exist.
func Clean() (errorlist []error) { func clean() (errorlist []error) {
for _, f := range cleaners { for _, f := range cleaners {
err := f() err := f()
if err != nil { if err != nil {

View File

@ -22,7 +22,7 @@ func TestRegisterAndErrorAtCleaning(t *testing.T) {
RegisterCleaner(f2, f3) RegisterCleaner(f2, f3)
// count := 0 // count := 0
errl := Clean() errl := clean()
if len(errl) != 2 { if len(errl) != 2 {
t.Fatalf("unexpected error count") t.Fatalf("unexpected error count")
} }
@ -43,7 +43,7 @@ func TestRegisterAndClean(t *testing.T) {
} }
RegisterCleaner(f, f2) RegisterCleaner(f, f2)
errl := Clean() errl := clean()
if len(errl) != 0 { if len(errl) != 0 {
t.Fatalf("unexpected error count") t.Fatalf("unexpected error count")
} }