diff --git a/realize.go b/realize.go index 0fdd581..2ec4f41 100644 --- a/realize.go +++ b/realize.go @@ -38,6 +38,7 @@ func main() { &cli.BoolFlag{Name: "install", Aliases: []string{"i"}, Value: false, Usage: "Enable go install"}, &cli.BoolFlag{Name: "build", Aliases: []string{"b"}, Value: false, Usage: "Enable go build"}, &cli.BoolFlag{Name: "run", Aliases: []string{"nr"}, Value: false, Usage: "Enable go run"}, + &cli.BoolFlag{Name: "legacy", Aliases: []string{"l"}, Value: false, Usage: "Legacy watch by polling instead fsnotify"}, &cli.BoolFlag{Name: "no-config", Aliases: []string{"nc"}, Value: false, Usage: "Ignore existing config and doesn't create a new one"}, }, Action: func(c *cli.Context) error { @@ -1117,6 +1118,10 @@ func setup(c *cli.Context) (err error) { // Start realize workflow func start(c *cli.Context) (err error) { r.Server = realize.Server{Parent: &r, Status: false, Open: false, Port: realize.Port, Host: realize.Host} + // set legacy watcher + if c.Bool("legacy"){ + r.Settings.Legacy.Set(1) + } // check no-config and read if !c.Bool("no-config") { // read a config if exist diff --git a/realize/cli.go b/realize/cli.go index 28a36b5..6e20511 100644 --- a/realize/cli.go +++ b/realize/cli.go @@ -18,7 +18,7 @@ var ( // RPrefix tool name RPrefix = "realize" // RVersion current version - RVersion = "2.0.1" + RVersion = "2.1" // RExt file extension RExt = ".yaml" // RFile config file name diff --git a/realize/notify.go b/realize/notify.go index 01b254c..03c6006 100644 --- a/realize/notify.go +++ b/realize/notify.go @@ -2,7 +2,7 @@ package realize // this code is imported from moby, unfortunately i can't import it directly as dependencies from its repo, // cause there was a problem between moby vendor and fsnotify -// i have just added only walk methods and some little changes to polling interval, originally set as static. +// i have just added only the walk methods and some little changes to polling interval, originally set as static. import ( "errors" @@ -57,7 +57,7 @@ type ( // PollingWatcher returns a poll-based file watcher func PollingWatcher(interval time.Duration) FileWatcher { if interval == 0 { - interval = 100 * time.Millisecond + interval = time.Duration(1) * time.Second } return &filePoller{ interval: interval, @@ -67,13 +67,13 @@ func PollingWatcher(interval time.Duration) FileWatcher { } // NewFileWatcher tries to use an fs-event watcher, and falls back to the poller if there is an error -func NewFileWatcher(force bool, interval time.Duration) (FileWatcher, error) { - if !force { +func NewFileWatcher(l Legacy) (FileWatcher, error) { + if !l.Force { if w, err := EventWatcher(); err == nil { return w, nil } } - return PollingWatcher(interval), nil + return PollingWatcher(l.Interval), nil } // EventWatcher returns an fs-event based file watcher diff --git a/realize/projects.go b/realize/projects.go index ff5fb66..6772ec3 100644 --- a/realize/projects.go +++ b/realize/projects.go @@ -274,7 +274,7 @@ func (p *Project) Watch(wg *sync.WaitGroup) { // change channel p.stop = make(chan bool) // init a new watcher - p.watcher, err = NewFileWatcher(p.parent.Settings.Legacy.Force, p.parent.Settings.Legacy.Interval) + p.watcher, err = NewFileWatcher(p.parent.Settings.Legacy) if err != nil { log.Fatal(err) } diff --git a/realize/projects_test.go b/realize/projects_test.go index 7aef1df..bdba2e1 100644 --- a/realize/projects_test.go +++ b/realize/projects_test.go @@ -100,7 +100,9 @@ func TestProject_Reload(t *testing.T) { parent: &r, }) input := "test/path" - r.Projects[0].watcher, _ = NewFileWatcher(false, 0) + r.Settings.Legacy.Force = false + r.Settings.Legacy.Interval = 0 + r.Projects[0].watcher, _ = NewFileWatcher(r.Settings.Legacy) r.Reload = func(context Context) { log.Println(context.Path) } diff --git a/realize/settings.go b/realize/settings.go index d30db6c..783cb2a 100644 --- a/realize/settings.go +++ b/realize/settings.go @@ -61,6 +61,17 @@ type Resource struct { Name string } +// Set legacy watcher with an interval +func (l *Legacy) Set(interval int){ + l.Force = true + l.Interval = time.Duration(interval) * time.Second +} + +// Unset legacy watcher +func (l *Legacy) Unset(){ + l.Force = false +} + // Remove realize folder func (s *Settings) Remove(d string) error { _, err := os.Stat(d)