Skip to content

Commit

Permalink
commands: Improve the common build flag handling
Browse files Browse the repository at this point in the history
Updates #10947
  • Loading branch information
bep committed May 17, 2023
1 parent 7ce033a commit 8a69ccb
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 71 deletions.
62 changes: 30 additions & 32 deletions commands/commandeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
11 changes: 6 additions & 5 deletions commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
5 changes: 3 additions & 2 deletions commands/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions commands/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 3 additions & 2 deletions commands/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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
}
5 changes: 3 additions & 2 deletions commands/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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
}
5 changes: 3 additions & 2 deletions commands/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand Down
16 changes: 6 additions & 10 deletions commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -408,10 +406,6 @@ type serverCommand struct {
doLiveReload bool

// Flags.

// Common build flags.
*buildFlags

renderToDisk bool
renderStaticToDisk bool
navigateToChanged bool
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions commands/xcommand_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 8a69ccb

Please sign in to comment.