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)