github.com/iter8-tools/iter8@v1.1.2/cmd/docs.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "path" 6 "path/filepath" 7 "strings" 8 9 "github.com/spf13/cobra/doc" 10 "golang.org/x/text/cases" 11 "golang.org/x/text/language" 12 13 "github.com/spf13/cobra" 14 ) 15 16 // docsDesc is the description for the docs command 17 const docsDesc = ` 18 Generate markdown documentation for Iter8 CLI commands. Documentation will be generated for all commands that are not hidden. 19 ` 20 21 // newDocsCmd creates the docs command 22 func newDocsCmd() *cobra.Command { 23 docsDir := "" 24 cmd := &cobra.Command{ 25 Use: "docs", 26 Short: "Generate markdown documentation for Iter8 CLI", 27 Long: docsDesc, 28 Hidden: true, 29 SilenceUsage: true, 30 RunE: func(cmd *cobra.Command, args []string) error { 31 standardLinks := func(s string) string { return s } 32 33 hdrFunc := func(filename string) string { 34 base := filepath.Base(filename) 35 name := strings.TrimSuffix(base, path.Ext(base)) 36 caser := cases.Title(language.English) 37 title := caser.String(strings.Replace(name, "_", " ", -1)) 38 tpl := `--- 39 template: main.html 40 title: "%s" 41 hide: 42 - toc 43 --- 44 ` 45 return fmt.Sprintf(tpl, title) 46 } 47 48 // automatically generate markdown documentation for all Iter8 commands 49 return doc.GenMarkdownTreeCustom(rootCmd, docsDir, hdrFunc, standardLinks) 50 }, 51 } 52 addDocsFlags(cmd, &docsDir) 53 return cmd 54 } 55 56 // addDocsFlags defines the flags for the docs command 57 func addDocsFlags(cmd *cobra.Command, docsDirPtr *string) { 58 cmd.Flags().StringVar(docsDirPtr, "commandDocsDir", "", "directory where Iter8 CLI documentation will be created") 59 _ = cmd.MarkFlagRequired("commandDocsDir") 60 }