Skip to content

Commit

Permalink
Convert 'watch' to new Watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
malcolmholmes committed Apr 30, 2024
1 parent 51ae143 commit e60ec58
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 77 deletions.
35 changes: 6 additions & 29 deletions cmd/grr/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,26 +276,6 @@ func applyCmd(registry grizzly.Registry) *cli.Command {
return initialiseCmd(cmd, &opts)
}

type jsonnetWatchParser struct {
resourcePath string
registry grizzly.Registry
resourceKind string
folderUID string
targets []string
jsonnetPaths []string
}

func (p *jsonnetWatchParser) Name() string {
return p.resourcePath
}

func (p *jsonnetWatchParser) Parse() (grizzly.Resources, error) {
return grizzly.DefaultParser(p.registry, p.targets, p.jsonnetPaths, grizzly.ParserContinueOnError(true)).Parse(p.resourcePath, grizzly.ParserOptions{
DefaultResourceKind: p.resourceKind,
DefaultFolderUID: p.folderUID,
})
}

func watchCmd(registry grizzly.Registry) *cli.Command {
cmd := &cli.Command{
Use: "watch <dir-to-watch> <resource-path>",
Expand All @@ -315,20 +295,17 @@ func watchCmd(registry grizzly.Registry) *cli.Command {
return err
}
targets := currentContext.GetTargets(opts.Targets)
parser := &jsonnetWatchParser{
resourcePath: args[1],
registry: registry,
resourceKind: resourceKind,
folderUID: folderUID,
targets: targets,
jsonnetPaths: opts.JsonnetPaths,
}

watchDir := args[0]

trailRecorder := grizzly.NewWriterRecorder(os.Stdout, grizzly.EventToPlainText)

return grizzly.Watch(registry, watchDir, parser, trailRecorder)
parser := grizzly.DefaultParser(registry, targets, opts.JsonnetPaths, grizzly.ParserContinueOnError(true))
parserOpts := grizzly.ParserOptions{
DefaultResourceKind: resourceKind,
DefaultFolderUID: folderUID,
}
return grizzly.Watch(registry, watchDir, parser, parserOpts, trailRecorder)
}
return initialiseCmd(cmd, &opts)
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/grizzly/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ func (w *Watcher) Watch(path string) error {
}()
return nil
}

func (w *Watcher) Wait() error {
done := make(chan bool)
<-done
return nil
}
67 changes: 19 additions & 48 deletions pkg/grizzly/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"text/tabwriter"

Expand All @@ -18,7 +16,6 @@ import (
"github.com/pmezard/go-difflib/difflib"
log "github.com/sirupsen/logrus"
terminal "golang.org/x/term"
"gopkg.in/fsnotify.v1"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -497,58 +494,32 @@ type WatchParser interface {

// Watch watches a directory for changes then pushes Jsonnet resource to endpoints
// when changes are noticed.
func Watch(registry Registry, watchDir string, parser WatchParser, trailRecorder eventsRecorder) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
defer watcher.Close()

done := make(chan bool)
go func() {
log.Info("Watching for changes")
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
log.Info("Changes detected. Applying ", parser.Name())
resources, err := parser.Parse()
if err != nil {
log.Error("Error: ", err)
}
err = Apply(registry, resources, false, trailRecorder) // TODO?
if err != nil {
log.Error("Error: ", err)
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Error("error: ", err)
}
}
}()
func Watch(registry Registry, watchDir string, parser Parser, parserOpts ParserOptions, trailRecorder eventsRecorder) error {

Check failure on line 497 in pkg/grizzly/workflow.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)

if err := filepath.WalkDir(watchDir, func(path string, d fs.DirEntry, err error) error {
updateWatchedResource := func(path string) error {
log.Info("Changes detected. Applying ", path)
resources, err := parser.Parse(path, parserOpts)
if err != nil {
return err
log.Error("Error: ", err)
}

if d.IsDir() {
return watcher.Add(path)
err = Apply(registry, resources, false, trailRecorder) // TODO?
if err != nil {
log.Error("Error: ", err)
}

return nil
}); err != nil {
}
watcher, err := NewWatcher(updateWatchedResource)
if err != nil {
return err
}
err = watcher.Watch(watchDir)
if err != nil {
return err
}
err = watcher.Wait()
if err != nil {
return err
}

<-done

return nil
}

Expand Down

0 comments on commit e60ec58

Please sign in to comment.