summaryrefslogtreecommitdiffstats
path: root/commands/gendoc.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-04-09 22:09:11 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-04-11 09:50:18 +0200
commite0621d207ce3278a82f8a60607e9cdd304149029 (patch)
tree086cebf89d337a5e65834b32fc06c0f04ea3b413 /commands/gendoc.go
parente26a8b242a6434117d089a0799238add7025dbf4 (diff)
commands: Make the gen commands non-global
See #4598
Diffstat (limited to 'commands/gendoc.go')
-rw-r--r--commands/gendoc.go90
1 files changed, 52 insertions, 38 deletions
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)
+ }
+
+ 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
}