Permalink
Browse files

commands: Make the gen commands non-global

See #4598
  • Loading branch information...
bep committed Apr 9, 2018
1 parent e26a8b2 commit e0621d207ce3278a82f8a60607e9cdd304149029
Showing with 158 additions and 99 deletions.
  1. +25 −3 commands/gen.go
  2. +37 −23 commands/genautocomplete.go
  3. +52 −38 commands/gendoc.go
  4. +43 −29 commands/genman.go
  5. +1 −6 commands/hugo.go
View
@@ -17,7 +17,29 @@ import (
"github.com/spf13/cobra"
)
var genCmd = &cobra.Command{
Use: "gen",
Short: "A collection of several useful generators.",
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",
Short: "A collection of several useful generators.",
}
cc.cmd.AddCommand(
newGenautocompleteCmd().getCommand(),
newGenDocCmd().getCommand(),
newGenManCmd().getCommand(),
createGenDocsHelper().getCommand(),
createGenChromaStyles().getCommand())
return cc
}
@@ -18,15 +18,28 @@ import (
jww "github.com/spf13/jwalterweatherman"
)
var autocompleteTarget string
var _ cmder = (*genautocompleteCmd)(nil)
// bash for now (zsh and others will come)
var autocompleteType string
type genautocompleteCmd struct {
autocompleteTarget string
var genautocompleteCmd = &cobra.Command{
Use: "autocomplete",
Short: "Generate shell autocompletion script for Hugo",
Long: `Generates a shell autocompletion script for Hugo.
// bash for now (zsh and others will come)
autocompleteType string
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.
This should work for *nix systems with Bash installed.
@@ -44,27 +57,28 @@ or just source them in directly:
$ . /etc/bash_completion`,
RunE: func(cmd *cobra.Command, args []string) error {
if autocompleteType != "bash" {
return newUserError("Only Bash is supported for now")
}
RunE: func(cmd *cobra.Command, args []string) error {
if cc.autocompleteType != "bash" {
return newUserError("Only Bash is supported for now")
}
err := cmd.Root().GenBashCompletionFile(autocompleteTarget)
err := cmd.Root().GenBashCompletionFile(cc.autocompleteTarget)
if err != nil {
return err
}
if err != nil {
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() {
genautocompleteCmd.PersistentFlags().StringVarP(&autocompleteTarget, "completionfile", "", "/etc/bash_completion.d/hugo.sh", "autocompletion file")
genautocompleteCmd.PersistentFlags().StringVarP(&autocompleteType, "type", "", "bash", "autocompletion type (currently only bash supported)")
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)")
// For bash-completion
genautocompleteCmd.PersistentFlags().SetAnnotation("completionfile", cobra.BashCompFilenameExt, []string{})
cc.cmd.PersistentFlags().SetAnnotation("completionfile", cobra.BashCompFilenameExt, []string{})
return cc
}
View
@@ -27,60 +27,74 @@ import (
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
title: "%s"
slug: %s
url: %s
---
`
var gendocdir string
var gendocCmd = &cobra.Command{
Use: "doc",
Short: "Generate Markdown documentation for the Hugo CLI.",
Long: `Generate Markdown documentation for the Hugo CLI.
cc := &genDocCmd{}
cc.cmd = &cobra.Command{
Use: "doc",
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
of Hugo's command-line interface for http://gohugo.io/.
It creates one Markdown file per command with front matter suitable
for rendering in Hugo.`,
RunE: func(cmd *cobra.Command, args []string) error {
if !strings.HasSuffix(gendocdir, helpers.FilePathSeparator) {
gendocdir += helpers.FilePathSeparator
}
if found, _ := helpers.Exists(gendocdir, hugofs.Os); !found {
jww.FEEDBACK.Println("Directory", gendocdir, "does not exist, creating...")
if err := hugofs.Os.MkdirAll(gendocdir, 0777); err != nil {
return err
RunE: func(cmd *cobra.Command, args []string) error {
if !strings.HasSuffix(cc.gendocdir, helpers.FilePathSeparator) {
cc.gendocdir += helpers.FilePathSeparator
}
if found, _ := helpers.Exists(cc.gendocdir, hugofs.Os); !found {
jww.FEEDBACK.Println("Directory", cc.gendocdir, "does not exist, creating...")
if err := hugofs.Os.MkdirAll(cc.gendocdir, 0777); err != nil {
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)
}
linkHandler := func(name string) string {
base := strings.TrimSuffix(name, path.Ext(name))
return "/commands/" + strings.ToLower(base) + "/"
}
}
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 {
base := strings.TrimSuffix(name, path.Ext(name))
return "/commands/" + strings.ToLower(base) + "/"
}
jww.FEEDBACK.Println("Generating Hugo command-line documentation in", gendocdir, "...")
doc.GenMarkdownTreeCustom(cmd.Root(), gendocdir, prepender, linkHandler)
jww.FEEDBACK.Println("Done.")
return nil
},
}
func init() {
gendocCmd.PersistentFlags().StringVar(&gendocdir, "dir", "/tmp/hugodoc/", "the directory to write the doc.")
jww.FEEDBACK.Println("Generating Hugo command-line documentation in", cc.gendocdir, "...")
doc.GenMarkdownTreeCustom(cmd.Root(), cc.gendocdir, prepender, linkHandler)
jww.FEEDBACK.Println("Done.")
return nil
},
}
cc.cmd.PersistentFlags().StringVar(&cc.gendocdir, "dir", "/tmp/hugodoc/", "the directory to write the doc.")
// For bash-completion
gendocCmd.PersistentFlags().SetAnnotation("dir", cobra.BashCompSubdirsInDir, []string{})
cc.cmd.PersistentFlags().SetAnnotation("dir", cobra.BashCompSubdirsInDir, []string{})
return cc
}
View
@@ -24,43 +24,57 @@ import (
jww "github.com/spf13/jwalterweatherman"
)
var genmandir string
var genmanCmd = &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
var _ cmder = (*genManCmd)(nil)
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",
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
in the "man" directory under the current directory.`,
RunE: func(cmd *cobra.Command, args []string) error {
header := &doc.GenManHeader{
Section: "1",
Manual: "Hugo Manual",
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
RunE: func(cmd *cobra.Command, args []string) error {
header := &doc.GenManHeader{
Section: "1",
Manual: "Hugo Manual",
Source: fmt.Sprintf("Hugo %s", helpers.CurrentHugoVersion),
}
if !strings.HasSuffix(cc.genmandir, helpers.FilePathSeparator) {
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
cmd.Root().DisableAutoGenTag = true
jww.FEEDBACK.Println("Generating Hugo man pages in", genmandir, "...")
doc.GenManTree(cmd.Root(), header, genmandir)
jww.FEEDBACK.Println("Generating Hugo man pages in", cc.genmandir, "...")
doc.GenManTree(cmd.Root(), header, cc.genmandir)
jww.FEEDBACK.Println("Done.")
jww.FEEDBACK.Println("Done.")
return nil
},
}
return nil
},
}
func init() {
genmanCmd.PersistentFlags().StringVar(&genmandir, "dir", "man/", "the directory to write the man pages.")
cc.cmd.PersistentFlags().StringVar(&cc.genmandir, "dir", "man/", "the directory to write the man pages.")
// For bash-completion
genmanCmd.PersistentFlags().SetAnnotation("dir", cobra.BashCompSubdirsInDir, []string{})
cc.cmd.PersistentFlags().SetAnnotation("dir", cobra.BashCompSubdirsInDir, []string{})
return cc
}
View
@@ -204,12 +204,7 @@ func AddCommands() {
HugoCmd.AddCommand(newListCmd().getCommand())
HugoCmd.AddCommand(newImportCmd().getCommand())
HugoCmd.AddCommand(genCmd)
genCmd.AddCommand(genautocompleteCmd)
genCmd.AddCommand(gendocCmd)
genCmd.AddCommand(genmanCmd)
genCmd.AddCommand(createGenDocsHelper().getCommand())
genCmd.AddCommand(createGenChromaStyles().getCommand())
HugoCmd.AddCommand(newGenCmd().getCommand())
}

0 comments on commit e0621d2

Please sign in to comment.