2023-02-25 22:32:10 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
2023-02-26 09:19:27 +01:00
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2023-02-25 22:32:10 +01:00
|
|
|
|
2023-02-26 09:19:27 +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 09:19:27 +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)
|
|
|
|
|
2023-02-26 09:19:27 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-02-26 09:19:27 +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)
|
2023-02-26 09:19:27 +01:00
|
|
|
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
|
|
|
}
|