github.com/Racer159/helm-experiment@v0.0.0-20230822001441-1eb31183f614/src/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 cmd 17 18 import ( 19 "fmt" 20 "io" 21 "path" 22 "path/filepath" 23 "strings" 24 25 "github.com/pkg/errors" 26 "github.com/spf13/cobra" 27 "github.com/spf13/cobra/doc" 28 "golang.org/x/text/cases" 29 "golang.org/x/text/language" 30 31 "helm.sh/helm/v3/cmd/helm/require" 32 ) 33 34 const docsDesc = ` 35 Generate documentation files for Helm. 36 37 This command can generate documentation for Helm in the following formats: 38 39 - Markdown 40 - Man pages 41 42 It can also generate bash autocompletions. 43 ` 44 45 type docsOptions struct { 46 dest string 47 docTypeString string 48 topCmd *cobra.Command 49 generateHeaders bool 50 } 51 52 func newDocsCmd(out io.Writer) *cobra.Command { 53 o := &docsOptions{} 54 55 cmd := &cobra.Command{ 56 Use: "docs", 57 Short: "generate documentation as markdown or man pages", 58 Long: docsDesc, 59 Hidden: true, 60 Args: require.NoArgs, 61 ValidArgsFunction: noCompletions, 62 RunE: func(cmd *cobra.Command, args []string) error { 63 o.topCmd = cmd.Root() 64 return o.run(out) 65 }, 66 } 67 68 f := cmd.Flags() 69 f.StringVar(&o.dest, "dir", "./", "directory to which documentation is written") 70 f.StringVar(&o.docTypeString, "type", "markdown", "the type of documentation to generate (markdown, man, bash)") 71 f.BoolVar(&o.generateHeaders, "generate-headers", false, "generate standard headers for markdown files") 72 73 cmd.RegisterFlagCompletionFunc("type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 74 return []string{"bash", "man", "markdown"}, cobra.ShellCompDirectiveNoFileComp 75 }) 76 77 return cmd 78 } 79 80 func (o *docsOptions) run(out io.Writer) error { 81 switch o.docTypeString { 82 case "markdown", "mdown", "md": 83 if o.generateHeaders { 84 standardLinks := func(s string) string { return s } 85 86 hdrFunc := func(filename string) string { 87 base := filepath.Base(filename) 88 name := strings.TrimSuffix(base, path.Ext(base)) 89 title := cases.Title(language.Und, cases.NoLower).String(strings.Replace(name, "_", " ", -1)) 90 return fmt.Sprintf("---\ntitle: \"%s\"\n---\n\n", title) 91 } 92 93 return doc.GenMarkdownTreeCustom(o.topCmd, o.dest, hdrFunc, standardLinks) 94 } 95 return doc.GenMarkdownTree(o.topCmd, o.dest) 96 case "man": 97 manHdr := &doc.GenManHeader{Title: "HELM", Section: "1"} 98 return doc.GenManTree(o.topCmd, manHdr, o.dest) 99 case "bash": 100 return o.topCmd.GenBashCompletionFile(filepath.Join(o.dest, "completions.bash")) 101 default: 102 return errors.Errorf("unknown doc type %q. Try 'markdown' or 'man'", o.docTypeString) 103 } 104 }