genman.go (2335B)
1 // Copyright 2016 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package commands 15 16 import ( 17 "fmt" 18 "strings" 19 20 "github.com/gohugoio/hugo/common/hugo" 21 "github.com/gohugoio/hugo/helpers" 22 "github.com/gohugoio/hugo/hugofs" 23 "github.com/spf13/cobra" 24 "github.com/spf13/cobra/doc" 25 jww "github.com/spf13/jwalterweatherman" 26 ) 27 28 var _ cmder = (*genManCmd)(nil) 29 30 type genManCmd struct { 31 genmandir string 32 *baseCmd 33 } 34 35 func newGenManCmd() *genManCmd { 36 cc := &genManCmd{} 37 38 cc.baseCmd = newBaseCmd(&cobra.Command{ 39 Use: "man", 40 Short: "Generate man pages for the Hugo CLI", 41 Long: `This command automatically generates up-to-date man pages of Hugo's 42 command-line interface. By default, it creates the man page files 43 in the "man" directory under the current directory.`, 44 45 RunE: func(cmd *cobra.Command, args []string) error { 46 header := &doc.GenManHeader{ 47 Section: "1", 48 Manual: "Hugo Manual", 49 Source: fmt.Sprintf("Hugo %s", hugo.CurrentVersion), 50 } 51 if !strings.HasSuffix(cc.genmandir, helpers.FilePathSeparator) { 52 cc.genmandir += helpers.FilePathSeparator 53 } 54 if found, _ := helpers.Exists(cc.genmandir, hugofs.Os); !found { 55 jww.FEEDBACK.Println("Directory", cc.genmandir, "does not exist, creating...") 56 if err := hugofs.Os.MkdirAll(cc.genmandir, 0777); err != nil { 57 return err 58 } 59 } 60 cmd.Root().DisableAutoGenTag = true 61 62 jww.FEEDBACK.Println("Generating Hugo man pages in", cc.genmandir, "...") 63 doc.GenManTree(cmd.Root(), header, cc.genmandir) 64 65 jww.FEEDBACK.Println("Done.") 66 67 return nil 68 }, 69 }) 70 71 cc.cmd.PersistentFlags().StringVar(&cc.genmandir, "dir", "man/", "the directory to write the man pages.") 72 73 // For bash-completion 74 cc.cmd.PersistentFlags().SetAnnotation("dir", cobra.BashCompSubdirsInDir, []string{}) 75 76 return cc 77 }