# README
cmd
Go library to encapsulate the signal handling pattern for termination signal so that I can properly clean up.
package main
import (
"fmt"
"time"
"github.com/gdey/cmd"
)
func cleanup(s string) {
fmt.Printf("Cleaning up: %s!\n", s)
}
func main() {
defer cmd.New().Complete()
// Main code here
fmt.Println("Runing 3 times.")
for i := 0; i < 3; i++ {
fmt.Println("Going to nap for a second.")
select {
case <-time.After(5 * time.Second):
fmt.Println("Ahhh that was a good nap!")
case <-cmd.Cancelled():
fmt.Println("Ho! I got Ctr-C")
cleanup("for and return")
return
}
fmt.Println("Going sleep some more")
<-time.After(2 * time.Second)
fmt.Println("Good sleep!")
if cmd.IsCancelled() {
fmt.Println("Ctr-C got called.")
cleanup("for and break")
break
}
// do some chunk off work.
<-time.After(2 * time.Second)
}
fmt.Println("All done!")
}
# Functions
Cancelled is provided for use in select statments.
Complete is a blocking call that should be the last call in your main function.
IsCancelled is provided for use in if and for blocks, this can be used to check to see if a termination signal has been send, and to the excuate appropriate logic as needed.
New initilizes and setups up the global context.
NewContext initilizes and setups up the context.
OnComplete adds a set of functions that are called just before complete; exists.
Signal provides one the ability to introspect which signal was actually send.