From a8f7fbbb10aa78f3ebac008d29d9969bb197393c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 11 Apr 2018 07:54:08 +0200 Subject: [PATCH] commands: Move the commands related logic to its own file See #4598 --- commands/commandeer.go | 2 +- commands/commands.go | 207 +++++++++++++++++++++++++++++++++++++++ commands/hugo.go | 185 ---------------------------------- commands/limit_darwin.go | 5 - 4 files changed, 208 insertions(+), 191 deletions(-) create mode 100644 commands/commands.go diff --git a/commands/commandeer.go b/commands/commandeer.go index ba38735c2..9b6e51585 100644 --- a/commands/commandeer.go +++ b/commands/commandeer.go @@ -1,4 +1,4 @@ -// Copyright 2017 The Hugo Authors. All rights reserved. +// Copyright 2018 The Hugo Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/commands/commands.go b/commands/commands.go new file mode 100644 index 000000000..0f6cb762f --- /dev/null +++ b/commands/commands.go @@ -0,0 +1,207 @@ +// Copyright 2017 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package commands + +import ( + "github.com/gohugoio/hugo/config" + "github.com/gohugoio/hugo/helpers" + "github.com/spf13/cobra" + + "github.com/spf13/nitro" +) + +// newHugoCompleteCmd builds the complete set of Hugo CLI commands. +func newHugoCompleteCmd() *cobra.Command { + hugoCmd := newHugoCmd().getCommand() + addAllCommands(hugoCmd) + return hugoCmd +} + +// addAllCommands adds child commands to the root command HugoCmd. +func addAllCommands(root *cobra.Command) { + addCommands( + root, + newServerCmd(), + newVersionCmd(), + newEnvCmd(), + newConfigCmd(), + newCheckCmd(), + newBenchmarkCmd(), + newConvertCmd(), + newNewCmd(), + newListCmd(), + newImportCmd(), + newGenCmd(), + ) +} + +func addCommands(root *cobra.Command, commands ...cmder) { + for _, command := range commands { + root.AddCommand(command.getCommand()) + } +} + +type baseCmd struct { + cmd *cobra.Command +} + +type baseBuilderCmd struct { + hugoBuilderCommon + *baseCmd +} + +func (c *baseCmd) getCommand() *cobra.Command { + return c.cmd +} + +func newBaseCmd(cmd *cobra.Command) *baseCmd { + return &baseCmd{cmd: cmd} +} + +func newBuilderCmd(cmd *cobra.Command) *baseBuilderCmd { + bcmd := &baseBuilderCmd{baseCmd: &baseCmd{cmd: cmd}} + bcmd.hugoBuilderCommon.handleFlags(cmd) + return bcmd +} + +// TODO(bep) cli refactor need root? +func (c *baseCmd) flagsToConfig(cfg config.Provider) { + initializeFlags(c.cmd, cfg) +} + +type hugoCmd struct { + + //cacheDir string + //contentDir string + //layoutDir string + //destination string + //theme string + //themesDir string + //logI18nWarnings bool + //disableKinds []string + + *baseBuilderCmd +} + +func newHugoCmd() *hugoCmd { + cc := &hugoCmd{} + + cc.baseBuilderCmd = newBuilderCmd(&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 cc.buildWatch { + c.Set("disableLiveReload", true) + } + return nil + } + + c, err := initializeConfig(cc.buildWatch, &cc.hugoBuilderCommon, cc, cfgInit) + if err != nil { + return err + } + + 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") + + // Set bash-completion + validConfigFilenames := []string{"json", "js", "yaml", "yml", "toml", "tml"} + _ = 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{}) + + cc.cmd.SetGlobalNormalizationFunc(helpers.NormalizeHugoFlags) + cc.cmd.SilenceUsage = true + + return cc +} + +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(&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/") + cmd.Flags().BoolP("ignoreCache", "", false, "ignores the cache directory") + cmd.Flags().StringP("destination", "d", "", "filesystem path to write files to") + cmd.Flags().StringP("theme", "t", "", "theme to use (located in /themes/THEMENAME/)") + 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(&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(&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") + cmd.Flags().Bool("templateMetricsHints", false, "calculate some improvement hints when combined with --templateMetrics") + cmd.Flags().Bool("pluralizeListTitles", true, "(deprecated) pluralize titles in lists using inflect") + cmd.Flags().Bool("preserveTaxonomyNames", false, `(deprecated) preserve taxonomy names as written ("Gérard Depardieu" vs "gerard-depardieu")`) + cmd.Flags().BoolP("forceSyncStatic", "", false, "copy all files when static is changed.") + cmd.Flags().BoolP("noTimes", "", false, "don't sync modification time of files") + cmd.Flags().BoolP("noChmod", "", false, "don't sync permission mode of files") + cmd.Flags().BoolP("i18n-warnings", "", false, "print missing translations") + cmd.Flags().Bool("renderToMemory", false, "render to memory (only useful for benchmark testing)") + + cmd.Flags().StringSlice("disableKinds", []string{}, "disable different kind of pages (home, RSS etc.)") + + // Set bash-completion. + // Each flag must first be defined before using the SetAnnotation() call. + _ = cmd.Flags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{}) + _ = cmd.Flags().SetAnnotation("cacheDir", cobra.BashCompSubdirsInDir, []string{}) + _ = cmd.Flags().SetAnnotation("destination", cobra.BashCompSubdirsInDir, []string{}) + _ = cmd.Flags().SetAnnotation("theme", cobra.BashCompSubdirsInDir, []string{"themes"}) +} diff --git a/commands/hugo.go b/commands/hugo.go index b3e5c67c6..209607198 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -49,163 +49,8 @@ import ( "github.com/spf13/cobra" "github.com/spf13/fsync" jww "github.com/spf13/jwalterweatherman" - "github.com/spf13/nitro" ) -type baseCmd struct { - cmd *cobra.Command -} - -type baseBuilderCmd struct { - hugoBuilderCommon - *baseCmd -} - -func (c *baseCmd) getCommand() *cobra.Command { - return c.cmd -} - -func newBaseCmd(cmd *cobra.Command) *baseCmd { - return &baseCmd{cmd: cmd} -} - -func newBuilderCmd(cmd *cobra.Command) *baseBuilderCmd { - bcmd := &baseBuilderCmd{baseCmd: &baseCmd{cmd: cmd}} - bcmd.hugoBuilderCommon.handleFlags(cmd) - return bcmd -} - -// TODO(bep) cli refactor need root? -func (c *baseCmd) flagsToConfig(cfg config.Provider) { - initializeFlags(c.cmd, cfg) -} - -type hugoCmd struct { - - //cacheDir string - //contentDir string - //layoutDir string - //destination string - //theme string - //themesDir string - //logI18nWarnings bool - //disableKinds []string - - *baseBuilderCmd -} - -func newHugoCmd() *hugoCmd { - cc := &hugoCmd{} - - cc.baseBuilderCmd = newBuilderCmd(&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 cc.buildWatch { - c.Set("disableLiveReload", true) - } - return nil - } - - c, err := initializeConfig(cc.buildWatch, &cc.hugoBuilderCommon, cc, cfgInit) - if err != nil { - return err - } - - 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") - - // Set bash-completion - validConfigFilenames := []string{"json", "js", "yaml", "yml", "toml", "tml"} - _ = 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{}) - - cc.cmd.SetGlobalNormalizationFunc(helpers.NormalizeHugoFlags) - cc.cmd.SilenceUsage = true - - return cc -} - -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(&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/") - cmd.Flags().BoolP("ignoreCache", "", false, "ignores the cache directory") - cmd.Flags().StringP("destination", "d", "", "filesystem path to write files to") - cmd.Flags().StringP("theme", "t", "", "theme to use (located in /themes/THEMENAME/)") - 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(&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(&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") - cmd.Flags().Bool("templateMetricsHints", false, "calculate some improvement hints when combined with --templateMetrics") - cmd.Flags().Bool("pluralizeListTitles", true, "(deprecated) pluralize titles in lists using inflect") - cmd.Flags().Bool("preserveTaxonomyNames", false, `(deprecated) preserve taxonomy names as written ("Gérard Depardieu" vs "gerard-depardieu")`) - cmd.Flags().BoolP("forceSyncStatic", "", false, "copy all files when static is changed.") - cmd.Flags().BoolP("noTimes", "", false, "don't sync modification time of files") - cmd.Flags().BoolP("noChmod", "", false, "don't sync permission mode of files") - cmd.Flags().BoolP("i18n-warnings", "", false, "print missing translations") - cmd.Flags().Bool("renderToMemory", false, "render to memory (only useful for benchmark testing)") - - cmd.Flags().StringSlice("disableKinds", []string{}, "disable different kind of pages (home, RSS etc.)") - - // Set bash-completion. - // Each flag must first be defined before using the SetAnnotation() call. - _ = cmd.Flags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{}) - _ = cmd.Flags().SetAnnotation("cacheDir", cobra.BashCompSubdirsInDir, []string{}) - _ = cmd.Flags().SetAnnotation("destination", cobra.BashCompSubdirsInDir, []string{}) - _ = cmd.Flags().SetAnnotation("theme", cobra.BashCompSubdirsInDir, []string{"themes"}) -} - // 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. @@ -232,36 +77,6 @@ func Execute() { } } -func newHugoCompleteCmd() *cobra.Command { - hugoCmd := newHugoCmd().getCommand() - addAllCommands(hugoCmd) - return hugoCmd -} - -// addAllCommands adds child commands to the root command HugoCmd. -func addAllCommands(root *cobra.Command) { - addCommands( - root, - newServerCmd(), - newVersionCmd(), - newEnvCmd(), - newConfigCmd(), - newCheckCmd(), - newBenchmarkCmd(), - newConvertCmd(), - newNewCmd(), - newListCmd(), - newImportCmd(), - newGenCmd(), - ) -} - -func addCommands(root *cobra.Command, commands ...cmder) { - for _, command := range commands { - root.AddCommand(command.getCommand()) - } -} - // InitializeConfig initializes a config file with sensible default configuration flags. func initializeConfig(running bool, h *hugoBuilderCommon, diff --git a/commands/limit_darwin.go b/commands/limit_darwin.go index 20341fa1c..7dce75b3e 100644 --- a/commands/limit_darwin.go +++ b/commands/limit_darwin.go @@ -61,11 +61,6 @@ This is primarily to ensure that Hugo can watch enough files on some OSs`, return &limitCmd{baseCmd: newBaseCmd(ccmd)} } -func init() { - // TODO(bep) cli refactor - //checkCmdOld.AddCommand(limit) -} - func tweakLimit() { var rLimit syscall.Rlimit err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)