]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
commands: Reinstate some of the removed build flags (e.g. --theme) to new and mod
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 28 May 2023 08:44:40 +0000 (10:44 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 28 May 2023 10:55:44 +0000 (12:55 +0200)
Fixes #11018

13 files changed:
commands/commandeer.go
commands/convert.go
commands/deploy.go
commands/deploy_off.go
commands/gen.go
commands/import.go
commands/mod.go
commands/new.go
commands/release.go
commands/server.go
commands/xcommand_template.go
testscripts/commands/mod__themesdir.txt [new file with mode: 0644]
testscripts/commands/new.txt

index 7b0c416522ebb67e77090720c5983903ff7bf0a4..23781f57cb16f77efca3d62c6952494cb0bf0ab0 100644 (file)
@@ -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.
@@ -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")
@@ -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"})
 
 }
 
@@ -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
@@ -593,6 +598,7 @@ 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
@@ -600,13 +606,12 @@ func (c *simpleCommand) Init(cd *simplecobra.Commandeer) error {
                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)
        }
index e5c3679136c0a0a60b84963b746c632f9f26ca0b..765fb5f549f0c3e2333e0f25d8c945cf6fc84e9b 100644 (file)
@@ -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{
@@ -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{
@@ -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) {
                                },
                        },
                },
index 0340ea3c42a5f753ca13bbe95587048586b9ffd6..8dae4bd882446831dc2359c768c465563474aab3 100644 (file)
@@ -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")
index 5e9b91f1636b743e9cb77574292d6a9f7532b866..b238d6cf7951244d9255e26e912df9410b4acd16 100644 (file)
@@ -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
                },
        }
index 1f8109e01c771eceb4dcab011ba72c73a65bc20a..c5eab894af4f055deed95428282e810097bb68d1 100644 (file)
@@ -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)")
@@ -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{})
@@ -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{})
@@ -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")
                        },
index 258323a3a5c4159136608b62faaebb78830648bb..30ada15f8f9d3a0ff84d893936cbf9fbafe59fc7 100644 (file)
@@ -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")
                                },
                        },
index 1af1f74314abc60d854f62e62526cdadef515814..36d4a5596f069244e1a9a0f0c5d2e6190120e226 100644 (file)
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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")
                                },
@@ -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 {
@@ -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 {
@@ -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 {
index fc9a0bd8c165346ce95a2cafbf38dabe02eac91d..6760bf71920509aaedbef79c3607b680cf4a54d5 100644 (file)
@@ -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)
 
                                },
                        },
@@ -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")
                                },
                        },
index fe3c5efb66b9c6789cf2c22842fbcb9f21f97fbb..54cf936e861dcd37092f0616692a6206c92dc0ed 100644 (file)
@@ -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")
index 4669cf0b4ed7b5bf9bcfee373653ce925de9d75d..c878cac2ff45d9d3a8b460fd9e9b6549e933bb06 100644 (file)
@@ -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
 }
index 7ceeffb19da4ca5ddad57343a3bcd360e8cc63b4..eeb9409a05290834c27011f8480bb8f60eb0a5e7 100644 (file)
@@ -28,7 +28,7 @@ func newSimpleTemplateCommand() simplecobra.Commander {
 
                        return nil
                },
-               withc: func(cmd *cobra.Command) {
+               withc: func(cmd *cobra.Command, r *rootCommand) {
 
                },
        }
diff --git a/testscripts/commands/mod__themesdir.txt b/testscripts/commands/mod__themesdir.txt
new file mode 100644 (file)
index 0000000..ec76d0c
--- /dev/null
@@ -0,0 +1,7 @@
+hugo --theme mytheme mod graph
+stdout 'project mytheme'
+
+-- hugo.toml --
+title = "Hugo Module"
+-- themes/mytheme/hugo.toml --
+title = "My Theme"
index aeebba66d0c7aa0cf3f191953aa799939469d88e..aad0d80f6a3eaffb2aa0cd29d2bfaea5e70ae4bf 100644 (file)
@@ -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.
+
+
+