add cleanup on exit

This commit is contained in:
Leo Antunes 2018-08-22 00:19:42 +02:00
parent 03a5f533ca
commit 97d926b432

16
main.go
View file

@ -18,7 +18,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"os"
"os/signal"
"strings"
"syscall"
docker "docker.io/go-docker"
@ -48,6 +51,13 @@ func main() {
log.SetLevel(logLevelMap[strings.ToLower(config.LogLevel)])
quitSig := make(chan os.Signal)
signal.Notify(quitSig, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-quitSig
cleanup()
}()
client, err := docker.NewEnvClient()
if err != nil {
log.Fatalf("error starting docker client: %s", err)
@ -60,3 +70,9 @@ func main() {
syncAndListenForEvents(client)
}
}
func cleanup() {
log.Info("cleaning up hosts file")
writeToEtcHosts(ipsToNamesMap{})
os.Exit(0)
}