diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-04-09 22:09:11 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-04-11 09:50:18 +0200 |
commit | e0621d207ce3278a82f8a60607e9cdd304149029 (patch) | |
tree | 086cebf89d337a5e65834b32fc06c0f04ea3b413 /commands/genman.go | |
parent | e26a8b242a6434117d089a0799238add7025dbf4 (diff) |
commands: Make the gen commands non-global
See #4598
Diffstat (limited to 'commands/genman.go')
-rw-r--r-- | commands/genman.go | 72 |
1 files changed, 43 insertions, 29 deletions
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), + } + 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 } |