From c54366a21ed95c4b678075f3406a08cc2719af48 Mon Sep 17 00:00:00 2001 From: Urko Date: Sun, 26 Feb 2023 09:19:27 +0100 Subject: [PATCH] refactor: use internal/pkg to refactor structure --- go.mod | 2 +- internal/watcher/watcher.go | 65 ++++++++++++++++ main.go | 150 ++++++++++++++++++++++-------------- pkg/watcher/watcher.go | 37 +++++++++ 4 files changed, 197 insertions(+), 57 deletions(-) create mode 100644 internal/watcher/watcher.go create mode 100644 pkg/watcher/watcher.go diff --git a/go.mod b/go.mod index 3ba4b49..2dc0182 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module filelistener +module gitea.urkob.com/urko/git-webhook-ci go 1.19 diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go new file mode 100644 index 0000000..50299bc --- /dev/null +++ b/internal/watcher/watcher.go @@ -0,0 +1,65 @@ +package watcher + +import ( + "errors" + "log" + + pkgwatcher "gitea.urkob.com/urko/git-webhook-ci/pkg/watcher" + "github.com/fsnotify/fsnotify" +) + +type watcher struct { + fswatcher *fsnotify.Watcher + deploy pkgwatcher.DeployFunc +} + +func NewWatcher(deploy pkgwatcher.DeployFunc) *watcher { + wt, err := fsnotify.NewWatcher() + if err != nil { + log.Printf("fsnotify.NewWatcher: %s\n", err) + return nil + } + return &watcher{ + fswatcher: wt, + deploy: deploy, + } +} + +func (w *watcher) Monitor(path string) error { + return w.fswatcher.Add(path) +} + +// Start listening for events. +func (w *watcher) Listen(binaryPath, scriptPath string, outputErr chan<- error) { + go func(events chan fsnotify.Event, errChan chan error, outputErr chan<- error) { + for { + select { + case event, ok := <-events: + if !ok { + log.Printf("!ok <-events \n") + outputErr <- errors.New("!ok <-events") + return + } + if !event.Has(fsnotify.Write) { + log.Printf("is not Write: %s\n", event.Name) + continue + } + + if err := w.deploy(binaryPath, scriptPath); err != nil { + log.Printf("deploy: %s\n", err) + continue + } + case err, ok := <-errChan: + if !ok { + log.Printf("!ok <-errors\n") + return + } + log.Printf("<-errors: %s\n", err) + } + } + }(w.fswatcher.Events, w.fswatcher.Errors, outputErr) +} + +func (w *watcher) Close() error { + return w.fswatcher.Close() +} diff --git a/main.go b/main.go index a349009..a5bb40d 100644 --- a/main.go +++ b/main.go @@ -1,81 +1,119 @@ package main import ( - "fmt" "log" "os" - "os/exec" + "os/signal" + "syscall" - "filelistener/cfg" - - "github.com/fsnotify/fsnotify" + "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" ) +var watcherIface pkgwatcher.WatcherIface + func main() { isProd := os.Getenv("ENV") == "prod" config := cfg.NewConfig(isProd) - // Create new watcher. - watcher, err := fsnotify.NewWatcher() - if err != nil { - log.Fatal(err) - } - defer watcher.Close() + watcherIface = watcher.NewWatcher(pkgwatcher.Deploy) - // 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 - } - - 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) - } + defer func() { + if err := watcherIface.Close(); err != nil { + log.Fatalf("watcherIface.Close: %s\n", err) } }() - // Add a path. - err = watcher.Add(config.FileToWatchPath) - if err != nil { - log.Fatal(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) + signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM) + + go func() { + <-signalChan + os.Exit(1) + }() + + go func(errors chan error) { + for { + select { + case err := <-errors: + if err != nil { + log.Fatalf("watcherIface.Monitor: %s\n", err) + return + } + } + } + }(errors) + // Block main goroutine forever. <-make(chan struct{}) + + // watcher, err := fsnotify.NewWatcher() + // if err != nil { + // log.Fatal(err) + // } + // defer watcher.Close() + + // // 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 + // } + + // 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) + // } + + // // Block main goroutine forever. + // <-make(chan struct{}) } -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 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 +// 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) - } +// if err := cmd.Run(); err != nil { +// return fmt.Errorf("cmd.Run %s", err) +// } - return nil -} +// return nil +// } diff --git a/pkg/watcher/watcher.go b/pkg/watcher/watcher.go new file mode 100644 index 0000000..276d633 --- /dev/null +++ b/pkg/watcher/watcher.go @@ -0,0 +1,37 @@ +package watcher + +import ( + "fmt" + "log" + "os" + "os/exec" +) + +type WatcherIface interface { + Monitor(path string) error + Listen(binaryPath, scriptPath string, outputErr chan<- error) + Close() error +} + +type DeployFunc func(binaryPath, scriptPath string) error + +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 +}