Generating ReStructured Text Docs For Your Own cobra.Command

Generating ReST pages from a cobra command is incredibly easy. An example is as follows:

  1. package main
  2. import (
  3. "log"
  4. "github.com/spf13/cobra"
  5. "github.com/spf13/cobra/doc"
  6. )
  7. func main() {
  8. cmd := &cobra.Command{
  9. Use: "test",
  10. Short: "my test program",
  11. }
  12. err := doc.GenReSTTree(cmd, "/tmp")
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. }

That will get you a ReST document /tmp/test.rst

Generate ReST docs for the entire command tree

This program can actually generate docs for the kubectl command in the kubernetes project

  1. package main
  2. import (
  3. "log"
  4. "io/ioutil"
  5. "os"
  6. "k8s.io/kubernetes/pkg/kubectl/cmd"
  7. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  8. "github.com/spf13/cobra/doc"
  9. )
  10. func main() {
  11. kubectl := cmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, ioutil.Discard, ioutil.Discard)
  12. err := doc.GenReSTTree(kubectl, "./")
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. }

This will generate a whole series of files, one for each command in the tree, in the directory specified (in this case “./“)

Generate ReST docs for a single command

You 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 GenReST instead of GenReSTTree

  1. out := new(bytes.Buffer)
  2. err := doc.GenReST(cmd, out)
  3. if err != nil {
  4. log.Fatal(err)
  5. }

This will write the ReST doc for ONLY “cmd” into the out, buffer.

Customize the output

Both GenReST and GenReSTTree have alternate versions with callbacks to get some control of the output:

  1. func GenReSTTreeCustom(cmd *Command, dir string, filePrepender func(string) string, linkHandler func(string, string) string) error {
  2. //...
  3. }
  1. func GenReSTCustom(cmd *Command, out *bytes.Buffer, linkHandler func(string, string) string) error {
  2. //...
  3. }

The filePrepender will prepend the return value given the full filepath to the rendered ReST file. A common use case is to add front matter to use the generated documentation with Hugo:

  1. const fmTemplate = `---
  2. date: %s
  3. title: "%s"
  4. slug: %s
  5. url: %s
  6. ---
  7. `
  8. filePrepender := func(filename string) string {
  9. now := time.Now().Format(time.RFC3339)
  10. name := filepath.Base(filename)
  11. base := strings.TrimSuffix(name, path.Ext(name))
  12. url := "/commands/" + strings.ToLower(base) + "/"
  13. return fmt.Sprintf(fmTemplate, now, strings.Replace(base, "_", " ", -1), base, url)
  14. }

The linkHandler can be used to customize the rendered links to the commands, given a command name and reference. This is useful while converting rst to html or while generating documentation with tools like Sphinx where :ref: is used:

  1. // Sphinx cross-referencing format
  2. linkHandler := func(name, ref string) string {
  3. return fmt.Sprintf(":ref:`%s <%s>`", name, ref)
  4. }