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)
Use: "gen",
Short: "A collection of several useful generators.", 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",
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,15 +18,28 @@ import (
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
) )
var autocompleteTarget string var _ cmder = (*genautocompleteCmd)(nil)
// bash for now (zsh and others will come) type genautocompleteCmd struct {
var autocompleteType string autocompleteTarget string
var genautocompleteCmd = &cobra.Command{ // bash for now (zsh and others will come)
Use: "autocomplete", autocompleteType string
Short: "Generate shell autocompletion script for Hugo",
Long: `Generates a shell autocompletion script for Hugo. cmd *cobra.Command
}
func (c *genautocompleteCmd) getCommand() *cobra.Command {
return c.cmd
}
func newGenautocompleteCmd() *genautocompleteCmd {
cc := &genautocompleteCmd{}
cc.cmd = &cobra.Command{
Use: "autocomplete",
Short: "Generate shell autocompletion script for Hugo",
Long: `Generates a shell autocompletion script for Hugo.
NOTE: The current version supports Bash only. NOTE: The current version supports Bash only.
This should work for *nix systems with Bash installed. This should work for *nix systems with Bash installed.
@ -44,27 +57,28 @@ 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,7 +27,19 @@ import (
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
) )
const gendocFrontmatterTemplate = `--- 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 = `---
date: %s date: %s
title: "%s" title: "%s"
slug: %s slug: %s
@ -35,11 +47,12 @@ url: %s
--- ---
` `
var gendocdir string cc := &genDocCmd{}
var gendocCmd = &cobra.Command{
Use: "doc", cc.cmd = &cobra.Command{
Short: "Generate Markdown documentation for the Hugo CLI.", Use: "doc",
Long: `Generate Markdown documentation for the Hugo CLI. Short: "Generate Markdown documentation for the Hugo CLI.",
Long: `Generate Markdown documentation for the Hugo CLI.
This command is, mostly, used to create up-to-date documentation This command is, mostly, used to create up-to-date documentation
of Hugo's command-line interface for http://gohugo.io/. of Hugo's command-line interface for http://gohugo.io/.
@ -47,40 +60,41 @@ of Hugo's command-line interface for http://gohugo.io/.
It creates one Markdown file per command with front matter suitable 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(time.RFC3339)
prepender := func(filename string) string {
name := filepath.Base(filename)
base := strings.TrimSuffix(name, path.Ext(name))
url := "/commands/" + strings.ToLower(base) + "/"
return fmt.Sprintf(gendocFrontmatterTemplate, now, strings.Replace(base, "_", " ", -1), base, url)
} }
}
now := time.Now().Format("2006-01-02")
prepender := func(filename string) string {
name := filepath.Base(filename)
base := strings.TrimSuffix(name, path.Ext(name))
url := "/commands/" + strings.ToLower(base) + "/"
return fmt.Sprintf(gendocFrontmatterTemplate, now, strings.Replace(base, "_", " ", -1), base, url)
}
linkHandler := func(name string) string { linkHandler := func(name string) string {
base := strings.TrimSuffix(name, path.Ext(name)) base := strings.TrimSuffix(name, path.Ext(name))
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,43 +24,57 @@ import (
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
) )
var genmandir string var _ cmder = (*genManCmd)(nil)
var genmanCmd = &cobra.Command{
Use: "man", type genManCmd struct {
Short: "Generate man pages for the Hugo CLI", genmandir string
Long: `This command automatically generates up-to-date man pages of Hugo's cmd *cobra.Command
}
func (c *genManCmd) getCommand() *cobra.Command {
return c.cmd
}
func newGenManCmd() *genManCmd {
cc := &genManCmd{}
cc.cmd = &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
command-line interface. By default, it creates the man page files command-line interface. By default, it creates the man page files
in the "man" directory under the current directory.`, in the "man" directory under the current directory.`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
header := &doc.GenManHeader{ header := &doc.GenManHeader{
Section: "1", Section: "1",
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) {
genmandir += helpers.FilePathSeparator
}
if found, _ := helpers.Exists(genmandir, hugofs.Os); !found {
jww.FEEDBACK.Println("Directory", genmandir, "does not exist, creating...")
if err := hugofs.Os.MkdirAll(genmandir, 0777); err != nil {
return err
} }
} if !strings.HasSuffix(cc.genmandir, helpers.FilePathSeparator) {
cmd.Root().DisableAutoGenTag = true cc.genmandir += helpers.FilePathSeparator
}
if found, _ := helpers.Exists(cc.genmandir, hugofs.Os); !found {
jww.FEEDBACK.Println("Directory", cc.genmandir, "does not exist, creating...")
if err := hugofs.Os.MkdirAll(cc.genmandir, 0777); err != nil {
return err
}
}
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.")
return nil return nil
}, },
} }
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())
} }