github.com/elmarschill/hugo_sample@v0.47.1/commands/genman.go (about)

     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/helpers"
    21  	"github.com/gohugoio/hugo/hugofs"
    22  	"github.com/spf13/cobra"
    23  	"github.com/spf13/cobra/doc"
    24  	jww "github.com/spf13/jwalterweatherman"
    25  )
    26  
    27  var _ cmder = (*genManCmd)(nil)
    28  
    29  type genManCmd struct {
    30  	genmandir string
    31  	*baseCmd
    32  }
    33  
    34  func newGenManCmd() *genManCmd {
    35  	cc := &genManCmd{}
    36  
    37  	cc.baseCmd = newBaseCmd(&cobra.Command{
    38  		Use:   "man",
    39  		Short: "Generate man pages for the Hugo CLI",
    40  		Long: `This command automatically generates up-to-date man pages of Hugo's
    41  command-line interface.  By default, it creates the man page files
    42  in the "man" directory under the current directory.`,
    43  
    44  		RunE: func(cmd *cobra.Command, args []string) error {
    45  			header := &doc.GenManHeader{
    46  				Section: "1",
    47  				Manual:  "Hugo Manual",
    48  				Source:  fmt.Sprintf("Hugo %s", helpers.CurrentHugoVersion),
    49  			}
    50  			if !strings.HasSuffix(cc.genmandir, helpers.FilePathSeparator) {
    51  				cc.genmandir += helpers.FilePathSeparator
    52  			}
    53  			if found, _ := helpers.Exists(cc.genmandir, hugofs.Os); !found {
    54  				jww.FEEDBACK.Println("Directory", cc.genmandir, "does not exist, creating...")
    55  				if err := hugofs.Os.MkdirAll(cc.genmandir, 0777); err != nil {
    56  					return err
    57  				}
    58  			}
    59  			cmd.Root().DisableAutoGenTag = true
    60  
    61  			jww.FEEDBACK.Println("Generating Hugo man pages in", cc.genmandir, "...")
    62  			doc.GenManTree(cmd.Root(), header, cc.genmandir)
    63  
    64  			jww.FEEDBACK.Println("Done.")
    65  
    66  			return nil
    67  		},
    68  	})
    69  
    70  	cc.cmd.PersistentFlags().StringVar(&cc.genmandir, "dir", "man/", "the directory to write the man pages.")
    71  
    72  	// For bash-completion
    73  	cc.cmd.PersistentFlags().SetAnnotation("dir", cobra.BashCompSubdirsInDir, []string{})
    74  
    75  	return cc
    76  }