git-webhook-ci/internal/watcher/watcher.go

76 lines
1.6 KiB
Go

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
}
var (
errEventsClosedChan = errors.New("events is closed")
errErrorsClosedChan = errors.New("errors is closed")
)
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.Println(errEventsClosedChan)
outputErr <- errEventsClosedChan
return
}
if !event.Has(fsnotify.Write) {
log.Printf("is not Write: %s\n", event.Name)
continue
}
log.Printf("event: %s | op: %s \n", event.Name, event.Op)
if err := w.deploy(binaryPath, scriptPath); err != nil {
log.Printf("deploy: %s\n", err)
outputErr <- err
continue
}
case err, ok := <-errChan:
if !ok {
log.Println(errErrorsClosedChan)
outputErr <- errErrorsClosedChan
return
}
log.Printf("<-errors: %s\n", err)
outputErr <- err
}
}
}(w.fswatcher.Events, w.fswatcher.Errors, outputErr)
}
func (w *watcher) Close() error {
return w.fswatcher.Close()
}