11 // TrapSignal catches the SIGTERM and executes cb function. After that it exits
13 func TrapSignal(cb func()) {
14 c := make(chan os.Signal, 1)
15 signal.Notify(c, os.Interrupt, syscall.SIGTERM)
18 fmt.Printf("captured %v, exiting...\n", sig)
33 func EnsureDir(dir string, mode os.FileMode) error {
34 if _, err := os.Stat(dir); os.IsNotExist(err) {
35 err := os.MkdirAll(dir, mode)
37 return fmt.Errorf("Could not create directory %v. %v", dir, err)
43 func FileExists(filePath string) bool {
44 _, err := os.Stat(filePath)
45 return !os.IsNotExist(err)
48 func WriteFile(filePath string, contents []byte, mode os.FileMode) error {
49 return ioutil.WriteFile(filePath, contents, mode)
52 func MustWriteFile(filePath string, contents []byte, mode os.FileMode) {
53 err := WriteFile(filePath, contents, mode)
55 Exit(Fmt("MustWriteFile failed: %v", err))