git-webhook-ci/main.go

56 lines
1.2 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
)
var watcherIface pkgwatcher.WatcherIface
2023-02-25 22:32:10 +01:00
func main() {
isProd := os.Getenv("ENV") == "prod"
config := cfg.NewConfig(isProd)
watcherIface = watcher.NewWatcher(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)
go func(errors chan error) {
2023-02-25 22:32:10 +01:00
for {
select {
2023-02-26 10:59:33 +01:00
case <-signalChan:
os.Exit(1)
case err := <-errors:
if err != nil {
log.Fatalf("watcherIface.Monitor: %s\n", err)
2023-02-25 22:32:10 +01:00
return
}
}
}
}(errors)
2023-02-25 22:32:10 +01:00
2023-02-26 10:59:33 +01:00
// TODO: Improve this: Block main goroutine forever.
2023-02-25 22:32:10 +01:00
<-make(chan struct{})
}