ref: 4d32f2fa8969f368b088dc9bcedb45f2c986cb27
parent: 018602c46db8d729af2871bd5f4c1e7480420f09
author: Bjørn Erik Pedersen <[email protected]>
date: Tue Apr 10 05:19:26 EDT 2018
commands: Make the hugo command non-global See #4598
--- a/commands/benchmark.go
+++ b/commands/benchmark.go
@@ -23,24 +23,14 @@
jww "github.com/spf13/jwalterweatherman"
)
-var _ cmder = (*benchmarkCmd)(nil)
-
type benchmarkCmd struct {
benchmarkTimes int
cpuProfileFile string
memProfileFile string
- cmd *cobra.Command
+ *baseBuilderCmd
}
-type cmder interface {
- getCommand() *cobra.Command
-}
-
-func (c *benchmarkCmd) getCommand() *cobra.Command {
- return c.cmd
-}
-
func newBenchmarkCmd() *benchmarkCmd {
cmd := &cobra.Command{
Use: "benchmark",
@@ -49,15 +39,14 @@
creating a benchmark.`,
}
- initHugoBuilderFlags(cmd)
- initBenchmarkBuildingFlags(cmd)
+ c := &benchmarkCmd{baseBuilderCmd: newBuilderCmd(cmd)}
- c := &benchmarkCmd{cmd: cmd}
-
cmd.Flags().StringVar(&c.cpuProfileFile, "cpuprofile", "", "path/filename for the CPU profile file")
cmd.Flags().StringVar(&c.memProfileFile, "memprofile", "", "path/filename for the memory profile file")
cmd.Flags().IntVarP(&c.benchmarkTimes, "count", "n", 13, "number of times to build the site")
+ cmd.Flags().Bool("renderToMemory", false, "render to memory (only useful for benchmark testing)")
+
cmd.RunE = c.benchmark
return c
@@ -67,7 +56,7 @@
cfgInit := func(c *commandeer) error {
return nil
}
- comm, err := InitializeConfig(false, cfgInit, c.cmd)
+ comm, err := initializeConfig(false, &c.hugoBuilderCommon, c, cfgInit)
if err != nil {
return err
}
--- a/commands/check.go
+++ b/commands/check.go
@@ -20,17 +20,13 @@
var _ cmder = (*checkCmd)(nil)
type checkCmd struct {
- cmd *cobra.Command
+ *baseCmd
}
func newCheckCmd() *checkCmd {
- return &checkCmd{cmd: &cobra.Command{
+ return &checkCmd{baseCmd: &baseCmd{cmd: &cobra.Command{
Use: "check",
Short: "Contains some verification checks",
},
- }
-}
-
-func (c *checkCmd) getCommand() *cobra.Command {
- return c.cmd
+ }}
}
--- a/commands/commandeer.go
+++ b/commands/commandeer.go
@@ -40,7 +40,8 @@
type commandeer struct {
*deps.DepsCfg
- subCmdVs []*cobra.Command
+ h *hugoBuilderCommon
+ ftch flagsToConfigHandler
pathSpec *helpers.PathSpec
visitedURLs *types.EvictingStringQueue
@@ -96,7 +97,7 @@
return nil
}
-func newCommandeer(running bool, doWithCommandeer func(c *commandeer) error, subCmdVs ...*cobra.Command) (*commandeer, error) {
+func newCommandeer(running bool, h *hugoBuilderCommon, f flagsToConfigHandler, doWithCommandeer func(c *commandeer) error, subCmdVs ...*cobra.Command) (*commandeer, error) {
var rebuildDebouncer func(f func())
if running {
@@ -107,8 +108,9 @@
}
c := &commandeer{
+ h: h,
+ ftch: f,
doWithCommandeer: doWithCommandeer,
- subCmdVs: append([]*cobra.Command{hugoCmdV}, subCmdVs...),
visitedURLs: types.NewEvictingStringQueue(10),
debounce: rebuildDebouncer,
}
@@ -127,8 +129,8 @@
cfg.Running = running
var dir string
- if source != "" {
- dir, _ = filepath.Abs(source)
+ if c.h.source != "" {
+ dir, _ = filepath.Abs(c.h.source)
} else {
dir, _ = os.Getwd()
}
@@ -139,8 +141,9 @@
}
doWithConfig := func(cfg config.Provider) error {
- for _, cmdV := range c.subCmdVs {
- initializeFlags(cmdV, cfg)
+
+ if c.ftch != nil {
+ c.ftch.flagsToConfig(cfg)
}
cfg.Set("workingDir", dir)
@@ -158,7 +161,7 @@
}
config, configFiles, err := hugolib.LoadConfig(
- hugolib.ConfigSourceDescriptor{Fs: sourceFs, Path: source, WorkingDir: dir, Filename: cfgFile},
+ hugolib.ConfigSourceDescriptor{Fs: sourceFs, Path: c.h.source, WorkingDir: dir, Filename: c.h.cfgFile},
doWithCommandeer,
doWithConfig)
@@ -181,7 +184,7 @@
}
}
- logger, err := createLogger(config)
+ logger, err := c.createLogger(config)
if err != nil {
return err
}
--- a/commands/convert.go
+++ b/commands/convert.go
@@ -32,15 +32,18 @@
_ cmder = (*convertCmd)(nil)
)
+// TODO(bep) cli refactor
var outputDir string
var unsafe bool
type convertCmd struct {
- cmd *cobra.Command
+ *baseBuilderCmd
}
func newConvertCmd() *convertCmd {
- cmd := &cobra.Command{
+ cc := &convertCmd{}
+
+ cc.baseBuilderCmd = newBuilderCmd(&cobra.Command{
Use: "convert",
Short: "Convert your content to different formats",
Long: `Convert your content (e.g. front matter) to different formats.
@@ -47,9 +50,9 @@
See convert's subcommands toJSON, toTOML and toYAML for more information.`,
RunE: nil,
- }
+ })
- cmd.AddCommand(
+ cc.cmd.AddCommand(
&cobra.Command{
Use: "toJSON",
Short: "Convert front matter to JSON",
@@ -56,7 +59,7 @@
Long: `toJSON converts all front matter in the content directory
to use JSON for the front matter.`,
RunE: func(cmd *cobra.Command, args []string) error {
- return convertContents(rune([]byte(parser.JSONLead)[0]))
+ return cc.convertContents(rune([]byte(parser.JSONLead)[0]))
},
},
&cobra.Command{
@@ -65,7 +68,7 @@
Long: `toTOML converts all front matter in the content directory
to use TOML for the front matter.`,
RunE: func(cmd *cobra.Command, args []string) error {
- return convertContents(rune([]byte(parser.TOMLLead)[0]))
+ return cc.convertContents(rune([]byte(parser.TOMLLead)[0]))
},
},
&cobra.Command{
@@ -74,29 +77,26 @@
Long: `toYAML converts all front matter in the content directory
to use YAML for the front matter.`,
RunE: func(cmd *cobra.Command, args []string) error {
- return convertContents(rune([]byte(parser.YAMLLead)[0]))
+ return cc.convertContents(rune([]byte(parser.YAMLLead)[0]))
},
},
)
- cmd.PersistentFlags().StringVarP(&outputDir, "output", "o", "", "filesystem path to write files to")
- cmd.PersistentFlags().StringVarP(&source, "source", "s", "", "filesystem path to read files relative from")
- cmd.PersistentFlags().BoolVar(&unsafe, "unsafe", false, "enable less safe operations, please backup first")
- cmd.PersistentFlags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{})
+ // TODO(bep) cli refactor
+ // cmd.PersistentFlags().StringVarP(&outputDir, "output", "o", "", "filesystem path to write files to")
+ // cmd.PersistentFlags().StringVarP(&source, "source", "s", "", "filesystem path to read files relative from")
+ // cmd.PersistentFlags().BoolVar(&unsafe, "unsafe", false, "enable less safe operations, please backup first")
+ cc.cmd.PersistentFlags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{})
- return &convertCmd{cmd: cmd}
+ return cc
}
-func (c *convertCmd) getCommand() *cobra.Command {
- return c.cmd
-}
-
-func convertContents(mark rune) error {
+func (cc *convertCmd) convertContents(mark rune) error {
if outputDir == "" && !unsafe {
return newUserError("Unsafe operation not allowed, use --unsafe or set a different output path")
}
- c, err := InitializeConfig(false, nil)
+ c, err := initializeConfig(false, &cc.hugoBuilderCommon, cc, nil)
if err != nil {
return err
}
--- a/commands/env.go
+++ b/commands/env.go
@@ -23,15 +23,11 @@
var _ cmder = (*envCmd)(nil)
type envCmd struct {
- cmd *cobra.Command
+ *baseCmd
}
-func (c *envCmd) getCommand() *cobra.Command {
- return c.cmd
-}
-
func newEnvCmd() *envCmd {
- return &envCmd{cmd: &cobra.Command{
+ return &envCmd{baseCmd: newBaseCmd(&cobra.Command{
Use: "env",
Short: "Print Hugo version and environment info",
Long: `Print Hugo version and environment info. This is useful in Hugo bug reports.`,
@@ -43,6 +39,6 @@
return nil
},
- },
+ }),
}
}
--- a/commands/gen.go
+++ b/commands/gen.go
@@ -20,19 +20,15 @@
var _ cmder = (*genCmd)(nil)
type genCmd struct {
- cmd *cobra.Command
+ *baseCmd
}
-func (c *genCmd) getCommand() *cobra.Command {
- return c.cmd
-}
-
func newGenCmd() *genCmd {
cc := &genCmd{}
- cc.cmd = &cobra.Command{
+ cc.baseCmd = newBaseCmd(&cobra.Command{
Use: "gen",
Short: "A collection of several useful generators.",
- }
+ })
cc.cmd.AddCommand(
newGenautocompleteCmd().getCommand(),
--- a/commands/genautocomplete.go
+++ b/commands/genautocomplete.go
@@ -26,17 +26,13 @@
// bash for now (zsh and others will come)
autocompleteType string
- cmd *cobra.Command
+ *baseCmd
}
-func (c *genautocompleteCmd) getCommand() *cobra.Command {
- return c.cmd
-}
-
func newGenautocompleteCmd() *genautocompleteCmd {
cc := &genautocompleteCmd{}
- cc.cmd = &cobra.Command{
+ cc.baseCmd = newBaseCmd(&cobra.Command{
Use: "autocomplete",
Short: "Generate shell autocompletion script for Hugo",
Long: `Generates a shell autocompletion script for Hugo.
@@ -72,7 +68,7 @@
return nil
},
- }
+ })
cc.cmd.PersistentFlags().StringVarP(&cc.autocompleteTarget, "completionfile", "", "/etc/bash_completion.d/hugo.sh", "autocompletion file")
cc.cmd.PersistentFlags().StringVarP(&cc.autocompleteType, "type", "", "bash", "autocompletion type (currently only bash supported)")
--- a/commands/genchromastyles.go
+++ b/commands/genchromastyles.go
@@ -30,23 +30,19 @@
style string
highlightStyle string
linesStyle string
- cmd *cobra.Command
+ *baseCmd
}
-func (c *genChromaStyles) getCommand() *cobra.Command {
- return c.cmd
-}
-
// TODO(bep) highlight
func createGenChromaStyles() *genChromaStyles {
g := &genChromaStyles{
- cmd: &cobra.Command{
+ baseCmd: newBaseCmd(&cobra.Command{
Use: "chromastyles",
Short: "Generate CSS stylesheet for the Chroma code highlighter",
Long: `Generate CSS stylesheet for the Chroma code highlighter for a given style. This stylesheet is needed if pygmentsUseClasses is enabled in config.
See https://help.farbox.com/pygments.html for preview of available styles`,
- },
+ }),
}
g.cmd.RunE = func(cmd *cobra.Command, args []string) error {
--- a/commands/gendoc.go
+++ b/commands/gendoc.go
@@ -31,13 +31,9 @@
type genDocCmd struct {
gendocdir string
- cmd *cobra.Command
+ *baseCmd
}
-func (c *genDocCmd) getCommand() *cobra.Command {
- return c.cmd
-}
-
func newGenDocCmd() *genDocCmd {
const gendocFrontmatterTemplate = `---
date: %s
@@ -49,7 +45,7 @@
cc := &genDocCmd{}
- cc.cmd = &cobra.Command{
+ cc.baseCmd = newBaseCmd(&cobra.Command{
Use: "doc",
Short: "Generate Markdown documentation for the Hugo CLI.",
Long: `Generate Markdown documentation for the Hugo CLI.
@@ -89,7 +85,7 @@
return nil
},
- }
+ })
cc.cmd.PersistentFlags().StringVar(&cc.gendocdir, "dir", "/tmp/hugodoc/", "the directory to write the doc.")
--- a/commands/gendocshelper.go
+++ b/commands/gendocshelper.go
@@ -29,20 +29,16 @@
type genDocsHelper struct {
target string
- cmd *cobra.Command
+ *baseCmd
}
-func (c *genDocsHelper) getCommand() *cobra.Command {
- return c.cmd
-}
-
func createGenDocsHelper() *genDocsHelper {
g := &genDocsHelper{
- cmd: &cobra.Command{
+ baseCmd: newBaseCmd(&cobra.Command{
Use: "docshelper",
Short: "Generate some data files for the Hugo docs.",
Hidden: true,
- },
+ }),
}
g.cmd.RunE = func(cmd *cobra.Command, args []string) error {
--- a/commands/genman.go
+++ b/commands/genman.go
@@ -28,17 +28,13 @@
type genManCmd struct {
genmandir string
- cmd *cobra.Command
+ *baseCmd
}
-func (c *genManCmd) getCommand() *cobra.Command {
- return c.cmd
-}
-
func newGenManCmd() *genManCmd {
cc := &genManCmd{}
- cc.cmd = &cobra.Command{
+ cc.baseCmd = newBaseCmd(&cobra.Command{
Use: "man",
Short: "Generate man pages for the Hugo CLI",
Long: `This command automatically generates up-to-date man pages of Hugo's
@@ -69,7 +65,7 @@
return nil
},
- }
+ })
cc.cmd.PersistentFlags().StringVar(&cc.genmandir, "dir", "man/", "the directory to write the man pages.")
--- a/commands/helpers.go
+++ b/commands/helpers.go
@@ -15,6 +15,14 @@
// used by Hugo. Commands and flags are implemented using Cobra.
package commands
+import (
+ "fmt"
+ "regexp"
+
+ "github.com/gohugoio/hugo/config"
+ "github.com/spf13/cobra"
+)
+
const (
ansiEsc = "\u001B"
clearLine = "\r\033[K"
@@ -21,6 +29,15 @@
hideCursor = ansiEsc + "[?25l"
showCursor = ansiEsc + "[?25h"
)
+
+type flagsToConfigHandler interface {
+ flagsToConfig(cfg config.Provider)
+}
+
+type cmder interface {
+ flagsToConfigHandler
+ getCommand() *cobra.Command
+}
// commandError is an error used to signal different error situations in command handling.
type commandError struct {
--- a/commands/hugo.go
+++ b/commands/hugo.go
@@ -39,8 +39,6 @@
"github.com/gohugoio/hugo/parser"
flag "github.com/spf13/pflag"
- "regexp"
-
"github.com/fsnotify/fsnotify"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/hugolib"
@@ -54,137 +52,123 @@
"github.com/spf13/nitro"
)
-// Hugo represents the Hugo sites to build. This variable is exported as it
-// is used by at least one external library (the Hugo caddy plugin). We should
-// provide a cleaner external API, but until then, this is it.
-var Hugo *hugolib.HugoSites
+type baseCmd struct {
+ cmd *cobra.Command
+}
-// Reset resets Hugo ready for a new full build. This is mainly only useful
-// for benchmark testing etc. via the CLI commands.
-func Reset() error {
- Hugo = nil
- return nil
+type baseBuilderCmd struct {
+ hugoBuilderCommon
+ *baseCmd
}
-// HugoCmd is Hugo's root command.
-// Every other command attached to HugoCmd is a child command to it.
-var HugoCmd = &cobra.Command{
- Use: "hugo",
- Short: "hugo builds your site",
- Long: `hugo is the main command, used to build your Hugo site.
+func (c *baseCmd) getCommand() *cobra.Command {
+ return c.cmd
+}
-Hugo is a Fast and Flexible Static Site Generator
-built with love by spf13 and friends in Go.
+func newBaseCmd(cmd *cobra.Command) *baseCmd {
+ return &baseCmd{cmd: cmd}
+}
-Complete documentation is available at http://gohugo.io/.`,
- RunE: func(cmd *cobra.Command, args []string) error {
-
- cfgInit := func(c *commandeer) error {
- if buildWatch {
- c.Set("disableLiveReload", true)
- }
- return nil
- }
-
- c, err := InitializeConfig(buildWatch, cfgInit)
- if err != nil {
- return err
- }
-
- return c.build()
- },
+func newBuilderCmd(cmd *cobra.Command) *baseBuilderCmd {
+ bcmd := &baseBuilderCmd{baseCmd: &baseCmd{cmd: cmd}}
+ bcmd.hugoBuilderCommon.handleFlags(cmd)
+ return bcmd
}
-var hugoCmdV *cobra.Command
-
-type flagVals struct {
+// TODO(bep) cli refactor need root?
+func (c *baseCmd) flagsToConfig(cfg config.Provider) {
+ initializeFlags(c.cmd, cfg)
}
-// Flags that are to be added to commands.
-var (
- // TODO(bep) var vs string
- buildWatch bool
- logging bool
- verbose bool
- verboseLog bool
- debug bool
- quiet bool
-)
+type hugoCmd struct {
-var (
- gc bool
- baseURL string
//cacheDir string
//contentDir string
//layoutDir string
- cfgFile string
//destination string
- logFile string
//theme string
//themesDir string
- source string
//logI18nWarnings bool
//disableKinds []string
-)
-// Execute adds all child commands to the root command HugoCmd and sets flags appropriately.
-func Execute() {
- HugoCmd.SetGlobalNormalizationFunc(helpers.NormalizeHugoFlags)
+ *baseBuilderCmd
+}
- HugoCmd.SilenceUsage = true
+func newHugoCmd() *hugoCmd {
+ cc := &hugoCmd{}
- AddCommands()
+ cc.baseBuilderCmd = newBuilderCmd(&cobra.Command{
+ Use: "hugo",
+ Short: "hugo builds your site",
+ Long: `hugo is the main command, used to build your Hugo site.
- if c, err := HugoCmd.ExecuteC(); err != nil {
- if isUserError(err) {
- c.Println("")
- c.Println(c.UsageString())
- }
+Hugo is a Fast and Flexible Static Site Generator
+built with love by spf13 and friends in Go.
- os.Exit(-1)
- }
-}
+Complete documentation is available at http://gohugo.io/.`,
+ RunE: func(cmd *cobra.Command, args []string) error {
+ cfgInit := func(c *commandeer) error {
+ if cc.buildWatch {
+ c.Set("disableLiveReload", true)
+ }
+ return nil
+ }
-// AddCommands adds child commands to the root command HugoCmd.
-func AddCommands() {
- HugoCmd.AddCommand(newServerCmd().getCommand())
- HugoCmd.AddCommand(newVersionCmd().getCommand())
- HugoCmd.AddCommand(newEnvCmd().getCommand())
- HugoCmd.AddCommand(newConfigCmd().getCommand())
- HugoCmd.AddCommand(newCheckCmd().getCommand())
- HugoCmd.AddCommand(newBenchmarkCmd().getCommand())
- HugoCmd.AddCommand(newConvertCmd().getCommand())
- HugoCmd.AddCommand(newNewCmd().getCommand())
- HugoCmd.AddCommand(newListCmd().getCommand())
- HugoCmd.AddCommand(newImportCmd().getCommand())
+ c, err := initializeConfig(cc.buildWatch, &cc.hugoBuilderCommon, cc, cfgInit)
+ if err != nil {
+ return err
+ }
- HugoCmd.AddCommand(newGenCmd().getCommand())
+ return c.build()
+ },
+ })
-}
+ cc.cmd.PersistentFlags().StringVar(&cc.cfgFile, "config", "", "config file (default is path/config.yaml|json|toml)")
+ cc.cmd.PersistentFlags().BoolVar(&cc.quiet, "quiet", false, "build in quiet mode")
-// initHugoBuilderFlags initializes all common flags, typically used by the
-// core build commands, namely hugo itself, server, check and benchmark.
-func initHugoBuilderFlags(cmd *cobra.Command) {
- initHugoBuildCommonFlags(cmd)
-}
+ // Set bash-completion
+ validConfigFilenames := []string{"json", "js", "yaml", "yml", "toml", "tml"}
+ _ = cc.cmd.PersistentFlags().SetAnnotation("config", cobra.BashCompFilenameExt, validConfigFilenames)
-func initRootPersistentFlags() {
- HugoCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is path/config.yaml|json|toml)")
- HugoCmd.PersistentFlags().BoolVar(&quiet, "quiet", false, "build in quiet mode")
+ cc.cmd.PersistentFlags().BoolVarP(&cc.verbose, "verbose", "v", false, "verbose output")
+ cc.cmd.PersistentFlags().BoolVarP(&cc.debug, "debug", "", false, "debug output")
+ cc.cmd.PersistentFlags().BoolVar(&cc.logging, "log", false, "enable Logging")
+ cc.cmd.PersistentFlags().StringVar(&cc.logFile, "logFile", "", "log File path (if set, logging enabled automatically)")
+ cc.cmd.PersistentFlags().BoolVar(&cc.verboseLog, "verboseLog", false, "verbose logging")
+ cc.cmd.Flags().BoolVarP(&cc.buildWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed")
+
// Set bash-completion
- validConfigFilenames := []string{"json", "js", "yaml", "yml", "toml", "tml"}
- _ = HugoCmd.PersistentFlags().SetAnnotation("config", cobra.BashCompFilenameExt, validConfigFilenames)
+ _ = cc.cmd.PersistentFlags().SetAnnotation("logFile", cobra.BashCompFilenameExt, []string{})
+
+ return cc
}
-// initHugoBuildCommonFlags initialize common flags related to the Hugo build.
-// Called by initHugoBuilderFlags.
-func initHugoBuildCommonFlags(cmd *cobra.Command) {
+type hugoBuilderCommon struct {
+ source string
+ baseURL string
+
+ buildWatch bool
+
+ gc bool
+
+ // TODO(bep) var vs string
+ logging bool
+ verbose bool
+ verboseLog bool
+ debug bool
+ quiet bool
+
+ cfgFile string
+ logFile string
+}
+
+func (cc *hugoBuilderCommon) handleFlags(cmd *cobra.Command) {
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().StringVarP(&source, "source", "s", "", "filesystem path to read files relative from")
+ cmd.Flags().StringVarP(&cc.source, "source", "s", "", "filesystem path to read files relative from")
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/")
@@ -194,9 +178,9 @@
cmd.Flags().StringP("themesDir", "", "", "filesystem path to themes directory")
cmd.Flags().Bool("uglyURLs", false, "(deprecated) if true, use /filename.html instead of /filename/")
cmd.Flags().Bool("canonifyURLs", false, "(deprecated) if true, all relative URLs will be canonicalized using baseURL")
- cmd.Flags().StringVarP(&baseURL, "baseURL", "b", "", "hostname (and path) to the root, e.g. http://spf13.com/")
+ cmd.Flags().StringVarP(&cc.baseURL, "baseURL", "b", "", "hostname (and path) to the root, e.g. http://spf13.com/")
cmd.Flags().Bool("enableGitInfo", false, "add Git revision, date and author info to the pages")
- cmd.Flags().BoolVar(&gc, "gc", false, "enable to run some cleanup tasks (remove unused cache files) after the build")
+ cmd.Flags().BoolVar(&cc.gc, "gc", false, "enable to run some cleanup tasks (remove unused cache files) after the build")
cmd.Flags().BoolVar(&nitro.AnalysisOn, "stepAnalysis", false, "display memory and timing of different steps of the program")
cmd.Flags().Bool("templateMetrics", false, "display metrics about template executions")
@@ -218,33 +202,74 @@
_ = cmd.Flags().SetAnnotation("theme", cobra.BashCompSubdirsInDir, []string{"themes"})
}
-func initBenchmarkBuildingFlags(cmd *cobra.Command) {
- cmd.Flags().Bool("renderToMemory", false, "render to memory (only useful for benchmark testing)")
+// Hugo represents the Hugo sites to build. This variable is exported as it
+// is used by at least one external library (the Hugo caddy plugin). We should
+// provide a cleaner external API, but until then, this is it.
+var Hugo *hugolib.HugoSites
+
+// Reset resets Hugo ready for a new full build. This is mainly only useful
+// for benchmark testing etc. via the CLI commands.
+func Reset() error {
+ Hugo = nil
+ return nil
}
-// init initializes flags.
-func init() {
- HugoCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
- HugoCmd.PersistentFlags().BoolVarP(&debug, "debug", "", false, "debug output")
- HugoCmd.PersistentFlags().BoolVar(&logging, "log", false, "enable Logging")
- HugoCmd.PersistentFlags().StringVar(&logFile, "logFile", "", "log File path (if set, logging enabled automatically)")
- HugoCmd.PersistentFlags().BoolVar(&verboseLog, "verboseLog", false, "verbose logging")
+var (
+ hugoCommand = newHugoCmd()
- initRootPersistentFlags()
- initHugoBuilderFlags(HugoCmd)
- initBenchmarkBuildingFlags(HugoCmd)
+ // HugoCmd is Hugo's root command.
+ // Every other command attached to HugoCmd is a child command to it.
+ HugoCmd = hugoCommand.getCommand()
+)
- HugoCmd.Flags().BoolVarP(&buildWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed")
- hugoCmdV = HugoCmd
+// Execute adds all child commands to the root command HugoCmd and sets flags appropriately.
+func Execute() {
+ HugoCmd.SetGlobalNormalizationFunc(helpers.NormalizeHugoFlags)
- // Set bash-completion
- _ = HugoCmd.PersistentFlags().SetAnnotation("logFile", cobra.BashCompFilenameExt, []string{})
+ HugoCmd.SilenceUsage = true
+
+ addAllCommands()
+
+ if c, err := HugoCmd.ExecuteC(); err != nil {
+ if isUserError(err) {
+ c.Println("")
+ c.Println(c.UsageString())
+ }
+
+ os.Exit(-1)
+ }
}
+// addAllCommands adds child commands to the root command HugoCmd.
+func addAllCommands() {
+ addCommands(
+ newServerCmd(),
+ newVersionCmd(),
+ newEnvCmd(),
+ newConfigCmd(),
+ newCheckCmd(),
+ newBenchmarkCmd(),
+ newConvertCmd(),
+ newNewCmd(),
+ newListCmd(),
+ newImportCmd(),
+ newGenCmd(),
+ )
+}
+
+func addCommands(commands ...cmder) {
+ for _, command := range commands {
+ HugoCmd.AddCommand(command.getCommand())
+ }
+}
+
// InitializeConfig initializes a config file with sensible default configuration flags.
-func InitializeConfig(running bool, doWithCommandeer func(c *commandeer) error, subCmdVs ...*cobra.Command) (*commandeer, error) {
+func initializeConfig(running bool,
+ h *hugoBuilderCommon,
+ f flagsToConfigHandler,
+ doWithCommandeer func(c *commandeer) error) (*commandeer, error) {
- c, err := newCommandeer(running, doWithCommandeer, subCmdVs...)
+ c, err := newCommandeer(running, h, f, doWithCommandeer)
if err != nil {
return nil, err
}
@@ -253,7 +278,7 @@
}
-func createLogger(cfg config.Provider) (*jww.Notepad, error) {
+func (c *commandeer) createLogger(cfg config.Provider) (*jww.Notepad, error) {
var (
logHandle = ioutil.Discard
logThreshold = jww.LevelWarn
@@ -262,7 +287,7 @@
stdoutThreshold = jww.LevelError
)
- if verboseLog || logging || (logFile != "") {
+ if c.h.verboseLog || c.h.logging || (c.h.logFile != "") {
var err error
if logFile != "" {
logHandle, err = os.OpenFile(logFile, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
@@ -275,7 +300,7 @@
return nil, newSystemError(err)
}
}
- } else if !quiet && cfg.GetBool("verbose") {
+ } else if !c.h.quiet && cfg.GetBool("verbose") {
stdoutThreshold = jww.LevelInfo
}
@@ -283,7 +308,7 @@
stdoutThreshold = jww.LevelDebug
}
- if verboseLog {
+ if c.h.verboseLog {
logThreshold = jww.LevelInfo
if cfg.GetBool("debug") {
logThreshold = jww.LevelDebug
@@ -381,7 +406,7 @@
langCount map[string]uint64
)
- if !quiet {
+ if !c.h.quiet {
fmt.Print(hideCursor + "Building sites … ")
defer func() {
fmt.Print(showCursor + clearLine)
@@ -424,7 +449,7 @@
s.ProcessingStats.Static = langCount[s.Language.Lang]
}
- if gc {
+ if c.h.gc {
count, err := Hugo.GC()
if err != nil {
return err
@@ -447,13 +472,13 @@
}
// TODO(bep) Feedback?
- if !quiet {
+ if !c.h.quiet {
fmt.Println()
Hugo.PrintProcessingStats(os.Stdout)
fmt.Println()
}
- if buildWatch {
+ if c.h.buildWatch {
watchDirs, err := c.getDirList()
if err != nil {
return err
@@ -481,7 +506,7 @@
}
// TODO(bep) Feedback?
- if !quiet {
+ if !c.h.quiet {
fmt.Println()
Hugo.PrintProcessingStats(os.Stdout)
fmt.Println()
@@ -613,7 +638,7 @@
}
func (c *commandeer) timeTrack(start time.Time, name string) {
- if quiet {
+ if c.h.quiet {
return
}
elapsed := time.Since(start)
@@ -765,7 +790,7 @@
if err := c.initSites(); err != nil {
return err
}
- if !quiet {
+ if !c.h.quiet {
c.Logger.FEEDBACK.Println("Started building sites ...")
}
return Hugo.Build(hugolib.BuildCfg{CreateSitesFromConfig: true})
@@ -775,7 +800,7 @@
if err = c.initSites(); err != nil {
return
}
- if !quiet {
+ if !c.h.quiet {
c.Logger.FEEDBACK.Println("Started building sites ...")
}
return Hugo.Build(hugolib.BuildCfg{ResetState: true})
@@ -811,7 +836,7 @@
return err
}
visited := c.visitedURLs.PeekAllSet()
- doLiveReload := !buildWatch && !c.Cfg.GetBool("disableLiveReload")
+ doLiveReload := !c.h.buildWatch && !c.Cfg.GetBool("disableLiveReload")
if doLiveReload && !c.Cfg.GetBool("disableFastRender") {
// Make sure we always render the home pages
@@ -833,7 +858,7 @@
jww.ERROR.Println("Failed to reload config:", err)
} else if err := c.recreateAndBuildSites(true); err != nil {
jww.ERROR.Println(err)
- } else if !buildWatch && !c.Cfg.GetBool("disableLiveReload") {
+ } else if !c.h.buildWatch && !c.Cfg.GetBool("disableLiveReload") {
livereload.ForceRefresh()
}
}
@@ -1013,7 +1038,7 @@
}
}
- if !buildWatch && !c.Cfg.GetBool("disableLiveReload") {
+ if !c.h.buildWatch && !c.Cfg.GetBool("disableLiveReload") {
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initialized
// force refresh when more than one file
@@ -1030,7 +1055,7 @@
}
if len(dynamicEvents) > 0 {
- doLiveReload := !buildWatch && !c.Cfg.GetBool("disableLiveReload")
+ doLiveReload := !c.h.buildWatch && !c.Cfg.GetBool("disableLiveReload")
onePageName := pickOneWriteOrCreatePath(dynamicEvents)
c.Logger.FEEDBACK.Println("\nChange detected, rebuilding site")
--- a/commands/import_jekyll.go
+++ b/commands/import_jekyll.go
@@ -35,20 +35,16 @@
jww "github.com/spf13/jwalterweatherman"
)
-var _ cmder = (*newThemeCmd)(nil)
+var _ cmder = (*importCmd)(nil)
type importCmd struct {
- cmd *cobra.Command
+ *baseCmd
}
-func (c *importCmd) getCommand() *cobra.Command {
- return c.cmd
-}
-
func newImportCmd() *importCmd {
cc := &importCmd{}
- cc.cmd = &cobra.Command{
+ cc.baseCmd = newBaseCmd(&cobra.Command{
Use: "import",
Short: "Import your site from others.",
Long: `Import your site from other web site generators like Jekyll.
@@ -55,7 +51,7 @@
Import requires a subcommand, e.g. ` + "`hugo import jekyll jekyll_root_path target_path`.",
RunE: nil,
- }
+ })
importJekyllCmd := &cobra.Command{
Use: "jekyll",
@@ -72,9 +68,6 @@
return cc
-}
-
-func init() {
}
func (i *importCmd) importFromJekyll(cmd *cobra.Command, args []string) error {
--- a/commands/limit_darwin.go
+++ b/commands/limit_darwin.go
@@ -23,7 +23,7 @@
var _ cmder = (*limitCmd)(nil)
type limitCmd struct {
- cmd *cobra.Command
+ *baseCmd
}
func newLimitCmd() *limitCmd {
@@ -58,11 +58,7 @@
},
}
- return &limitCmd{cmd: ccmd}
-}
-
-func (c *limitCmd) getCommand() *cobra.Command {
- return c.cmd
+ return &limitCmd{baseCmd: newBaseCmd(ccmd)}
}
func init() {
--- a/commands/list.go
+++ b/commands/list.go
@@ -24,17 +24,14 @@
var _ cmder = (*listCmd)(nil)
type listCmd struct {
- cmd *cobra.Command
+ hugoBuilderCommon
+ *baseCmd
}
-func (c *listCmd) getCommand() *cobra.Command {
- return c.cmd
-}
-
func newListCmd() *listCmd {
cc := &listCmd{}
- cc.cmd = &cobra.Command{
+ cc.baseCmd = newBaseCmd(&cobra.Command{
Use: "list",
Short: "Listing out various types of content",
Long: `Listing out various types of content.
@@ -41,7 +38,7 @@
List requires a subcommand, e.g. ` + "`hugo list drafts`.",
RunE: nil,
- }
+ })
cc.cmd.AddCommand(
&cobra.Command{
@@ -53,7 +50,7 @@
c.Set("buildDrafts", true)
return nil
}
- c, err := InitializeConfig(false, cfgInit)
+ c, err := initializeConfig(false, &cc.hugoBuilderCommon, cc, cfgInit)
if err != nil {
return err
}
@@ -89,7 +86,7 @@
c.Set("buildFuture", true)
return nil
}
- c, err := InitializeConfig(false, cfgInit)
+ c, err := initializeConfig(false, &cc.hugoBuilderCommon, cc, cfgInit)
if err != nil {
return err
}
@@ -125,7 +122,7 @@
c.Set("buildExpired", true)
return nil
}
- c, err := InitializeConfig(false, cfgInit)
+ c, err := initializeConfig(false, &cc.hugoBuilderCommon, cc, cfgInit)
if err != nil {
return err
}
@@ -153,7 +150,8 @@
},
)
- cc.cmd.PersistentFlags().StringVarP(&source, "source", "s", "", "filesystem path to read files relative from")
+ // TODO(bep) cli refactor
+ // cc.cmd.PersistentFlags().StringVarP(&source, "source", "s", "", "filesystem path to read files relative from")
cc.cmd.PersistentFlags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{})
return cc
--- a/commands/list_config.go
+++ b/commands/list_config.go
@@ -25,27 +25,23 @@
var _ cmder = (*configCmd)(nil)
type configCmd struct {
- cmd *cobra.Command
+ *baseCmd
}
-func (c *configCmd) getCommand() *cobra.Command {
- return c.cmd
-}
-
func newConfigCmd() *configCmd {
cc := &configCmd{}
- cc.cmd = &cobra.Command{
+ cc.baseCmd = newBaseCmd(&cobra.Command{
Use: "config",
Short: "Print the site configuration",
Long: `Print the site configuration, both default and custom settings.`,
RunE: cc.printConfig,
- }
+ })
return cc
}
func (c *configCmd) printConfig(cmd *cobra.Command, args []string) error {
- cfg, err := InitializeConfig(false, nil, c.cmd)
+ cfg, err := initializeConfig(false, nil, c, nil)
if err != nil {
return err
--- a/commands/new.go
+++ b/commands/new.go
@@ -33,15 +33,11 @@
contentEditor string
contentType string
- cmd *cobra.Command
+ *baseCmd
}
-func (c *newCmd) getCommand() *cobra.Command {
- return c.cmd
-}
-
func newNewCmd() *newCmd {
- ccmd := &newCmd{}
+ ccmd := &newCmd{baseCmd: newBaseCmd(nil)}
cmd := &cobra.Command{
Use: "new [path]",
Short: "Create new content for your site",
@@ -57,7 +53,7 @@
cmd.Flags().StringVarP(&ccmd.contentType, "kind", "k", "", "content type to create")
// TODO(bep) cli refactor
- cmd.PersistentFlags().StringVarP(&source, "source", "s", "", "filesystem path to read files relative from")
+ // cmd.PersistentFlags().StringVarP(&source, "source", "s", "", "filesystem path to read files relative from")
cmd.PersistentFlags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{})
cmd.Flags().StringVar(&ccmd.contentEditor, "editor", "", "edit new content with this editor, if provided")
@@ -77,7 +73,7 @@
return nil
}
- c, err := InitializeConfig(false, cfgInit)
+ c, err := initializeConfig(false, nil, n, cfgInit)
if err != nil {
return err
--- a/commands/new_site.go
+++ b/commands/new_site.go
@@ -40,13 +40,9 @@
type newSiteCmd struct {
configFormat string
- cmd *cobra.Command
+ *baseCmd
}
-func (c *newSiteCmd) getCommand() *cobra.Command {
- return c.cmd
-}
-
func newNewSiteCmd() *newSiteCmd {
ccmd := &newSiteCmd{}
@@ -62,7 +58,7 @@
cmd.Flags().StringVarP(&ccmd.configFormat, "format", "f", "toml", "config & frontmatter format")
cmd.Flags().Bool("force", false, "init inside non-empty directory")
- ccmd.cmd = cmd
+ ccmd.baseCmd = newBaseCmd(cmd)
return ccmd
--- a/commands/new_theme.go
+++ b/commands/new_theme.go
@@ -31,15 +31,11 @@
var _ cmder = (*newThemeCmd)(nil)
type newThemeCmd struct {
- cmd *cobra.Command
+ *baseCmd
}
-func (c *newThemeCmd) getCommand() *cobra.Command {
- return c.cmd
-}
-
func newNewThemeCmd() *newThemeCmd {
- ccmd := &newThemeCmd{}
+ ccmd := &newThemeCmd{newBaseCmd(nil)}
cmd := &cobra.Command{
Use: "theme [name]",
@@ -57,7 +53,7 @@
}
func (n *newThemeCmd) newTheme(cmd *cobra.Command, args []string) error {
- c, err := InitializeConfig(false, nil)
+ c, err := initializeConfig(false, nil, n, nil)
if err != nil {
return err
--- a/commands/release.go
+++ b/commands/release.go
@@ -23,7 +23,8 @@
)
func init() {
- HugoCmd.AddCommand(createReleaser().cmd)
+ // TODO(bep) cli refactor
+ //HugoCmd.AddCommand(createReleaser().cmd)
}
type releaseCommandeer struct {
--- a/commands/server.go
+++ b/commands/server.go
@@ -38,9 +38,9 @@
jww "github.com/spf13/jwalterweatherman"
)
-var _ cmder = (*serverCmd)(nil)
-
type serverCmd struct {
+ hugoBuilderCommon
+
disableLiveReload bool
navigateToChanged bool
renderToDisk bool
@@ -53,17 +53,31 @@
disableFastRender bool
- cmd *cobra.Command
+ *baseCmd
}
-func (c *serverCmd) getCommand() *cobra.Command {
- return c.cmd
+func (cc *serverCmd) handleFlags(cmd *cobra.Command) {
+ // TODO(bep) cli refactor fields vs strings
+ cc.cmd.Flags().IntVarP(&cc.serverPort, "port", "p", 1313, "port on which the server will listen")
+ cc.cmd.Flags().IntVar(&cc.liveReloadPort, "liveReloadPort", -1, "port for live reloading (i.e. 443 in HTTPS proxy situations)")
+ cc.cmd.Flags().StringVarP(&cc.serverInterface, "bind", "", "127.0.0.1", "interface to which the server will bind")
+ cc.cmd.Flags().BoolVarP(&cc.serverWatch, "watch", "w", true, "watch filesystem for changes and recreate as needed")
+ cc.cmd.Flags().BoolVar(&cc.noHTTPCache, "noHTTPCache", false, "prevent HTTP caching")
+ cc.cmd.Flags().BoolVarP(&cc.serverAppend, "appendPort", "", true, "append port to baseURL")
+ cc.cmd.Flags().BoolVar(&cc.disableLiveReload, "disableLiveReload", false, "watch without enabling live browser reload on rebuild")
+ cc.cmd.Flags().BoolVar(&cc.navigateToChanged, "navigateToChanged", false, "navigate to changed content file on live browser reload")
+ cc.cmd.Flags().BoolVar(&cc.renderToDisk, "renderToDisk", false, "render to Destination path (default is render to memory & serve from there)")
+ cc.cmd.Flags().BoolVar(&cc.disableFastRender, "disableFastRender", false, "enables full re-renders on changes")
+
+ cc.cmd.Flags().String("memstats", "", "log memory usage to this file")
+ cc.cmd.Flags().String("meminterval", "100ms", "interval to poll memory usage (requires --memstats), valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\".")
+
}
func newServerCmd() *serverCmd {
cc := &serverCmd{}
- cc.cmd = &cobra.Command{
+ cc.baseCmd = newBaseCmd(&cobra.Command{
Use: "server",
Aliases: []string{"serve"},
Short: "A high performance webserver",
@@ -80,25 +94,8 @@
and push the latest content to them. As most Hugo sites are built in a fraction
of a second, you will be able to save and see your changes nearly instantly.`,
RunE: cc.server,
- }
+ })
- initHugoBuilderFlags(cc.cmd)
-
- // TODO(bep) cli refactor fields vs strings
- cc.cmd.Flags().IntVarP(&cc.serverPort, "port", "p", 1313, "port on which the server will listen")
- cc.cmd.Flags().IntVar(&cc.liveReloadPort, "liveReloadPort", -1, "port for live reloading (i.e. 443 in HTTPS proxy situations)")
- cc.cmd.Flags().StringVarP(&cc.serverInterface, "bind", "", "127.0.0.1", "interface to which the server will bind")
- cc.cmd.Flags().BoolVarP(&cc.serverWatch, "watch", "w", true, "watch filesystem for changes and recreate as needed")
- cc.cmd.Flags().BoolVar(&cc.noHTTPCache, "noHTTPCache", false, "prevent HTTP caching")
- cc.cmd.Flags().BoolVarP(&cc.serverAppend, "appendPort", "", true, "append port to baseURL")
- cc.cmd.Flags().BoolVar(&cc.disableLiveReload, "disableLiveReload", false, "watch without enabling live browser reload on rebuild")
- cc.cmd.Flags().BoolVar(&cc.navigateToChanged, "navigateToChanged", false, "navigate to changed content file on live browser reload")
- cc.cmd.Flags().BoolVar(&cc.renderToDisk, "renderToDisk", false, "render to Destination path (default is render to memory & serve from there)")
- cc.cmd.Flags().BoolVar(&cc.disableFastRender, "disableFastRender", false, "enables full re-renders on changes")
-
- cc.cmd.Flags().String("memstats", "", "log memory usage to this file")
- cc.cmd.Flags().String("meminterval", "100ms", "interval to poll memory usage (requires --memstats), valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\".")
-
return cc
}
@@ -122,10 +119,6 @@
return nil, nil
}
-func init() {
-
-}
-
var serverPorts []int
func (s *serverCmd) server(cmd *cobra.Command, args []string) error {
@@ -212,7 +205,7 @@
serverPort = serverPorts[0]
}
- baseURL, err := s.fixURL(language, baseURL, serverPort)
+ baseURL, err := s.fixURL(language, s.baseURL, serverPort)
if err != nil {
return nil
}
@@ -232,7 +225,7 @@
jww.ERROR.Println("memstats error:", err)
}
- c, err := InitializeConfig(true, cfgInit, s.cmd)
+ c, err := initializeConfig(true, &s.hugoBuilderCommon, s, cfgInit)
// TODO(bep) cli refactor
if err != nil {
return err
@@ -308,7 +301,7 @@
httpFs := afero.NewHttpFs(f.c.Fs.Destination)
fs := filesOnlyFs{httpFs.Dir(absPublishDir)}
- doLiveReload := !buildWatch && !f.c.Cfg.GetBool("disableLiveReload")
+ doLiveReload := !f.s.buildWatch && !f.c.Cfg.GetBool("disableLiveReload")
fastRenderMode := doLiveReload && !f.c.Cfg.GetBool("disableFastRender")
if i == 0 && fastRenderMode {
--- a/commands/version.go
+++ b/commands/version.go
@@ -26,16 +26,12 @@
var _ cmder = (*versionCmd)(nil)
type versionCmd struct {
- cmd *cobra.Command
+ *baseCmd
}
-func (c *versionCmd) getCommand() *cobra.Command {
- return c.cmd
-}
-
func newVersionCmd() *versionCmd {
return &versionCmd{
- &cobra.Command{
+ newBaseCmd(&cobra.Command{
Use: "version",
Short: "Print the version number of Hugo",
Long: `All software has versions. This is Hugo's.`,
@@ -43,7 +39,7 @@
printHugoVersion()
return nil
},
- },
+ }),
}
}