commands: Make the gen commands non-global

See #4598
This commit is contained in:
Bjørn Erik Pedersen 2018-04-09 22:09:11 +02:00
parent e26a8b242a
commit e0621d207c
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
5 changed files with 155 additions and 96 deletions

View file

@ -17,7 +17,29 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var genCmd = &cobra.Command{ var _ cmder = (*genCmd)(nil)
type genCmd struct {
cmd *cobra.Command
}
func (c *genCmd) getCommand() *cobra.Command {
return c.cmd
}
func newGenCmd() *genCmd {
cc := &genCmd{}
cc.cmd = &cobra.Command{
Use: "gen", Use: "gen",
Short: "A collection of several useful generators.", Short: "A collection of several useful generators.",
} }
cc.cmd.AddCommand(
newGenautocompleteCmd().getCommand(),
newGenDocCmd().getCommand(),
newGenManCmd().getCommand(),
createGenDocsHelper().getCommand(),
createGenChromaStyles().getCommand())
return cc
}

View file

@ -18,12 +18,25 @@ import (
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
) )
var autocompleteTarget string var _ cmder = (*genautocompleteCmd)(nil)
type genautocompleteCmd struct {
autocompleteTarget string
// bash for now (zsh and others will come) // bash for now (zsh and others will come)
var autocompleteType string autocompleteType string
var genautocompleteCmd = &cobra.Command{ cmd *cobra.Command
}
func (c *genautocompleteCmd) getCommand() *cobra.Command {
return c.cmd
}
func newGenautocompleteCmd() *genautocompleteCmd {
cc := &genautocompleteCmd{}
cc.cmd = &cobra.Command{
Use: "autocomplete", Use: "autocomplete",
Short: "Generate shell autocompletion script for Hugo", Short: "Generate shell autocompletion script for Hugo",
Long: `Generates a shell autocompletion script for Hugo. Long: `Generates a shell autocompletion script for Hugo.
@ -45,26 +58,27 @@ or just source them in directly:
$ . /etc/bash_completion`, $ . /etc/bash_completion`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
if autocompleteType != "bash" { if cc.autocompleteType != "bash" {
return newUserError("Only Bash is supported for now") return newUserError("Only Bash is supported for now")
} }
err := cmd.Root().GenBashCompletionFile(autocompleteTarget) err := cmd.Root().GenBashCompletionFile(cc.autocompleteTarget)
if err != nil { if err != nil {
return err return err
} }
jww.FEEDBACK.Println("Bash completion file for Hugo saved to", autocompleteTarget) jww.FEEDBACK.Println("Bash completion file for Hugo saved to", cc.autocompleteTarget)
return nil return nil
}, },
} }
func init() { cc.cmd.PersistentFlags().StringVarP(&cc.autocompleteTarget, "completionfile", "", "/etc/bash_completion.d/hugo.sh", "autocompletion file")
genautocompleteCmd.PersistentFlags().StringVarP(&autocompleteTarget, "completionfile", "", "/etc/bash_completion.d/hugo.sh", "autocompletion file") cc.cmd.PersistentFlags().StringVarP(&cc.autocompleteType, "type", "", "bash", "autocompletion type (currently only bash supported)")
genautocompleteCmd.PersistentFlags().StringVarP(&autocompleteType, "type", "", "bash", "autocompletion type (currently only bash supported)")
// For bash-completion // For bash-completion
genautocompleteCmd.PersistentFlags().SetAnnotation("completionfile", cobra.BashCompFilenameExt, []string{}) cc.cmd.PersistentFlags().SetAnnotation("completionfile", cobra.BashCompFilenameExt, []string{})
return cc
} }

View file

@ -27,6 +27,18 @@ import (
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
) )
var _ cmder = (*genDocCmd)(nil)
type genDocCmd struct {
gendocdir string
cmd *cobra.Command
}
func (c *genDocCmd) getCommand() *cobra.Command {
return c.cmd
}
func newGenDocCmd() *genDocCmd {
const gendocFrontmatterTemplate = `--- const gendocFrontmatterTemplate = `---
date: %s date: %s
title: "%s" title: "%s"
@ -35,8 +47,9 @@ url: %s
--- ---
` `
var gendocdir string cc := &genDocCmd{}
var gendocCmd = &cobra.Command{
cc.cmd = &cobra.Command{
Use: "doc", Use: "doc",
Short: "Generate Markdown documentation for the Hugo CLI.", Short: "Generate Markdown documentation for the Hugo CLI.",
Long: `Generate Markdown documentation for the Hugo CLI. Long: `Generate Markdown documentation for the Hugo CLI.
@ -48,16 +61,16 @@ It creates one Markdown file per command with front matter suitable
for rendering in Hugo.`, for rendering in Hugo.`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
if !strings.HasSuffix(gendocdir, helpers.FilePathSeparator) { if !strings.HasSuffix(cc.gendocdir, helpers.FilePathSeparator) {
gendocdir += helpers.FilePathSeparator cc.gendocdir += helpers.FilePathSeparator
} }
if found, _ := helpers.Exists(gendocdir, hugofs.Os); !found { if found, _ := helpers.Exists(cc.gendocdir, hugofs.Os); !found {
jww.FEEDBACK.Println("Directory", gendocdir, "does not exist, creating...") jww.FEEDBACK.Println("Directory", cc.gendocdir, "does not exist, creating...")
if err := hugofs.Os.MkdirAll(gendocdir, 0777); err != nil { if err := hugofs.Os.MkdirAll(cc.gendocdir, 0777); err != nil {
return err return err
} }
} }
now := time.Now().Format("2006-01-02") now := time.Now().Format(time.RFC3339)
prepender := func(filename string) string { prepender := func(filename string) string {
name := filepath.Base(filename) name := filepath.Base(filename)
base := strings.TrimSuffix(name, path.Ext(name)) base := strings.TrimSuffix(name, path.Ext(name))
@ -70,17 +83,18 @@ for rendering in Hugo.`,
return "/commands/" + strings.ToLower(base) + "/" return "/commands/" + strings.ToLower(base) + "/"
} }
jww.FEEDBACK.Println("Generating Hugo command-line documentation in", gendocdir, "...") jww.FEEDBACK.Println("Generating Hugo command-line documentation in", cc.gendocdir, "...")
doc.GenMarkdownTreeCustom(cmd.Root(), gendocdir, prepender, linkHandler) doc.GenMarkdownTreeCustom(cmd.Root(), cc.gendocdir, prepender, linkHandler)
jww.FEEDBACK.Println("Done.") jww.FEEDBACK.Println("Done.")
return nil return nil
}, },
} }
func init() { cc.cmd.PersistentFlags().StringVar(&cc.gendocdir, "dir", "/tmp/hugodoc/", "the directory to write the doc.")
gendocCmd.PersistentFlags().StringVar(&gendocdir, "dir", "/tmp/hugodoc/", "the directory to write the doc.")
// For bash-completion // For bash-completion
gendocCmd.PersistentFlags().SetAnnotation("dir", cobra.BashCompSubdirsInDir, []string{}) cc.cmd.PersistentFlags().SetAnnotation("dir", cobra.BashCompSubdirsInDir, []string{})
return cc
} }

View file

@ -24,8 +24,21 @@ import (
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
) )
var genmandir string var _ cmder = (*genManCmd)(nil)
var genmanCmd = &cobra.Command{
type genManCmd struct {
genmandir string
cmd *cobra.Command
}
func (c *genManCmd) getCommand() *cobra.Command {
return c.cmd
}
func newGenManCmd() *genManCmd {
cc := &genManCmd{}
cc.cmd = &cobra.Command{
Use: "man", Use: "man",
Short: "Generate man pages for the Hugo CLI", Short: "Generate man pages for the Hugo CLI",
Long: `This command automatically generates up-to-date man pages of Hugo's Long: `This command automatically generates up-to-date man pages of Hugo's
@ -38,19 +51,19 @@ in the "man" directory under the current directory.`,
Manual: "Hugo Manual", Manual: "Hugo Manual",
Source: fmt.Sprintf("Hugo %s", helpers.CurrentHugoVersion), Source: fmt.Sprintf("Hugo %s", helpers.CurrentHugoVersion),
} }
if !strings.HasSuffix(genmandir, helpers.FilePathSeparator) { if !strings.HasSuffix(cc.genmandir, helpers.FilePathSeparator) {
genmandir += helpers.FilePathSeparator cc.genmandir += helpers.FilePathSeparator
} }
if found, _ := helpers.Exists(genmandir, hugofs.Os); !found { if found, _ := helpers.Exists(cc.genmandir, hugofs.Os); !found {
jww.FEEDBACK.Println("Directory", genmandir, "does not exist, creating...") jww.FEEDBACK.Println("Directory", cc.genmandir, "does not exist, creating...")
if err := hugofs.Os.MkdirAll(genmandir, 0777); err != nil { if err := hugofs.Os.MkdirAll(cc.genmandir, 0777); err != nil {
return err return err
} }
} }
cmd.Root().DisableAutoGenTag = true cmd.Root().DisableAutoGenTag = true
jww.FEEDBACK.Println("Generating Hugo man pages in", genmandir, "...") jww.FEEDBACK.Println("Generating Hugo man pages in", cc.genmandir, "...")
doc.GenManTree(cmd.Root(), header, genmandir) doc.GenManTree(cmd.Root(), header, cc.genmandir)
jww.FEEDBACK.Println("Done.") jww.FEEDBACK.Println("Done.")
@ -58,9 +71,10 @@ in the "man" directory under the current directory.`,
}, },
} }
func init() { cc.cmd.PersistentFlags().StringVar(&cc.genmandir, "dir", "man/", "the directory to write the man pages.")
genmanCmd.PersistentFlags().StringVar(&genmandir, "dir", "man/", "the directory to write the man pages.")
// For bash-completion // For bash-completion
genmanCmd.PersistentFlags().SetAnnotation("dir", cobra.BashCompSubdirsInDir, []string{}) cc.cmd.PersistentFlags().SetAnnotation("dir", cobra.BashCompSubdirsInDir, []string{})
return cc
} }

View file

@ -204,12 +204,7 @@ func AddCommands() {
HugoCmd.AddCommand(newListCmd().getCommand()) HugoCmd.AddCommand(newListCmd().getCommand())
HugoCmd.AddCommand(newImportCmd().getCommand()) HugoCmd.AddCommand(newImportCmd().getCommand())
HugoCmd.AddCommand(genCmd) HugoCmd.AddCommand(newGenCmd().getCommand())
genCmd.AddCommand(genautocompleteCmd)
genCmd.AddCommand(gendocCmd)
genCmd.AddCommand(genmanCmd)
genCmd.AddCommand(createGenDocsHelper().getCommand())
genCmd.AddCommand(createGenChromaStyles().getCommand())
} }