git-webhook-ci/main.go

54 lines
1.1 KiB
Go
Raw Normal View History

2023-02-25 22:32:10 +01:00
package main
import (
"log"
"os"
"os/signal"
"syscall"
2023-02-25 22:32:10 +01:00
"gitea.urkob.com/urko/git-webhook-ci/cfg"
"gitea.urkob.com/urko/git-webhook-ci/internal/watcher"
pkgwatcher "gitea.urkob.com/urko/git-webhook-ci/pkg/watcher"
2023-02-25 22:32:10 +01:00
)
2023-02-26 16:59:20 +01:00
var (
watcherIface pkgwatcher.WatcherIface
notifierIface pkgwatcher.NotifyIface
)
2023-02-25 22:32:10 +01:00
func main() {
isProd := os.Getenv("ENV") == "prod"
config := cfg.NewConfig(isProd)
2023-02-26 16:59:20 +01:00
notifierIface = watcher.NewNotifier()
watcherIface = watcher.NewWatcher(notifierIface, pkgwatcher.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)
2023-02-25 22:32:10 +01:00
}
errors := make(chan error)
watcherIface.Listen(config.ScriptBinaryPath, config.WebhookScriptPath, errors)
// Handle termination on ctrl+signalChan
2023-02-26 10:59:33 +01:00
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
2023-02-26 16:19:12 +01:00
for {
select {
case <-signalChan:
os.Exit(1)
case err := <-errors:
if err != nil {
log.Printf("watcherIface.Monitor: %s\n", err)
continue
2023-02-25 22:32:10 +01:00
}
}
2023-02-26 16:19:12 +01:00
}
2023-02-25 22:32:10 +01:00
}