package prep import ( "fmt" "os" "time" "go.wit.com/lib/config" "go.wit.com/log" ) // since we know when the command starts (duh, this parses os.Args) // this is a convienent way to provide a standard exit format back // to the shell that also has built in timing! // also, it supports a custom Exit() back to your application func (pb *Auto) GoodExit(msg string) { go ExitWatchdog() if myAuto.appExit != nil { myAuto.appExit() } dur := time.Since(pb.Ctime.AsTime()) log.Infof("%s: %s (%s)\n", pb.Argname, msg, config.FormatDuration(dur)) os.Exit(0) } func (pb *Auto) BadExit(msg string, err error) { go ExitWatchdog() if myAuto.appExit != nil { myAuto.appExit() } if err != nil { log.Info(err) } dur := time.Since(pb.Ctime.AsTime()) log.Infof("%s: %s (%s)\n", pb.Argname, msg, config.FormatDuration(dur)) os.Exit(-1) } // this code doesn't need to be this complicated. I put it here as reference code for myself so I could remember where it is. func ExitWatchdog() { dog := time.NewTicker(5 * time.Second) defer dog.Stop() dogchan := make(chan bool) /* // this example would exit/destroy the ticker in 10 seconds go func() { time.Sleep(10 * time.Second) done <- true }() */ for { select { case <-dogchan: fmt.Println("Done!") return case t := <-dog.C: _ = t log.Info("argv.Exit() watchdog: stalled in", myAuto.appName+".Exit()") // h.Scan() } } }