diff --git a/commands/gen.go b/commands/gen.go index 62a84b0d0..c22d8f8b0 100644 --- a/commands/gen.go +++ b/commands/gen.go @@ -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 } diff --git a/commands/genautocomplete.go b/commands/genautocomplete.go index c2004ab22..245635454 100644 --- a/commands/genautocomplete.go +++ b/commands/genautocomplete.go @@ -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 } diff --git a/commands/gendoc.go b/commands/gendoc.go index 0fe30bf19..8c46fe98c 100644 --- a/commands/gendoc.go +++ b/commands/gendoc.go @@ -27,7 +27,19 @@ 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 @@ -35,11 +47,12 @@ 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/. @@ -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 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) } - } - 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) + "/" - } + 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.") + 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 - }, -} + return nil + }, + } -func init() { - gendocCmd.PersistentFlags().StringVar(&gendocdir, "dir", "/tmp/hugodoc/", "the directory to write the doc.") + 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 } diff --git a/commands/genman.go b/commands/genman.go index 004e669e7..fd9a49720 100644 --- a/commands/genman.go +++ b/commands/genman.go @@ -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), } - } - cmd.Root().DisableAutoGenTag = true + 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 - 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 } diff --git a/commands/hugo.go b/commands/hugo.go index 30670a30e..8f1df441a 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -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()) }