hugo

Unnamed repository; edit this file 'description' to name the repository.

git clone git://git.shimmy1996.com/hugo.git
commit e31b1d194655ac3a38fe903ff3995806b129b88a
parent 43a23239b2e3ad602c06d9af0b648e0304fc8744
Author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Date:   Mon,  5 Jul 2021 10:13:41 +0200

commands: Make the --poll flag a duration

So you can do:

```
hugo server --poll 700ms
```

See #8720

Diffstat:
Mcommands/commands.go | 4++--
Mcommands/hugo.go | 19++++++++++++++-----
Mcommon/types/convert.go | 20++++++++++++++++++++
Mcommon/types/convert_test.go | 11+++++++++++
Mdocs/content/en/commands/hugo.md | 2+-
Mdocs/content/en/commands/hugo_mod.md | 2+-
Mdocs/content/en/commands/hugo_new.md | 2+-
Mdocs/content/en/commands/hugo_server.md | 2+-
Mhugolib/site.go | 12+++++-------
9 files changed, 56 insertions(+), 18 deletions(-)
diff --git a/commands/commands.go b/commands/commands.go
@@ -204,7 +204,7 @@ type hugoBuilderCommon struct {
 	environment string
 
 	buildWatch bool
-	poll       bool
+	poll       string
 
 	gc bool
 
@@ -292,7 +292,7 @@ func (cc *hugoBuilderCommon) handleFlags(cmd *cobra.Command) {
 	cmd.Flags().StringVarP(&cc.baseURL, "baseURL", "b", "", "hostname (and path) to the root, e.g. http://spf13.com/")
 	cmd.Flags().Bool("enableGitInfo", false, "add Git revision, date and author info to the pages")
 	cmd.Flags().BoolVar(&cc.gc, "gc", false, "enable to run some cleanup tasks (remove unused cache files) after the build")
-	cmd.Flags().BoolVar(&cc.poll, "poll", false, "use a poll based approach to watch for file system changes")
+	cmd.Flags().StringVar(&cc.poll, "poll", "", "set this to a poll interval, e.g --poll 700ms, to use a poll based approach to watch for file system changes")
 
 	cmd.Flags().Bool("templateMetrics", false, "display metrics about template executions")
 	cmd.Flags().Bool("templateMetricsHints", false, "calculate some improvement hints when combined with --templateMetrics")
diff --git a/commands/hugo.go b/commands/hugo.go
@@ -30,6 +30,8 @@ import (
 	"syscall"
 	"time"
 
+	"github.com/gohugoio/hugo/common/types"
+
 	"github.com/gohugoio/hugo/hugofs"
 
 	"github.com/gohugoio/hugo/resources/page"
@@ -820,7 +822,7 @@ func (c *commandeer) fullRebuild(changeType string) {
 }
 
 // newWatcher creates a new watcher to watch filesystem events.
-func (c *commandeer) newWatcher(poll bool, dirList ...string) (*watcher.Batcher, error) {
+func (c *commandeer) newWatcher(pollIntervalStr string, dirList ...string) (*watcher.Batcher, error) {
 	if runtime.GOOS == "darwin" {
 		tweakLimit()
 	}
@@ -830,10 +832,17 @@ func (c *commandeer) newWatcher(poll bool, dirList ...string) (*watcher.Batcher,
 		return nil, err
 	}
 
-	// The second interval is used by the poll based watcher.
-	// Setting a shorter interval would make it snappier,
-	// but it would consume more CPU.
-	watcher, err := watcher.New(500*time.Millisecond, 700*time.Millisecond, poll)
+	var pollInterval time.Duration
+	poll := pollIntervalStr != ""
+	if poll {
+		pollInterval, err = types.ToDurationE(pollIntervalStr)
+		if err != nil {
+			return nil, fmt.Errorf("invalid value for flag poll: %s", err)
+		}
+		c.logger.Printf("Use watcher with poll interval %v", pollInterval)
+	}
+
+	watcher, err := watcher.New(500*time.Millisecond, pollInterval, poll)
 	if err != nil {
 		return nil, err
 	}
diff --git a/common/types/convert.go b/common/types/convert.go
@@ -18,10 +18,30 @@ import (
 	"fmt"
 	"html/template"
 	"reflect"
+	"time"
 
 	"github.com/spf13/cast"
 )
 
+// ToDuration converts v to time.Duration.
+// See ToDurationE if you need to handle errors.
+func ToDuration(v interface{}) time.Duration {
+	d, _ := ToDurationE(v)
+	return d
+}
+
+// ToDurationE converts v to time.Duration.
+func ToDurationE(v interface{}) (time.Duration, error) {
+	if n := cast.ToInt(v); n > 0 {
+		return time.Duration(n) * time.Millisecond, nil
+	}
+	d, err := time.ParseDuration(cast.ToString(v))
+	if err != nil {
+		return 0, fmt.Errorf("cannot convert %v to time.Duration", v)
+	}
+	return d, nil
+}
+
 // ToStringSlicePreserveString is the same as ToStringSlicePreserveStringE,
 // but it never fails.
 func ToStringSlicePreserveString(v interface{}) []string {
diff --git a/common/types/convert_test.go b/common/types/convert_test.go
@@ -16,6 +16,7 @@ package types
 import (
 	"encoding/json"
 	"testing"
+	"time"
 
 	qt "github.com/frankban/quicktest"
 )
@@ -36,3 +37,13 @@ func TestToString(t *testing.T) {
 	c.Assert(ToString([]byte("Hugo")), qt.Equals, "Hugo")
 	c.Assert(ToString(json.RawMessage("Hugo")), qt.Equals, "Hugo")
 }
+
+func TestToDuration(t *testing.T) {
+	c := qt.New(t)
+
+	c.Assert(ToDuration("200ms"), qt.Equals, 200*time.Millisecond)
+	c.Assert(ToDuration("200"), qt.Equals, 200*time.Millisecond)
+	c.Assert(ToDuration("4m"), qt.Equals, 4*time.Minute)
+	c.Assert(ToDuration("asdfadf"), qt.Equals, time.Duration(0))
+
+}
diff --git a/docs/content/en/commands/hugo.md b/docs/content/en/commands/hugo.md
@@ -51,7 +51,7 @@ hugo [flags]
       --noChmod                    don't sync permission mode of files
       --noTimes                    don't sync modification time of files
       --path-warnings              print warnings on duplicate target paths etc.
-      --poll                       use a poll based approach to watch for file system changes
+      --poll string                set this to a poll interval, e.g --poll 700ms, to use a poll based approach to watch for file system changes
       --print-mem                  print memory usage to screen at intervals
       --quiet                      build in quiet mode
       --renderToMemory             render to memory (only useful for benchmark testing)
diff --git a/docs/content/en/commands/hugo_mod.md b/docs/content/en/commands/hugo_mod.md
@@ -46,7 +46,7 @@ See https://gohugo.io/hugo-modules/ for more information.
       --noChmod                don't sync permission mode of files
       --noTimes                don't sync modification time of files
       --path-warnings          print warnings on duplicate target paths etc.
-      --poll                   use a poll based approach to watch for file system changes
+      --poll string            set this to a poll interval, e.g --poll 700ms, to use a poll based approach to watch for file system changes
       --print-mem              print memory usage to screen at intervals
       --templateMetrics        display metrics about template executions
       --templateMetricsHints   calculate some improvement hints when combined with --templateMetrics
diff --git a/docs/content/en/commands/hugo_new.md b/docs/content/en/commands/hugo_new.md
@@ -47,7 +47,7 @@ hugo new [path] [flags]
       --noChmod                don't sync permission mode of files
       --noTimes                don't sync modification time of files
       --path-warnings          print warnings on duplicate target paths etc.
-      --poll                   use a poll based approach to watch for file system changes
+      --poll string            set this to a poll interval, e.g --poll 700ms, to use a poll based approach to watch for file system changes
       --print-mem              print memory usage to screen at intervals
       --templateMetrics        display metrics about template executions
       --templateMetricsHints   calculate some improvement hints when combined with --templateMetrics
diff --git a/docs/content/en/commands/hugo_server.md b/docs/content/en/commands/hugo_server.md
@@ -59,7 +59,7 @@ hugo server [flags]
       --noHTTPCache            prevent HTTP caching
       --noTimes                don't sync modification time of files
       --path-warnings          print warnings on duplicate target paths etc.
-      --poll                   use a poll based approach to watch for file system changes
+      --poll string            set this to a poll interval, e.g --poll 700ms, to use a poll based approach to watch for file system changes
   -p, --port int               port on which the server will listen (default 1313)
       --print-mem              print memory usage to screen at intervals
       --renderToDisk           render to Destination path (default is render to memory & serve from there)
diff --git a/hugolib/site.go b/hugolib/site.go
@@ -29,6 +29,8 @@ import (
 	"strings"
 	"time"
 
+	"github.com/gohugoio/hugo/common/types"
+
 	"github.com/gohugoio/hugo/common/paths"
 
 	"github.com/gohugoio/hugo/common/constants"
@@ -523,13 +525,9 @@ But this also means that your site configuration may not do what you expect. If 
 	timeout := 30 * time.Second
 	if cfg.Language.IsSet("timeout") {
 		v := cfg.Language.Get("timeout")
-		if n := cast.ToInt(v); n > 0 {
-			timeout = time.Duration(n) * time.Millisecond
-		} else {
-			d, err := time.ParseDuration(cast.ToString(v))
-			if err == nil {
-				timeout = d
-			}
+		d, err := types.ToDurationE(v)
+		if err == nil {
+			timeout = d
 		}
 	}