git-webhook-ci/main.go

63 lines
1.3 KiB
Go

package main
import (
"flag"
"log"
"os"
"os/signal"
"syscall"
"gitea.urkob.com/urko/git-webhook-ci/internal/watcher"
"gitea.urkob.com/urko/git-webhook-ci/kit/config"
"gitea.urkob.com/urko/git-webhook-ci/pkg"
)
var (
watcherIface pkg.WatcherIface
notifierIface pkg.NotifyIface
)
func main() {
envFilePath := ""
if os.Getenv("ENV") != "prod" {
flag.StringVar(&envFilePath, ".env path", "/", "provide .env path file as an absolute path")
flag.Parse()
}
config, err := config.LoadConfig(envFilePath)
if err != nil {
panic(err)
}
notifierIface = watcher.NewNotifier()
watcherIface = watcher.NewWatcher(notifierIface, pkg.Deploy)
defer func() {
if err := watcherIface.Close(); err != nil {
log.Fatalf("watcherIface.Close: %s\n", err)
}
}()
if err := watcherIface.Monitor(config.FileToWatchPath); err != nil {
log.Fatalf("watcherIface.Monitor: %s\n", err)
}
errors := make(chan error)
watcherIface.Listen(config.ScriptBinaryPath, config.WebhookScriptPath, errors)
// Handle termination on ctrl+signalChan
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
for {
select {
case <-signalChan:
os.Exit(1)
case err := <-errors:
if err != nil {
log.Printf("watcherIface.Monitor: %s\n", err)
continue
}
}
}
}