commands: Improve the common build flag handling

Updates #10947
This commit is contained in:
Bjørn Erik Pedersen 2023-05-17 18:45:23 +02:00
parent 7ce033a89d
commit 8a69ccbb00
16 changed files with 86 additions and 71 deletions

View file

@ -111,22 +111,6 @@ type rootCommand struct {
environment string
// Common build flags.
*buildFlags
// TODO(bep) var vs string
logging bool
verbose bool
verboseLog bool
debug bool
quiet bool
renderToMemory bool
cfgFile string
cfgDir string
logFile string
}
type buildFlags struct {
baseURL string
gc bool
poll string
@ -139,6 +123,18 @@ type buildFlags struct {
mutexprofile string
traceprofile string
printm bool
// TODO(bep) var vs string
logging bool
verbose bool
verboseLog bool
debug bool
quiet bool
renderToMemory bool
cfgFile string
cfgDir string
logFile string
}
func (r *rootCommand) Build(cd *simplecobra.Commandeer, bcfg hugolib.BuildCfg, cfg config.Provider) (*hugolib.HugoSites, error) {
@ -384,7 +380,7 @@ func (r *rootCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args
return nil
}
func (r *rootCommand) Init(cd, runner *simplecobra.Commandeer) error {
func (r *rootCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
r.Out = os.Stdout
if r.quiet {
r.Out = io.Discard
@ -463,7 +459,8 @@ func (r *rootCommand) IsTestRun() bool {
return os.Getenv("HUGO_TESTRUN") != ""
}
func (r *rootCommand) WithCobraCommand(cmd *cobra.Command) error {
func (r *rootCommand) Init(cd *simplecobra.Commandeer) error {
cmd := cd.CobraCommand
cmd.Use = "hugo [flags]"
cmd.Short = "hugo builds your site"
cmd.Long = `hugo is the main command, used to build your Hugo site.
@ -503,7 +500,7 @@ Complete documentation is available at https://gohugo.io/.`
_ = cmd.PersistentFlags().SetAnnotation("logFile", cobra.BashCompFilenameExt, []string{})
// Configure local flags
applyLocalBuildFlags(cmd, r.buildFlags)
applyLocalBuildFlags(cmd, r)
// Set bash-completion.
// Each flag must first be defined before using the SetAnnotation() call.
@ -512,7 +509,7 @@ Complete documentation is available at https://gohugo.io/.`
return nil
}
func applyLocalBuildFlags(cmd *cobra.Command, f *buildFlags) {
func applyLocalBuildFlags(cmd *cobra.Command, r *rootCommand) {
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")
@ -522,25 +519,25 @@ func applyLocalBuildFlags(cmd *cobra.Command, f *buildFlags) {
cmd.Flags().StringP("cacheDir", "", "", "filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/")
cmd.Flags().BoolP("ignoreCache", "", false, "ignores the cache directory")
cmd.Flags().StringSliceP("theme", "t", []string{}, "themes to use (located in /themes/THEMENAME/)")
cmd.Flags().StringVarP(&f.baseURL, "baseURL", "b", "", "hostname (and path) to the root, e.g. https://spf13.com/")
cmd.Flags().StringVarP(&r.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(&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().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().Bool("templateMetrics", false, "display metrics about template executions")
cmd.Flags().Bool("templateMetricsHints", false, "calculate some improvement hints when combined with --templateMetrics")
cmd.Flags().BoolVar(&f.forceSyncStatic, "forceSyncStatic", false, "copy all files when static is changed.")
cmd.Flags().BoolVar(&r.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(&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)")
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)")
// Hide these for now.
cmd.Flags().MarkHidden("profile-cpu")
@ -591,7 +588,8 @@ func (c *simpleCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, arg
return c.run(ctx, cd, c.rootCmd, args)
}
func (c *simpleCommand) WithCobraCommand(cmd *cobra.Command) error {
func (c *simpleCommand) Init(cd *simplecobra.Commandeer) error {
cmd := cd.CobraCommand
cmd.Short = c.short
cmd.Long = c.long
if c.use != "" {
@ -603,7 +601,7 @@ func (c *simpleCommand) WithCobraCommand(cmd *cobra.Command) error {
return nil
}
func (c *simpleCommand) Init(cd, runner *simplecobra.Commandeer) error {
func (c *simpleCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
c.rootCmd = cd.Root.Command.(*rootCommand)
if c.initc != nil {
return c.initc(cd)

View file

@ -20,7 +20,6 @@ import (
// newExec wires up all of Hugo's CLI.
func newExec() (*simplecobra.Exec, error) {
rootCmd := &rootCommand{
buildFlags: &buildFlags{},
commands: []simplecobra.Commander{
newVersionCmd(),
newEnvCommand(),

View file

@ -23,7 +23,6 @@ import (
"github.com/gohugoio/hugo/modules"
"github.com/gohugoio/hugo/parser"
"github.com/gohugoio/hugo/parser/metadecoders"
"github.com/spf13/cobra"
)
// newConfigCommand creates a new config command and its subcommands.
@ -68,13 +67,14 @@ func (c *configCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, arg
return nil
}
func (c *configCommand) WithCobraCommand(cmd *cobra.Command) error {
func (c *configCommand) Init(cd *simplecobra.Commandeer) error {
cmd := cd.CobraCommand
cmd.Short = "Print the site configuration"
cmd.Long = `Print the site configuration, both default and custom settings.`
return nil
}
func (c *configCommand) Init(cd, runner *simplecobra.Commandeer) error {
func (c *configCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
c.r = cd.Root.Command.(*rootCommand)
return nil
}
@ -176,12 +176,13 @@ func (c *configMountsCommand) Run(ctx context.Context, cd *simplecobra.Commandee
return nil
}
func (c *configMountsCommand) WithCobraCommand(cmd *cobra.Command) error {
func (c *configMountsCommand) Init(cd *simplecobra.Commandeer) error {
cmd := cd.CobraCommand
cmd.Short = "Print the configured file mounts"
return nil
}
func (c *configMountsCommand) Init(cd, runner *simplecobra.Commandeer) error {
func (c *configMountsCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
c.configCmd = cd.Parent.Command.(*configCommand)
return nil
}

View file

@ -100,7 +100,8 @@ func (c *convertCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, ar
return nil
}
func (c *convertCommand) WithCobraCommand(cmd *cobra.Command) error {
func (c *convertCommand) Init(cd *simplecobra.Commandeer) error {
cmd := cd.CobraCommand
cmd.Short = "Convert your content to different formats"
cmd.Long = `Convert your content (e.g. front matter) to different formats.
@ -112,7 +113,7 @@ See convert's subcommands toJSON, toTOML and toYAML for more information.`
return nil
}
func (c *convertCommand) Init(cd, runner *simplecobra.Commandeer) error {
func (c *convertCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
c.r = cd.Root.Command.(*rootCommand)
cfg := config.New()
cfg.Set("buildDrafts", true)

View file

@ -202,12 +202,13 @@ func (c *genCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args [
return nil
}
func (c *genCommand) WithCobraCommand(cmd *cobra.Command) error {
func (c *genCommand) Init(cd *simplecobra.Commandeer) error {
cmd := cd.CobraCommand
cmd.Short = "A collection of several useful generators."
return nil
}
func (c *genCommand) Init(cd, runner *simplecobra.Commandeer) error {
func (c *genCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
c.rootCmd = cd.Root.Command.(*rootCommand)
return nil
}

View file

@ -90,7 +90,8 @@ func (c *importCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, arg
return nil
}
func (c *importCommand) WithCobraCommand(cmd *cobra.Command) error {
func (c *importCommand) Init(cd *simplecobra.Commandeer) error {
cmd := cd.CobraCommand
cmd.Short = "Import your site from others."
cmd.Long = `Import your site from other web site generators like Jekyll.
@ -99,7 +100,7 @@ Import requires a subcommand, e.g. ` + "`hugo import jekyll jekyll_root_path tar
return nil
}
func (c *importCommand) Init(cd, runner *simplecobra.Commandeer) error {
func (c *importCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
c.r = cd.Root.Command.(*rootCommand)
return nil
}

View file

@ -23,7 +23,6 @@ import (
"github.com/gohugoio/hugo/hugolib"
"github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/hugo/resources/resource"
"github.com/spf13/cobra"
)
// newListCommand creates a new list command and its subcommands.
@ -153,7 +152,8 @@ func (c *listCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args
return nil
}
func (c *listCommand) WithCobraCommand(cmd *cobra.Command) error {
func (c *listCommand) Init(cd *simplecobra.Commandeer) error {
cmd := cd.CobraCommand
cmd.Short = "Listing out various types of content"
cmd.Long = `Listing out various types of content.
@ -162,6 +162,6 @@ List requires a subcommand, e.g. hugo list drafts`
return nil
}
func (c *listCommand) Init(cd, runner *simplecobra.Commandeer) error {
func (c *listCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
return nil
}

View file

@ -293,7 +293,8 @@ func (c *modCommands) Run(ctx context.Context, cd *simplecobra.Commandeer, args
return nil
}
func (c *modCommands) WithCobraCommand(cmd *cobra.Command) error {
func (c *modCommands) Init(cd *simplecobra.Commandeer) error {
cmd := cd.CobraCommand
cmd.Short = "Various Hugo Modules helpers."
cmd.Long = `Various helpers to help manage the modules in your project's dependency graph.
Most operations here requires a Go version installed on your system (>= Go 1.12) and the relevant VCS client (typically Git).
@ -304,7 +305,7 @@ This is not needed if you only operate on modules inside /themes or if you have
return nil
}
func (c *modCommands) Init(cd, runner *simplecobra.Commandeer) error {
func (c *modCommands) PreRun(cd, runner *simplecobra.Commandeer) error {
c.r = cd.Root.Command.(*rootCommand)
return nil
}

View file

@ -271,7 +271,8 @@ func (c *newCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args [
return nil
}
func (c *newCommand) WithCobraCommand(cmd *cobra.Command) error {
func (c *newCommand) Init(cd *simplecobra.Commandeer) error {
cmd := cd.CobraCommand
cmd.Short = "Create new content for your site"
cmd.Long = `Create a new content file and automatically set the date and title.
It will guess which kind of file to create based on the path provided.
@ -284,7 +285,7 @@ Ensure you run this within the root directory of your site.`
return nil
}
func (c *newCommand) Init(cd, runner *simplecobra.Commandeer) error {
func (c *newCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
c.rootCmd = cd.Root.Command.(*rootCommand)
return nil
}

View file

@ -54,7 +54,6 @@ import (
"github.com/gohugoio/hugo/transform"
"github.com/gohugoio/hugo/transform/livereloadinject"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/fsync"
"golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore"
@ -99,8 +98,7 @@ func newHugoBuilder(r *rootCommand, s *serverCommand, onConfigLoaded ...func(rel
func newServerCommand() *serverCommand {
var c *serverCommand
c = &serverCommand{
buildFlags: &buildFlags{},
quit: make(chan bool),
quit: make(chan bool),
}
return c
}
@ -408,10 +406,6 @@ type serverCommand struct {
doLiveReload bool
// Flags.
// Common build flags.
*buildFlags
renderToDisk bool
renderStaticToDisk bool
navigateToChanged bool
@ -472,7 +466,8 @@ func (c *serverCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, arg
return c.serve()
}
func (c *serverCommand) WithCobraCommand(cmd *cobra.Command) error {
func (c *serverCommand) Init(cd *simplecobra.Commandeer) error {
cmd := cd.CobraCommand
cmd.Short = "A high performance webserver"
cmd.Long = `Hugo provides its own webserver which builds and serves the site.
While hugo server is high performance, it is a webserver with limited options.
@ -504,12 +499,13 @@ 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)
r := cd.Root.Command.(*rootCommand)
applyLocalBuildFlags(cmd, r)
return nil
}
func (c *serverCommand) Init(cd, runner *simplecobra.Commandeer) error {
func (c *serverCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
c.r = cd.Root.Command.(*rootCommand)
c.hugoBuilder = newHugoBuilder(

View file

@ -66,13 +66,14 @@ func (c *templateCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, a
return nil
}
func (c *templateCommand) WithCobraCommand(cmd *cobra.Command) error {
func (c *templateCommand) Init(cd *simplecobra.Commandeer) error {
cmd := cd.CobraCommand
cmd.Short = "Print the site configuration"
cmd.Long = `Print the site configuration, both default and custom settings.`
return nil
}
func (c *templateCommand) Init(cd, runner *simplecobra.Commandeer) error {
func (c *templateCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
c.r = cd.Root.Command.(*rootCommand)
return nil
}

View file

@ -23,6 +23,7 @@ import (
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/gohugoio/hugo/cache/filecache"
@ -92,7 +93,7 @@ type Config struct {
// For internal use only.
Internal InternalConfig `mapstructure:"-" json:"-"`
// For internal use only.
C ConfigCompiled `mapstructure:"-" json:"-"`
C *ConfigCompiled `mapstructure:"-" json:"-"`
RootConfig
@ -188,6 +189,8 @@ type configCompiler interface {
func (c Config) cloneForLang() *Config {
x := c
x.C = nil
// Collapse all static dirs to one.
x.StaticDir = x.staticDirs()
// These will go away soon ...
@ -302,7 +305,7 @@ func (c *Config) CompileConfig() error {
}
}
c.C = ConfigCompiled{
c.C = &ConfigCompiled{
Timeout: timeout,
BaseURL: baseURL,
BaseURLLiveReload: baseURL,
@ -329,11 +332,11 @@ func (c *Config) CompileConfig() error {
return nil
}
func (c Config) IsKindEnabled(kind string) bool {
func (c *Config) IsKindEnabled(kind string) bool {
return !c.C.DisabledKinds[kind]
}
func (c Config) IsLangDisabled(lang string) bool {
func (c *Config) IsLangDisabled(lang string) bool {
return c.C.DisabledLanguages[lang]
}
@ -356,10 +359,17 @@ type ConfigCompiled struct {
// With themes/modules we compute the configuration in multiple passes, and
// errors with missing output format definitions may resolve itself.
transientErr error
mu sync.Mutex
}
// This may be set after the config is compiled.
func (c *ConfigCompiled) SetMainSections(sections []string) {
func (c *ConfigCompiled) SetMainSectionsIfNotSet(sections []string) {
c.mu.Lock()
defer c.mu.Unlock()
if len(c.MainSections) > 0 {
return
}
c.MainSections = sections
}

2
go.mod
View file

@ -95,7 +95,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/sts v1.7.0 // indirect
github.com/aws/smithy-go v1.8.0 // indirect
github.com/bep/helpers v0.4.0 // indirect
github.com/bep/simplecobra v0.2.0 // indirect
github.com/bep/simplecobra v0.3.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/dlclark/regexp2 v1.7.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect

2
go.sum
View file

@ -187,6 +187,8 @@ github.com/bep/overlayfs v0.6.0 h1:sgLcq/qtIzbaQNl2TldGXOkHvqeZB025sPvHOQL+DYo=
github.com/bep/overlayfs v0.6.0/go.mod h1:NFjSmn3kCqG7KX2Lmz8qT8VhPPCwZap3UNogXawoQHM=
github.com/bep/simplecobra v0.2.0 h1:gfdZZ8QlPBMC9R9DRzUsxExR3FyuNtRkqMJqK98SBno=
github.com/bep/simplecobra v0.2.0/go.mod h1:EOp6bCKuuHmwA9bQcRC8LcDB60co2Cmht5X4xMIOwf0=
github.com/bep/simplecobra v0.3.0 h1:BEeb/Q5qdsv5Yy9Go30ynZtAbI+jHV4f5VtNSXvp91E=
github.com/bep/simplecobra v0.3.0/go.mod h1:EOp6bCKuuHmwA9bQcRC8LcDB60co2Cmht5X4xMIOwf0=
github.com/bep/tmc v0.5.1 h1:CsQnSC6MsomH64gw0cT5f+EwQDcvZz4AazKunFwTpuI=
github.com/bep/tmc v0.5.1/go.mod h1:tGYHN8fS85aJPhDLgXETVKp+PR382OvFi2+q2GkGsq0=
github.com/bep/workers v1.0.0 h1:U+H8YmEaBCEaFZBst7GcRVEoqeRC9dzH2dWOwGmOchg=

View file

@ -743,9 +743,12 @@ func (m *pageMaps) AssemblePages() error {
sw := &sectionWalker{m: pm.contentMap}
a := sw.applyAggregates()
if a.mainSection != "" && len(pm.s.s.conf.C.MainSections) == 0 {
if a.mainSection != "" {
// Note, sites that have no custom config share a common config struct pointer.
// This means that we currently do not support setting different values per language.
// The end user can, however, configure this per language if needed.
mainSections := []string{strings.TrimRight(a.mainSection, "/")}
pm.s.s.conf.C.SetMainSections(mainSections)
pm.s.s.conf.C.SetMainSectionsIfNotSet(mainSections)
}
pm.s.lastmod = a.datesAll.Lastmod()
if resource.IsZeroDates(pm.s.home) {