diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-04-10 09:19:26 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-04-11 09:50:19 +0200 |
commit | 4d32f2fa8969f368b088dc9bcedb45f2c986cb27 (patch) | |
tree | a091c49f6011605f08b92b9dbdb2d2acdd87f9ce /commands | |
parent | 018602c46db8d729af2871bd5f4c1e7480420f09 (diff) |
commands: Make the hugo command non-global
See #4598
Diffstat (limited to 'commands')
-rw-r--r-- | commands/benchmark.go | 21 | ||||
-rw-r--r-- | commands/check.go | 10 | ||||
-rw-r--r-- | commands/commandeer.go | 21 | ||||
-rw-r--r-- | commands/convert.go | 36 | ||||
-rw-r--r-- | commands/env.go | 10 | ||||
-rw-r--r-- | commands/gen.go | 10 | ||||
-rw-r--r-- | commands/genautocomplete.go | 10 | ||||
-rw-r--r-- | commands/genchromastyles.go | 10 | ||||
-rw-r--r-- | commands/gendoc.go | 10 | ||||
-rw-r--r-- | commands/gendocshelper.go | 10 | ||||
-rw-r--r-- | commands/genman.go | 10 | ||||
-rw-r--r-- | commands/helpers.go | 17 | ||||
-rw-r--r-- | commands/hugo.go | 293 | ||||
-rw-r--r-- | commands/import_jekyll.go | 15 | ||||
-rw-r--r-- | commands/limit_darwin.go | 8 | ||||
-rw-r--r-- | commands/list.go | 20 | ||||
-rw-r--r-- | commands/list_config.go | 12 | ||||
-rw-r--r-- | commands/new.go | 12 | ||||
-rw-r--r-- | commands/new_site.go | 8 | ||||
-rw-r--r-- | commands/new_theme.go | 10 | ||||
-rw-r--r-- | commands/release.go | 3 | ||||
-rw-r--r-- | commands/server.go | 55 | ||||
-rw-r--r-- | commands/version.go | 10 |
23 files changed, 292 insertions, 329 deletions
diff --git a/commands/benchmark.go b/commands/benchmark.go index ae5d436e8..b1291cc45 100644 --- a/commands/benchmark.go +++ b/commands/benchmark.go @@ -23,22 +23,12 @@ import ( jww "github.com/spf13/jwalterweatherman" ) -var _ cmder = (*benchmarkCmd)(nil) - type benchmarkCmd struct { benchmarkTimes int cpuProfileFile string memProfileFile string - cmd *cobra.Command -} - -type cmder interface { - getCommand() *cobra.Command -} - -func (c *benchmarkCmd) getCommand() *cobra.Command { - return c.cmd + *baseBuilderCmd } func newBenchmarkCmd() *benchmarkCmd { @@ -49,15 +39,14 @@ func newBenchmarkCmd() *benchmarkCmd { creating a benchmark.`, } - initHugoBuilderFlags(cmd) - initBenchmarkBuildingFlags(cmd) - - c := &benchmarkCmd{cmd: cmd} + c := &benchmarkCmd{baseBuilderCmd: newBuilderCmd(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 @@ func (c *benchmarkCmd) benchmark(cmd *cobra.Command, args []string) error { 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 } diff --git a/commands/check.go b/commands/check.go index 5812bb6aa..f20a18b02 100644 --- a/commands/check.go +++ b/commands/check.go @@ -20,17 +20,13 @@ import ( 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 + }} } diff --git a/commands/commandeer.go b/commands/commandeer.go index 7d053f249..4c8abd7d8 100644 --- a/commands/commandeer.go +++ b/commands/commandeer.go @@ -40,7 +40,8 @@ import ( type commandeer struct { *deps.DepsCfg - subCmdVs []*cobra.Command + h *hugoBuilderCommon + ftch flagsToConfigHandler pathSpec *helpers.PathSpec visitedURLs *types.EvictingStringQueue @@ -96,7 +97,7 @@ func (c *commandeer) initFs(fs *hugofs.Fs) error { 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 @@ func newCommandeer(running bool, doWithCommandeer func(c *commandeer) error, sub } c := &commandeer{ + h: h, + ftch: f, doWithCommandeer: doWithCommandeer, - subCmdVs: append([]*cobra.Command{hugoCmdV}, subCmdVs...), visitedURLs: types.NewEvictingStringQueue(10), debounce: rebuildDebouncer, } @@ -127,8 +129,8 @@ func (c *commandeer) loadConfig(running bool) error { 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 @@ func (c *commandeer) loadConfig(running bool) error { } 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 @@ func (c *commandeer) loadConfig(running bool) error { } 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 @@ func (c *commandeer) loadConfig(running bool) error { } } - logger, err := createLogger(config) + logger, err := c.createLogger(config) if err != nil { return err } diff --git a/commands/convert.go b/commands/convert.go index cc07fe087..9e0a66026 100644 --- a/commands/convert.go +++ b/commands/convert.go @@ -32,31 +32,34 @@ var ( _ 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. 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", 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 @@ to use JSON for the front matter.`, 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 @@ to use TOML for the front matter.`, 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{}) - - return &convertCmd{cmd: cmd} -} + // 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{}) -func (c *convertCmd) getCommand() *cobra.Command { - return c.cmd + return cc } -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 } diff --git a/commands/env.go b/commands/env.go index 700cddf5a..76c16b93b 100644 --- a/commands/env.go +++ b/commands/env.go @@ -23,15 +23,11 @@ import ( var _ cmder = (*envCmd)(nil) type envCmd struct { - cmd *cobra.Command -} - -func (c *envCmd) getCommand() *cobra.Command { - return c.cmd + *baseCmd } 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 @@ func newEnvCmd() *envCmd { return nil }, - }, + }), } } diff --git a/commands/gen.go b/commands/gen.go index c22d8f8b0..6878cfe70 100644 --- a/commands/gen.go +++ b/commands/gen.go @@ -20,19 +20,15 @@ import ( var _ cmder = (*genCmd)(nil) type genCmd struct { - cmd *cobra.Command -} - -func (c *genCmd) getCommand() *cobra.Command { - return c.cmd + *baseCmd } 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(), diff --git a/commands/genautocomplete.go b/commands/genautocomplete.go index 245635454..b0b98abb4 100644 --- a/commands/genautocomplete.go +++ b/commands/genautocomplete.go @@ -26,17 +26,13 @@ type genautocompleteCmd struct { // bash for now (zsh and others will come) autocompleteType string - cmd *cobra.Command -} - -func (c *genautocompleteCmd) getCommand() *cobra.Command { - return c.cmd + *baseCmd } 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 @@ or just source them in directly: 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)") diff --git a/commands/genchromastyles.go b/commands/genchromastyles.go index 6d7b8b5cb..a2231e56e 100644 --- a/commands/genchromastyles.go +++ b/commands/genchromastyles.go @@ -30,23 +30,19 @@ type genChromaStyles struct { style string highlightStyle string linesStyle string - cmd *cobra.Command -} - -func (c *genChromaStyles) getCommand() *cobra.Command { - return c.cmd + *baseCmd } // 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 { diff --git a/commands/gendoc.go b/commands/gendoc.go index 8c46fe98c..3446c2622 100644 --- a/commands/gendoc.go +++ b/commands/gendoc.go @@ -31,11 +31,7 @@ var _ cmder = (*genDocCmd)(nil) type genDocCmd struct { gendocdir string - cmd *cobra.Command -} - -func (c *genDocCmd) getCommand() *cobra.Command { - return c.cmd + *baseCmd } func newGenDocCmd() *genDocCmd { @@ -49,7 +45,7 @@ url: %s 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 @@ for rendering in Hugo.`, return nil }, - } + }) cc.cmd.PersistentFlags().StringVar(&cc.gendocdir, "dir", "/tmp/hugodoc/", "the directory to write the doc.") diff --git a/commands/gendocshelper.go b/commands/gendocshelper.go index e98bfde79..c243581f6 100644 --- a/commands/gendocshelper.go +++ b/commands/gendocshelper.go @@ -29,20 +29,16 @@ var ( type genDocsHelper struct { target string - cmd *cobra.Command -} - -func (c *genDocsHelper) getCommand() *cobra.Command { - return c.cmd + *baseCmd } 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 { diff --git a/commands/genman.go b/commands/genman.go index fd9a49720..ac4eaf8d1 100644 --- a/commands/genman.go +++ b/commands/genman.go @@ -28,17 +28,13 @@ var _ cmder = (*genManCmd)(nil) type genManCmd struct { genmandir string - cmd *cobra.Command -} - -func (c *genManCmd) getCommand() *cobra.Command { - return c.cmd + *baseCmd } 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 @@ in the "man" directory under the current directory.`, return nil }, - } + }) cc.cmd.PersistentFlags().StringVar(&cc.genmandir, "dir", "man/", "the directory to write the man pages.") diff --git a/commands/helpers.go b/commands/helpers.go index 78e549d22..1386e425f 100644 --- 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" @@ -22,6 +30,15 @@ const ( 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 { s string diff --git a/commands/hugo.go b/commands/hugo.go index 3f468dd7d..1da764d93 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -39,8 +39,6 @@ import ( "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 @@ import ( "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 - -// 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 baseCmd struct { + cmd *cobra.Command } -// 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. - -Hugo is a Fast and Flexible Static Site Generator -built with love by spf13 and friends in Go. - -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 - } +type baseBuilderCmd struct { + hugoBuilderCommon + *baseCmd +} - c, err := InitializeConfig(buildWatch, cfgInit) - if err != nil { - return err - } +func (c *baseCmd) getCommand() *cobra.Command { + return c.cmd +} - return c.build() - }, +func newBaseCmd(cmd *cobra.Command) *baseCmd { + return &baseCmd{cmd: cmd} } -var hugoCmdV *cobra.Command +func newBuilderCmd(cmd *cobra.Command) *baseBuilderCmd { + bcmd := &baseBuilderCmd{baseCmd: &baseCmd{cmd: cmd}} + bcmd.hugoBuilderCommon.handleFlags(cmd) + return bcmd +} -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) - - HugoCmd.SilenceUsage = true + *baseBuilderCmd +} - AddCommands() +func newHugoCmd() *hugoCmd { + cc := &hugoCmd{} - if c, err := HugoCmd.ExecuteC(); err != nil { - if isUserError(err) { - c.Println("") - c.Println(c.UsageString()) - } + cc.baseBuilderCmd = newBuilderCmd(&cobra.Command{ + Use: "hugo", + Short: "hugo builds your site", + Long: `hugo is the main command, used to build your Hugo site. - os.Exit(-1) - } -} +Hugo is a Fast and Flexible Static Site Generator +built with love by spf13 and friends in Go. -// 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()) - - HugoCmd.AddCommand(newGenCmd().getCommand()) +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 + } -} + c, err := initializeConfig(cc.buildWatch, &cc.hugoBuilderCommon, cc, cfgInit) + if err != nil { + return err + } -// 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) -} + return c.build() + }, + }) -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().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") // Set bash-completion validConfigFilenames := []string{"json", "js", "yaml", "yml", "toml", "tml"} - _ = HugoCmd.PersistentFlags().SetAnnotation("config", cobra.BashCompFilenameExt, validConfigFilenames) + _ = cc.cmd.PersistentFlags().SetAnnotation("config", cobra.BashCompFilenameExt, validConfigFilenames) + + 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 + _ = cc.cmd.PersistentFlags().SetAnnotation("logFile", cobra.BashCompFilenameExt, []string{}) + + return cc +} + +type hugoBuilderCommon struct { + source string + baseURL string + + buildWatch bool + + gc bool + + // TODO(bep) var vs string |