gitlab.azmi.pl/azmi-open-source/helm@v3.0.0-beta.3+incompatible/cmd/helm/docs.go (about)

     1  /*
     2  Copyright The Helm Authors.
     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  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package main
    17  
    18  import (
    19  	"io"
    20  	"path/filepath"
    21  
    22  	"github.com/pkg/errors"
    23  	"github.com/spf13/cobra"
    24  	"github.com/spf13/cobra/doc"
    25  
    26  	"helm.sh/helm/cmd/helm/require"
    27  )
    28  
    29  const docsDesc = `
    30  Generate documentation files for Helm.
    31  
    32  This command can generate documentation for Helm in the following formats:
    33  
    34  - Markdown
    35  - Man pages
    36  
    37  It can also generate bash autocompletions.
    38  
    39  	$ helm docs markdown -dir mydocs/
    40  `
    41  
    42  type docsOptions struct {
    43  	dest          string
    44  	docTypeString string
    45  	topCmd        *cobra.Command
    46  }
    47  
    48  func newDocsCmd(out io.Writer) *cobra.Command {
    49  	o := &docsOptions{}
    50  
    51  	cmd := &cobra.Command{
    52  		Use:    "docs",
    53  		Short:  "Generate documentation as markdown or man pages",
    54  		Long:   docsDesc,
    55  		Hidden: true,
    56  		Args:   require.NoArgs,
    57  		RunE: func(cmd *cobra.Command, args []string) error {
    58  			o.topCmd = cmd.Root()
    59  			return o.run(out)
    60  		},
    61  	}
    62  
    63  	f := cmd.Flags()
    64  	f.StringVar(&o.dest, "dir", "./", "directory to which documentation is written")
    65  	f.StringVar(&o.docTypeString, "type", "markdown", "the type of documentation to generate (markdown, man, bash)")
    66  
    67  	return cmd
    68  }
    69  
    70  func (o *docsOptions) run(out io.Writer) error {
    71  	switch o.docTypeString {
    72  	case "markdown", "mdown", "md":
    73  		return doc.GenMarkdownTree(o.topCmd, o.dest)
    74  	case "man":
    75  		manHdr := &doc.GenManHeader{Title: "HELM", Section: "1"}
    76  		return doc.GenManTree(o.topCmd, manHdr, o.dest)
    77  	case "bash":
    78  		return o.topCmd.GenBashCompletionFile(filepath.Join(o.dest, "completions.bash"))
    79  	default:
    80  		return errors.Errorf("unknown doc type %q. Try 'markdown' or 'man'", o.docTypeString)
    81  	}
    82  }