refactor: use internal/pkg to refactor structure
This commit is contained in:
65
internal/watcher/watcher.go
Normal file
65
internal/watcher/watcher.go
Normal file
@@ -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()
|
||||
}
|
||||
Reference in New Issue
Block a user