Skip to content

Commit

Permalink
commands: Reinstate some of the removed build flags (e.g. --theme) to…
Browse files Browse the repository at this point in the history
… new and mod

Fixes #11018
  • Loading branch information
bep committed May 28, 2023
1 parent e96cdfe commit 43f1282
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 33 deletions.
33 changes: 19 additions & 14 deletions commands/commandeer.go
Expand Up @@ -504,7 +504,7 @@ Complete documentation is available at https://gohugo.io/.`
_ = cmd.PersistentFlags().SetAnnotation("logFile", cobra.BashCompFilenameExt, []string{})

// Configure local flags
applyLocalBuildFlags(cmd, r)
applyLocalFlagsBuild(cmd, r)

// Set bash-completion.
// Each flag must first be defined before using the SetAnnotation() call.
Expand All @@ -513,17 +513,26 @@ Complete documentation is available at https://gohugo.io/.`
return nil
}

func applyLocalBuildFlags(cmd *cobra.Command, r *rootCommand) {
// A sub set of the complete build flags. These flags are used by new and mod.
func applyLocalFlagsBuildConfig(cmd *cobra.Command, r *rootCommand) {
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().StringP("cacheDir", "", "", "filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/")
_ = cmd.Flags().SetAnnotation("cacheDir", cobra.BashCompSubdirsInDir, []string{})
cmd.Flags().StringP("contentDir", "c", "", "filesystem path to content directory")
cmd.Flags().StringP("layoutDir", "l", "", "filesystem path to layout directory")
_ = cmd.Flags().SetAnnotation("theme", cobra.BashCompSubdirsInDir, []string{"themes"})

}

// Flags needed to do a build (used by hugo and hugo server commands)
func applyLocalFlagsBuild(cmd *cobra.Command, r *rootCommand) {
applyLocalFlagsBuildConfig(cmd, r)
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")
cmd.Flags().BoolP("buildExpired", "E", false, "include expired content")
cmd.Flags().StringP("contentDir", "c", "", "filesystem path to content directory")
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().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().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")
Expand All @@ -549,12 +558,8 @@ func applyLocalBuildFlags(cmd *cobra.Command, r *rootCommand) {
cmd.Flags().MarkHidden("profile-mutex")

cmd.Flags().StringSlice("disableKinds", []string{}, "disable different kind of pages (home, RSS etc.)")

cmd.Flags().Bool("minify", false, "minify any supported output format (HTML, XML etc.)")

_ = cmd.Flags().SetAnnotation("cacheDir", cobra.BashCompSubdirsInDir, []string{})
_ = cmd.Flags().SetAnnotation("destination", cobra.BashCompSubdirsInDir, []string{})
_ = cmd.Flags().SetAnnotation("theme", cobra.BashCompSubdirsInDir, []string{"themes"})

}

Expand All @@ -569,7 +574,7 @@ type simpleCommand struct {
short string
long string
run func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *rootCommand, args []string) error
withc func(cmd *cobra.Command)
withc func(cmd *cobra.Command, r *rootCommand)
initc func(cd *simplecobra.Commandeer) error

commands []simplecobra.Commander
Expand All @@ -593,20 +598,20 @@ func (c *simpleCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, arg
}

func (c *simpleCommand) Init(cd *simplecobra.Commandeer) error {
c.rootCmd = cd.Root.Command.(*rootCommand)
cmd := cd.CobraCommand
cmd.Short = c.short
cmd.Long = c.long
if c.use != "" {
cmd.Use = c.use
}
if c.withc != nil {
c.withc(cmd)
c.withc(cmd, c.rootCmd)
}
return nil
}

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
6 changes: 3 additions & 3 deletions commands/convert.go
Expand Up @@ -45,7 +45,7 @@ to use JSON for the front matter.`,
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
return c.convertContents(metadecoders.JSON)
},
withc: func(cmd *cobra.Command) {
withc: func(cmd *cobra.Command, r *rootCommand) {
},
},
&simpleCommand{
Expand All @@ -56,7 +56,7 @@ to use TOML for the front matter.`,
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
return c.convertContents(metadecoders.TOML)
},
withc: func(cmd *cobra.Command) {
withc: func(cmd *cobra.Command, r *rootCommand) {
},
},
&simpleCommand{
Expand All @@ -67,7 +67,7 @@ to use YAML for the front matter.`,
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
return c.convertContents(metadecoders.YAML)
},
withc: func(cmd *cobra.Command) {
withc: func(cmd *cobra.Command, r *rootCommand) {
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion commands/deploy.go
Expand Up @@ -58,7 +58,7 @@ documentation.
}
return deployer.Deploy(ctx)
},
withc: func(cmd *cobra.Command) {
withc: func(cmd *cobra.Command, r *rootCommand) {
cmd.Flags().String("target", "", "target deployment from deployments section in config file; defaults to the first one")
cmd.Flags().Bool("confirm", false, "ask for confirmation before making changes to the target")
cmd.Flags().Bool("dryRun", false, "dry run")
Expand Down
2 changes: 1 addition & 1 deletion commands/deploy_off.go
Expand Up @@ -41,7 +41,7 @@ func newDeployCommand() simplecobra.Commander {
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
return nil
},
withc: func(cmd *cobra.Command) {
withc: func(cmd *cobra.Command, r *rootCommand) {
cmd.Hidden = true
},
}
Expand Down
8 changes: 4 additions & 4 deletions commands/gen.go
Expand Up @@ -70,7 +70,7 @@ See https://xyproto.github.io/splash/docs/all.html for a preview of the availabl
formatter.WriteCSS(os.Stdout, style)
return nil
},
withc: func(cmd *cobra.Command) {
withc: func(cmd *cobra.Command, r *rootCommand) {
cmd.PersistentFlags().StringVar(&style, "style", "friendly", "highlighter style (see https://xyproto.github.io/splash/docs/)")
cmd.PersistentFlags().StringVar(&highlightStyle, "highlightStyle", "bg:#ffffcc", "style used for highlighting lines (see https://github.com/alecthomas/chroma)")
cmd.PersistentFlags().StringVar(&linesStyle, "linesStyle", "", "style used for line numbers (see https://github.com/alecthomas/chroma)")
Expand Down Expand Up @@ -110,7 +110,7 @@ See https://xyproto.github.io/splash/docs/all.html for a preview of the availabl

return nil
},
withc: func(cmd *cobra.Command) {
withc: func(cmd *cobra.Command, r *rootCommand) {
cmd.PersistentFlags().StringVar(&genmandir, "dir", "man/", "the directory to write the man pages.")
// For bash-completion
cmd.PersistentFlags().SetAnnotation("dir", cobra.BashCompSubdirsInDir, []string{})
Expand Down Expand Up @@ -167,7 +167,7 @@ url: %s

return nil
},
withc: func(cmd *cobra.Command) {
withc: func(cmd *cobra.Command, r *rootCommand) {
cmd.PersistentFlags().StringVar(&gendocdir, "dir", "/tmp/hugodoc/", "the directory to write the doc.")
// For bash-completion
cmd.PersistentFlags().SetAnnotation("dir", cobra.BashCompSubdirsInDir, []string{})
Expand Down Expand Up @@ -204,7 +204,7 @@ url: %s
r.Println("Done!")
return nil
},
withc: func(cmd *cobra.Command) {
withc: func(cmd *cobra.Command, r *rootCommand) {
cmd.Hidden = true
cmd.PersistentFlags().StringVarP(&docsHelperTarget, "dir", "", "docs/data", "data dir")
},
Expand Down
2 changes: 1 addition & 1 deletion commands/import.go
Expand Up @@ -59,7 +59,7 @@ Import from Jekyll requires two paths, e.g. ` + "`hugo import jekyll jekyll_root
}
return c.importFromJekyll(args)
},
withc: func(cmd *cobra.Command) {
withc: func(cmd *cobra.Command, r *rootCommand) {
cmd.Flags().BoolVar(&c.force, "force", false, "allow import into non-empty target directory")
},
},
Expand Down
23 changes: 19 additions & 4 deletions commands/mod.go
Expand Up @@ -61,6 +61,9 @@ This command is marked as 'Experimental'. We think it's a great idea, so it's no
removed from Hugo, but we need to test this out in "real life" to get a feel of it,
so this may/will change in future versions of Hugo.
`,
withc: func(cmd *cobra.Command, r *rootCommand) {
applyLocalFlagsBuildConfig(cmd, r)
},
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
h, err := r.Hugo(flagsToCfg(cd, nil))
if err != nil {
Expand All @@ -85,6 +88,9 @@ so this may/will change in future versions of Hugo.
Note that Hugo Modules supports multi-module projects, so you can initialize a Hugo Module
inside a subfolder on GitHub, as one example.
`,
withc: func(cmd *cobra.Command, r *rootCommand) {
applyLocalFlagsBuildConfig(cmd, r)
},
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
h, err := r.Hugo(flagsToCfg(cd, nil))
if err != nil {
Expand All @@ -101,7 +107,8 @@ so this may/will change in future versions of Hugo.
name: "verify",
short: "Verify dependencies.",
long: `Verify checks that the dependencies of the current module, which are stored in a local downloaded source cache, have not been modified since being downloaded.`,
withc: func(cmd *cobra.Command) {
withc: func(cmd *cobra.Command, r *rootCommand) {
applyLocalFlagsBuildConfig(cmd, r)
cmd.Flags().BoolVarP(&clean, "clean", "", false, "delete module cache for dependencies that fail verification")
},
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
Expand All @@ -119,7 +126,8 @@ so this may/will change in future versions of Hugo.
long: `Print a module dependency graph with information about module status (disabled, vendored).
Note that for vendored modules, that is the version listed and not the one from go.mod.
`,
withc: func(cmd *cobra.Command) {
withc: func(cmd *cobra.Command, r *rootCommand) {
applyLocalFlagsBuildConfig(cmd, r)
cmd.Flags().BoolVarP(&clean, "clean", "", false, "delete module cache for dependencies that fail verification")
},
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
Expand All @@ -135,7 +143,8 @@ Note that for vendored modules, that is the version listed and not the one from
name: "clean",
short: "Delete the Hugo Module cache for the current project.",
long: `Delete the Hugo Module cache for the current project.`,
withc: func(cmd *cobra.Command) {
withc: func(cmd *cobra.Command, r *rootCommand) {
applyLocalFlagsBuildConfig(cmd, r)
cmd.Flags().StringVarP(&pattern, "pattern", "", "", `pattern matching module paths to clean (all if not set), e.g. "**hugo*"`)
cmd.Flags().BoolVarP(&all, "all", "", false, "clean entire module cache")
},
Expand All @@ -157,6 +166,9 @@ Note that for vendored modules, that is the version listed and not the one from
&simpleCommand{
name: "tidy",
short: "Remove unused entries in go.mod and go.sum.",
withc: func(cmd *cobra.Command, r *rootCommand) {
applyLocalFlagsBuildConfig(cmd, r)
},
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
h, err := r.Hugo(flagsToCfg(cd, nil))
if err != nil {
Expand All @@ -171,6 +183,9 @@ Note that for vendored modules, that is the version listed and not the one from
long: `Vendor all module dependencies into the _vendor directory.
If a module is vendored, that is where Hugo will look for it's dependencies.
`,
withc: func(cmd *cobra.Command, r *rootCommand) {
applyLocalFlagsBuildConfig(cmd, r)
},
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
h, err := r.Hugo(flagsToCfg(cd, nil))
if err != nil {
Expand Down Expand Up @@ -203,7 +218,7 @@ Install the latest versions of all module dependencies:
Run "go help get" for more information. All flags available for "go get" is also relevant here.
` + commonUsageMod,
withc: func(cmd *cobra.Command) {
withc: func(cmd *cobra.Command, r *rootCommand) {
cmd.DisableFlagParsing = true
},
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
Expand Down
5 changes: 3 additions & 2 deletions commands/new.go
Expand Up @@ -64,11 +64,12 @@ func newNewCommand() *newCommand {
}
return create.NewContent(h, contentType, args[0], force)
},
withc: func(cmd *cobra.Command) {
withc: func(cmd *cobra.Command, r *rootCommand) {
cmd.Flags().StringVarP(&contentType, "kind", "k", "", "content type to create")
cmd.Flags().String("editor", "", "edit new content with this editor, if provided")
cmd.Flags().BoolVarP(&force, "force", "f", false, "overwrite file if it already exists")
cmd.Flags().StringVar(&format, "format", "toml", "preferred file format (toml, yaml or json)")
applyLocalFlagsBuildConfig(cmd, r)

},
},
Expand Down Expand Up @@ -147,7 +148,7 @@ Use ` + "`hugo new [contentPath]`" + ` to create new content.`,

return nil
},
withc: func(cmd *cobra.Command) {
withc: func(cmd *cobra.Command, r *rootCommand) {
cmd.Flags().BoolVarP(&force, "force", "f", false, "init inside non-empty directory")
},
},
Expand Down
2 changes: 1 addition & 1 deletion commands/release.go
Expand Up @@ -42,7 +42,7 @@ func newReleaseCommand() simplecobra.Commander {

return rel.Run()
},
withc: func(cmd *cobra.Command) {
withc: func(cmd *cobra.Command, r *rootCommand) {
cmd.Hidden = true
cmd.PersistentFlags().BoolVarP(&skipPush, "skip-push", "", false, "skip pushing to remote")
cmd.PersistentFlags().BoolVarP(&try, "try", "", false, "no changes")
Expand Down
2 changes: 1 addition & 1 deletion commands/server.go
Expand Up @@ -509,7 +509,7 @@ of a second, you will be able to save and see your changes nearly instantly.`
cmd.Flags().String("meminterval", "100ms", "interval to poll memory usage (requires --memstats), valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\".")

r := cd.Root.Command.(*rootCommand)
applyLocalBuildFlags(cmd, r)
applyLocalFlagsBuild(cmd, r)

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion commands/xcommand_template.go
Expand Up @@ -28,7 +28,7 @@ func newSimpleTemplateCommand() simplecobra.Commander {

return nil
},
withc: func(cmd *cobra.Command) {
withc: func(cmd *cobra.Command, r *rootCommand) {

},
}
Expand Down
7 changes: 7 additions & 0 deletions testscripts/commands/mod__themesdir.txt
@@ -0,0 +1,7 @@
hugo --theme mytheme mod graph
stdout 'project mytheme'

-- hugo.toml --
title = "Hugo Module"
-- themes/mytheme/hugo.toml --
title = "My Theme"
23 changes: 23 additions & 0 deletions testscripts/commands/new.txt
Expand Up @@ -25,3 +25,26 @@ stdout 'Create a new content file.'
hugo new posts/my-first-post.md
checkfile content/posts/my-first-post.md

cd ..
cd myexistingsite
hugo new post/foo.md -t mytheme
grep 'Dummy content' content/post/foo.md

-- myexistingsite/hugo.toml --
theme = "mytheme"
-- myexistingsite/content/p1.md --
---
title: "P1"
---
-- myexistingsite/themes/mytheme/hugo.toml --
-- myexistingsite/themes/mytheme/archetypes/post.md --
---
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
draft: true
---

Dummy content.



0 comments on commit 43f1282

Please sign in to comment.