Skip to content
Myr

md.md

This file

Path

site/content/docgen/md.md

115 lines · 2.7 KiB · Markdown

Last changed by

Replace deprecated ioutil usage (#2181)Nir Sofferbefore adoption511af59

History of this file

Take it

Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a

lines 1–37why, one step back
1# Generating Markdown Docs For Your Own cobra.Command
23Generating Markdown pages from a cobra command is incredibly easy. An example is as follows:45```go6package main78import (9 "log"1011 "github.com/spf13/cobra"12 "github.com/spf13/cobra/doc"13)1415func main() {16 cmd := &cobra.Command{17 Use: "test",18 Short: "my test program",19 }20 err := doc.GenMarkdownTree(cmd, "/tmp")21 if err != nil {22 log.Fatal(err)23 }24}25```2627That will get you a Markdown document `/tmp/test.md`2829## Generate markdown docs for the entire command tree3031This program can actually generate docs for the kubectl command in the kubernetes project3233```go34package main3536import (37 "log"

Replace deprecated ioutil usage (#2181)Nir Sofferbefore adoption511af59

line 38why, one step back
38 "io"

Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a

lines 39–47why, one step back
39 "os"
4041 "k8s.io/kubernetes/pkg/kubectl/cmd"42 cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"4344 "github.com/spf13/cobra/doc"45)4647func main() {

Replace deprecated ioutil usage (#2181)Nir Sofferbefore adoption511af59

line 48why, one step back
48 kubectl := cmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, io.Discard, io.Discard)

Move documentation sources to site/content (#1428)Unai Martinez-Corralbefore adoptiondcb405a

lines 49–115why, one step back
49 err := doc.GenMarkdownTree(kubectl, "./")
50 if err != nil {51 log.Fatal(err)52 }53}54```5556This will generate a whole series of files, one for each command in the tree, in the directory specified (in this case "./")5758## Generate markdown docs for a single command5960You may wish to have more control over the output, or only generate for a single command, instead of the entire command tree. If this is the case you may prefer to `GenMarkdown` instead of `GenMarkdownTree`6162```go63 out := new(bytes.Buffer)64 err := doc.GenMarkdown(cmd, out)65 if err != nil {66 log.Fatal(err)67 }68```6970This will write the markdown doc for ONLY "cmd" into the out, buffer.7172## Customize the output7374Both `GenMarkdown` and `GenMarkdownTree` have alternate versions with callbacks to get some control of the output:7576```go77func GenMarkdownTreeCustom(cmd *Command, dir string, filePrepender, linkHandler func(string) string) error {78 //...79}80```8182```go83func GenMarkdownCustom(cmd *Command, out *bytes.Buffer, linkHandler func(string) string) error {84 //...85}86```8788The `filePrepender` will prepend the return value given the full filepath to the rendered Markdown file. A common use case is to add front matter to use the generated documentation with [Hugo](https://gohugo.io/):8990```go91const fmTemplate = `---92date: %s93title: "%s"94slug: %s95url: %s96---97`9899filePrepender := func(filename string) string {100 now := time.Now().Format(time.RFC3339)101 name := filepath.Base(filename)102 base := strings.TrimSuffix(name, path.Ext(name))103 url := "/commands/" + strings.ToLower(base) + "/"104 return fmt.Sprintf(fmTemplate, now, strings.Replace(base, "_", " ", -1), base, url)105}106```107108The `linkHandler` can be used to customize the rendered internal links to the commands, given a filename:109110```go111linkHandler := func(name string) string {112 base := strings.TrimSuffix(name, path.Ext(name))113 return "/commands/" + strings.ToLower(base) + "/"114}115```