Permalink
Browse files

commands: Make the hugo command non-global

See #4598
  • Loading branch information...
bep committed Apr 10, 2018
1 parent 018602c commit 4d32f2fa8969f368b088dc9bcedb45f2c986cb27
View
@@ -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
}
View
@@ -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
}}
}
View
@@ -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
}
View
@@ -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
}
View
@@ -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
},
},
}),
}
}
View
@@ -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(),
@@ -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)")
@@ -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 {
View
@@ -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.")
View
@@ -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 {
Oops, something went wrong.

0 comments on commit 4d32f2f

Please sign in to comment.