diff --git a/main.go b/main.go index e790e9d4..499a71b0 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,8 @@ import ( "os" "errors" "os/exec" + "os/signal" + "syscall" ) type Exchange struct { @@ -30,7 +32,30 @@ type Bot struct { var bot Bot +func HandleInterrupt() { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + go func() { + sig := <-c + log.Printf("Captured %v.", sig) + Shutdown() + log.Println("Exiting.") + os.Exit(1) + }() +} + +func Shutdown() { + err := SaveConfig() + + if err != nil { + log.Println("Unable to save config.") + } + + log.Println("Config file saved successfully.") +} + func main() { + HandleInterrupt() log.Println("Loading config file config.json..") err := errors.New("") @@ -468,4 +493,4 @@ func main() { cmd.Stdout = os.Stdout cmd.Run() } -} +} \ No newline at end of file