]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Re-establish all the server flags
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 17 May 2023 10:45:51 +0000 (12:45 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 17 May 2023 20:13:29 +0000 (22:13 +0200)
Updates #10947

commands/commandeer.go
commands/commands.go
commands/server.go
testscripts/commands/server.txt [deleted file]
testscripts/unfinished/server.txt [new file with mode: 0644]

index ed578e9bf2cbb68da12662a0714d0c77dc082605..7e53189961c0f920abed5d7324856c3aabca14d9 100644 (file)
@@ -106,21 +106,12 @@ type rootCommand struct {
        commands []simplecobra.Commander
 
        // Flags
-       source          string
-       baseURL         string
-       buildWatch      bool
-       forceSyncStatic bool
-       panicOnWarning  bool
-       environment     string
-       poll            string
-       gc              bool
+       source      string
+       buildWatch  bool
+       environment string
 
-       // Profile flags (for debugging of performance problems)
-       cpuprofile   string
-       memprofile   string
-       mutexprofile string
-       traceprofile string
-       printm       bool
+       // Common build flags.
+       *buildFlags
 
        // TODO(bep) var vs string
        logging        bool
@@ -135,6 +126,21 @@ type rootCommand struct {
        logFile string
 }
 
+type buildFlags struct {
+       baseURL         string
+       gc              bool
+       poll            string
+       panicOnWarning  bool
+       forceSyncStatic bool
+
+       // Profile flags (for debugging of performance problems)
+       cpuprofile   string
+       memprofile   string
+       mutexprofile string
+       traceprofile string
+       printm       bool
+}
+
 func (r *rootCommand) Build(cd *simplecobra.Commandeer, bcfg hugolib.BuildCfg, cfg config.Provider) (*hugolib.HugoSites, error) {
        h, err := r.Hugo(cfg)
        if err != nil {
@@ -470,6 +476,9 @@ Complete documentation is available at https://gohugo.io/.`
        // Configure persistent flags
        cmd.PersistentFlags().StringVarP(&r.source, "source", "s", "", "filesystem path to read files relative from")
        cmd.PersistentFlags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{})
+       cmd.PersistentFlags().StringP("destination", "d", "", "filesystem path to write files to")
+       cmd.PersistentFlags().SetAnnotation("destination", cobra.BashCompSubdirsInDir, []string{})
+
        cmd.PersistentFlags().StringVarP(&r.environment, "environment", "e", "", "build environment")
        cmd.PersistentFlags().StringP("themesDir", "", "", "filesystem path to themes directory")
        cmd.PersistentFlags().StringP("ignoreVendorPaths", "", "", "ignores any _vendor for module paths matching the given Glob pattern")
@@ -494,6 +503,16 @@ Complete documentation is available at https://gohugo.io/.`
        _ = cmd.PersistentFlags().SetAnnotation("logFile", cobra.BashCompFilenameExt, []string{})
 
        // Configure local flags
+       applyLocalBuildFlags(cmd, r.buildFlags)
+
+       // Set bash-completion.
+       // Each flag must first be defined before using the SetAnnotation() call.
+       _ = cmd.Flags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{})
+
+       return nil
+}
+
+func applyLocalBuildFlags(cmd *cobra.Command, f *buildFlags) {
        cmd.Flags().Bool("cleanDestinationDir", false, "remove files from destination not found in static directories")
        cmd.Flags().BoolP("buildDrafts", "D", false, "include content marked as draft")
        cmd.Flags().BoolP("buildFuture", "F", false, "include content with publishdate in the future")
@@ -502,27 +521,26 @@ Complete documentation is available at https://gohugo.io/.`
        cmd.Flags().StringP("layoutDir", "l", "", "filesystem path to layout directory")
        cmd.Flags().StringP("cacheDir", "", "", "filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/")
        cmd.Flags().BoolP("ignoreCache", "", false, "ignores the cache directory")
-       cmd.Flags().StringP("destination", "d", "", "filesystem path to write files to")
        cmd.Flags().StringSliceP("theme", "t", []string{}, "themes to use (located in /themes/THEMENAME/)")
-       cmd.Flags().StringVarP(&r.baseURL, "baseURL", "b", "", "hostname (and path) to the root, e.g. https://spf13.com/")
+       cmd.Flags().StringVarP(&f.baseURL, "baseURL", "b", "", "hostname (and path) to the root, e.g. https://spf13.com/")
        cmd.Flags().Bool("enableGitInfo", false, "add Git revision, date, author, and CODEOWNERS info to the pages")
-       cmd.Flags().BoolVar(&r.gc, "gc", false, "enable to run some cleanup tasks (remove unused cache files) after the build")
-       cmd.Flags().StringVar(&r.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().BoolVar(&r.panicOnWarning, "panicOnWarning", false, "panic on first WARNING log")
+       cmd.Flags().BoolVar(&f.gc, "gc", false, "enable to run some cleanup tasks (remove unused cache files) after the build")
+       cmd.Flags().StringVar(&f.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().BoolVar(&f.panicOnWarning, "panicOnWarning", false, "panic on first WARNING log")
        cmd.Flags().Bool("templateMetrics", false, "display metrics about template executions")
        cmd.Flags().Bool("templateMetricsHints", false, "calculate some improvement hints when combined with --templateMetrics")
-       cmd.Flags().BoolVar(&r.forceSyncStatic, "forceSyncStatic", false, "copy all files when static is changed.")
+       cmd.Flags().BoolVar(&f.forceSyncStatic, "forceSyncStatic", false, "copy all files when static is changed.")
        cmd.Flags().BoolP("noTimes", "", false, "don't sync modification time of files")
        cmd.Flags().BoolP("noChmod", "", false, "don't sync permission mode of files")
        cmd.Flags().BoolP("noBuildLock", "", false, "don't create .hugo_build.lock file")
        cmd.Flags().BoolP("printI18nWarnings", "", false, "print missing translations")
        cmd.Flags().BoolP("printPathWarnings", "", false, "print warnings on duplicate target paths etc.")
        cmd.Flags().BoolP("printUnusedTemplates", "", false, "print warnings on unused templates.")
-       cmd.Flags().StringVarP(&r.cpuprofile, "profile-cpu", "", "", "write cpu profile to `file`")
-       cmd.Flags().StringVarP(&r.memprofile, "profile-mem", "", "", "write memory profile to `file`")
-       cmd.Flags().BoolVarP(&r.printm, "printMemoryUsage", "", false, "print memory usage to screen at intervals")
-       cmd.Flags().StringVarP(&r.mutexprofile, "profile-mutex", "", "", "write Mutex profile to `file`")
-       cmd.Flags().StringVarP(&r.traceprofile, "trace", "", "", "write trace to `file` (not useful in general)")
+       cmd.Flags().StringVarP(&f.cpuprofile, "profile-cpu", "", "", "write cpu profile to `file`")
+       cmd.Flags().StringVarP(&f.memprofile, "profile-mem", "", "", "write memory profile to `file`")
+       cmd.Flags().BoolVarP(&f.printm, "printMemoryUsage", "", false, "print memory usage to screen at intervals")
+       cmd.Flags().StringVarP(&f.mutexprofile, "profile-mutex", "", "", "write Mutex profile to `file`")
+       cmd.Flags().StringVarP(&f.traceprofile, "trace", "", "", "write trace to `file` (not useful in general)")
 
        // Hide these for now.
        cmd.Flags().MarkHidden("profile-cpu")
@@ -533,14 +551,10 @@ Complete documentation is available at https://gohugo.io/.`
 
        cmd.Flags().Bool("minify", false, "minify any supported output format (HTML, XML etc.)")
 
-       // Set bash-completion.
-       // Each flag must first be defined before using the SetAnnotation() call.
-       _ = cmd.Flags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{})
        _ = cmd.Flags().SetAnnotation("cacheDir", cobra.BashCompSubdirsInDir, []string{})
        _ = cmd.Flags().SetAnnotation("destination", cobra.BashCompSubdirsInDir, []string{})
        _ = cmd.Flags().SetAnnotation("theme", cobra.BashCompSubdirsInDir, []string{"themes"})
 
-       return nil
 }
 
 func (r *rootCommand) timeTrack(start time.Time, name string) {
index 9d707b8418994e6753b3044df34cce6fd1beeb9c..f90b09e0c00d93b8310c11ef26b135bec7c81dd7 100644 (file)
@@ -20,6 +20,7 @@ import (
 // newExec wires up all of Hugo's CLI.
 func newExec() (*simplecobra.Exec, error) {
        rootCmd := &rootCommand{
+               buildFlags: &buildFlags{},
                commands: []simplecobra.Commander{
                        newVersionCmd(),
                        newEnvCommand(),
index 81a5120efeabdfb63cdbed6e08d616935ffccaa2..5c2fd7704c6ff2d008a26e4a6e8e46c863184bdc 100644 (file)
@@ -99,7 +99,8 @@ func newHugoBuilder(r *rootCommand, s *serverCommand, onConfigLoaded ...func(rel
 func newServerCommand() *serverCommand {
        var c *serverCommand
        c = &serverCommand{
-               quit: make(chan bool),
+               buildFlags: &buildFlags{},
+               quit:       make(chan bool),
        }
        return c
 }
@@ -408,6 +409,9 @@ type serverCommand struct {
 
        // Flags.
 
+       // Common build flags.
+       *buildFlags
+
        renderToDisk        bool
        renderStaticToDisk  bool
        navigateToChanged   bool
@@ -499,6 +503,9 @@ of a second, you will be able to save and see your changes nearly instantly.`
 
        cmd.Flags().String("memstats", "", "log memory usage to this file")
        cmd.Flags().String("meminterval", "100ms", "interval to poll memory usage (requires --memstats), valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\".")
+
+       applyLocalBuildFlags(cmd, c.buildFlags)
+
        return nil
 }
 
diff --git a/testscripts/commands/server.txt b/testscripts/commands/server.txt
deleted file mode 100644 (file)
index 1316356..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-# Test the hugo server command.
-
-# We run these tests in parallel so let Hugo decide which port to use.
-hugo server &
-
-waitServer
-
-httpget $HUGOTEST_BASEURL_0 'Title: Hugo Server Test' $HUGOTEST_BASEURL_0 'ServerPort: \d{4,5}' 'myenv: thedevelopment' 'livereload\.js' 'Env: development' 'IsServer: true'
-httpget ${HUGOTEST_BASEURL_0}doesnotexist 'custom 404'
-
-# By defauilt, the server renders to memory.
-! exists public/index.html
-
-stopServer
-! stderr .
-
--- hugo.toml --
-title = "Hugo Server Test"
-baseURL = "https://example.org/"
-disableKinds = ["taxonomy", "term", "sitemap"]
--- config/production/params.toml --
-myenv = "theproduction"
--- config/development/params.toml --
-myenv = "thedevelopment"
--- layouts/index.html --
-<body>
-Title: {{ .Title }}|BaseURL: {{ site.BaseURL }}|ServerPort: {{ site.ServerPort }}|myenv: {{ .Site.Params.myenv }}|Env: {{ hugo.Environment }}|IsServer: {{ site.IsServer }}|
-</body>
--- layouts/404.html --
-custom 404
diff --git a/testscripts/unfinished/server.txt b/testscripts/unfinished/server.txt
new file mode 100644 (file)
index 0000000..fd6b200
--- /dev/null
@@ -0,0 +1,30 @@
+# Test the hugo server command.
+
+# We run these tests in parallel so let Hugo decide which port to use.
+hugo server --gc &
+
+waitServer
+
+httpget $HUGOTEST_BASEURL_0 'Title: Hugo Server Test' $HUGOTEST_BASEURL_0 'ServerPort: \d{4,5}' 'myenv: thedevelopment' 'livereload\.js' 'Env: development' 'IsServer: true'
+httpget ${HUGOTEST_BASEURL_0}doesnotexist 'custom 404'
+
+# By defauilt, the server renders to memory.
+! exists public/index.html
+
+stopServer
+! stderr .
+
+-- hugo.toml --
+title = "Hugo Server Test"
+baseURL = "https://example.org/"
+disableKinds = ["taxonomy", "term", "sitemap"]
+-- config/production/params.toml --
+myenv = "theproduction"
+-- config/development/params.toml --
+myenv = "thedevelopment"
+-- layouts/index.html --
+<body>
+Title: {{ .Title }}|BaseURL: {{ site.BaseURL }}|ServerPort: {{ site.ServerPort }}|myenv: {{ .Site.Params.myenv }}|Env: {{ hugo.Environment }}|IsServer: {{ site.IsServer }}|
+</body>
+-- layouts/404.html --
+custom 404