Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto watch jpath #575

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/grr/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func serveCmd(registry grizzly.Registry) *cli.Command {
server.SetContext(currentContext.Name)
server.SetFormatting(onlySpec, format)
if opts.Watch {
server.Watch(watchPaths)
server.Watch(watchPaths, opts.JsonnetPaths)
if opts.WatchScript != "" {
server.WatchScript(opts.WatchScript)
}
Expand Down
57 changes: 55 additions & 2 deletions pkg/grizzly/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"net/http/httputil"
"os"
"os/exec"
"path/filepath"
"runtime"
"slices"
"strings"

"github.com/go-chi/chi"
Expand Down Expand Up @@ -37,6 +39,7 @@ type Server struct {
UserAgent string
ResourcePath string
WatchPaths []string
jpath []string
watchScript string
OnlySpec bool
OutputFormat string
Expand Down Expand Up @@ -89,9 +92,29 @@ func (s *Server) OpenBrowser() {
s.openBrowser = true
}

func (s *Server) Watch(watchPaths []string) {
func (s *Server) Watch(watchPaths, jpath []string) {
s.watch = true
s.WatchPaths = watchPaths

for _, path := range watchPaths {
stat, err := os.Stat(path)
if err != nil {
log.Fatal(err)
}
if !stat.IsDir() {
parent := filepath.Dir(path)
for _, p := range jpath {
log.WithField("path", p).Debug("checking if jpath exists")
if !filepath.IsAbs(p) {
p = filepath.Join(parent, p)
}
if _, err := os.Stat(p); err == nil {
log.WithField("path", p).Debug("adding jpath to watched files")
s.jpath = append(s.jpath, p)
}
}
}
}
}

func (s *Server) WatchScript(script string) {
Expand Down Expand Up @@ -237,7 +260,8 @@ func (s *Server) Start() error {
if err != nil {
return err
}
for _, path := range s.WatchPaths {
watchPaths := slices.Concat(s.WatchPaths, s.jpath)
for _, path := range watchPaths {
err = watcher.Add(path)
if err != nil {
return err
Expand Down Expand Up @@ -312,11 +336,30 @@ func (s *Server) updateWatchedResource(name string) error {
log.Infof("[watcher] Skipping %s", uerr.File)
return nil
}

if errors.As(err, &ParseError{}) && s.fileInJpath(name) {
log.Infof("[watcher] updating all dashboards due to changes in jpath")
return s.updateWatchedResources()
}

if err != nil {
log.Errorf("[watcher] Error: %s", err)
return err
}
s.reloadDashboad(resources)
return nil
}

func (s *Server) fileInJpath(name string) bool {
for _, jpath := range s.jpath {
if strings.HasPrefix(name, jpath) {
return true
}
}
return false
}

func (s *Server) reloadDashboad(resources Resources) {
for _, resource := range resources.AsList() {
handler, err := s.Registry.GetHandler(resource.Kind())
if err != nil {
Expand All @@ -329,6 +372,16 @@ func (s *Server) updateWatchedResource(name string) error {
livereload.ReloadDashboard(resource.Name())
}
}
}

func (s *Server) updateWatchedResources() error {
for _, name := range s.WatchPaths {
resources, err := s.ParseResources(name)
if err != nil {
return err
}
s.reloadDashboad(resources)
}
return nil
}

Expand Down
Loading