git-webhook-ci/main.go

120 lines
2.5 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
signalChan := make(chan os.Signal)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
2023-02-25 22:32:10 +01:00
go func() {
<-signalChan
os.Exit(1)
}()
go func(errors chan error) {
2023-02-25 22:32:10 +01:00
for {
select {
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
// Block main goroutine forever.
<-make(chan struct{})
// watcher, err := fsnotify.NewWatcher()
// if err != nil {
// log.Fatal(err)
// }
// defer watcher.Close()
2023-02-25 22:32:10 +01:00
// // Start listening for events.
// go func() {
// for {
// select {
// case event, ok := <-watcher.Events:
// if !ok {
// log.Printf("<-watcher.Events: %s\n", err)
// return
// }
// if !event.Has(fsnotify.Write) {
// log.Printf("is not Write: %s\n", event.Name)
// continue
// }
2023-02-25 22:32:10 +01:00
// if err := deploy(config.ScriptBinaryPath, config.WebhookScriptPath); err != nil {
// log.Printf("deploy: %s\n", err)
// continue
// }
// case err, ok := <-watcher.Errors:
// if !ok {
// return
// }
// log.Printf("<-watcher.Errors: %s\n", err)
// }
// }
// }()
// if err = watcher.Add(config.FileToWatchPath); err != nil {
// log.Fatal(err)
// }
2023-02-25 22:32:10 +01:00
// // Block main goroutine forever.
// <-make(chan struct{})
2023-02-25 22:32:10 +01:00
}
// func deploy(binaryPath, scriptPath string) error {
// err := execute(binaryPath, scriptPath)
// if err != nil {
// return fmt.Errorf("execute: %s", err)
// }
// log.Println("deploy done")
// return nil
// }
// func execute(binaryPath, scriptPath string) error {
// cmd := exec.Command(binaryPath, scriptPath)
// cmd.Stdout = os.Stdout
// cmd.Stderr = os.Stderr
// if err := cmd.Run(); err != nil {
// return fmt.Errorf("cmd.Run %s", err)
// }
// return nil
// }