Skip to content
Myr

yaml_docs.md

This file

Path

doc/yaml_docs.md

112 lines · 2.6 KiB · Markdown

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 0ea93dd point at it.

Fixed linter issuesJuan Lenibefore adoption0ea93dd

lines 1–112why, one step back
1# Generating Yaml Docs For Your Own cobra.Command
23Generating yaml files 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.GenYamlTree(cmd, "/tmp")21 if err != nil {22 log.Fatal(err)23 }24}25```2627That will get you a Yaml document `/tmp/test.yaml`2829## Generate yaml docs for the entire command tree3031This program can actually generate docs for the kubectl command in the kubernetes project3233```go34package main3536import (37 "io/ioutil"38 "log"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() {48 kubectl := cmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, ioutil.Discard, ioutil.Discard)49 err := doc.GenYamlTree(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 yaml 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 `GenYaml` instead of `GenYamlTree`6162```go63 out := new(bytes.Buffer)64 doc.GenYaml(cmd, out)65```6667This will write the yaml doc for ONLY "cmd" into the out, buffer.6869## Customize the output7071Both `GenYaml` and `GenYamlTree` have alternate versions with callbacks to get some control of the output:7273```go74func GenYamlTreeCustom(cmd *Command, dir string, filePrepender, linkHandler func(string) string) error {75 //...76}77```7879```go80func GenYamlCustom(cmd *Command, out *bytes.Buffer, linkHandler func(string) string) error {81 //...82}83```8485The `filePrepender` will prepend the return value given the full filepath to the rendered Yaml file. A common use case is to add front matter to use the generated documentation with [Hugo](http://gohugo.io/):8687```go88const fmTemplate = `---89date: %s90title: "%s"91slug: %s92url: %s93---94`9596filePrepender := func(filename string) string {97 now := time.Now().Format(time.RFC3339)98 name := filepath.Base(filename)99 base := strings.TrimSuffix(name, path.Ext(name))100 url := "/commands/" + strings.ToLower(base) + "/"101 return fmt.Sprintf(fmTemplate, now, strings.Replace(base, "_", " ", -1), base, url)102}103```104105The `linkHandler` can be used to customize the rendered internal links to the commands, given a filename:106107```go108linkHandler := func(name string) string {109 base := strings.TrimSuffix(name, path.Ext(name))110 return "/commands/" + strings.ToLower(base) + "/"111}112```