2020-10-05 01:00:12 +02:00
---
weight: 24
---
## Yaml Docs
2017-01-30 13:45:31 -08:00
Generating yaml files from a cobra command is incredibly easy. An example is as follows:
```go
package main
import (
2017-04-19 14:39:58 +02:00
"log"
2017-01-30 13:45:31 -08:00
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)
func main() {
cmd := & cobra.Command{
Use: "test",
Short: "my test program",
}
2017-04-19 14:39:58 +02:00
err := doc.GenYamlTree(cmd, "/tmp")
if err != nil {
log.Fatal(err)
}
2017-01-30 13:45:31 -08:00
}
```
That will get you a Yaml document `/tmp/test.yaml`
2020-10-05 01:00:12 +02:00
### The entire command tree
2017-01-30 13:45:31 -08:00
This program can actually generate docs for the kubectl command in the kubernetes project
```go
package main
import (
"io/ioutil"
2017-04-19 14:39:58 +02:00
"log"
2017-01-30 13:45:31 -08:00
"os"
"k8s.io/kubernetes/pkg/kubectl/cmd"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"github.com/spf13/cobra/doc"
)
func main() {
kubectl := cmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, ioutil.Discard, ioutil.Discard)
2017-04-19 14:39:58 +02:00
err := doc.GenYamlTree(kubectl, "./")
if err != nil {
log.Fatal(err)
}
2017-01-30 13:45:31 -08:00
}
```
This will generate a whole series of files, one for each command in the tree, in the directory specified (in this case "./")
2020-10-05 01:00:12 +02:00
### For a single command
2017-01-30 13:45:31 -08:00
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 `GenYaml` instead of `GenYamlTree`
```go
out := new(bytes.Buffer)
doc.GenYaml(cmd, out)
```
This will write the yaml doc for ONLY "cmd" into the out, buffer.
2020-10-05 01:00:12 +02:00
### Customize the output
2017-01-30 13:45:31 -08:00
Both `GenYaml` and `GenYamlTree` have alternate versions with callbacks to get some control of the output:
```go
func GenYamlTreeCustom(cmd *Command, dir string, filePrepender, linkHandler func(string) string) error {
//...
}
```
```go
func GenYamlCustom(cmd *Command, out *bytes.Buffer, linkHandler func(string) string) error {
//...
}
```
2022-05-17 20:28:13 +02:00
The `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 ](https://gohugo.io/ ):
2017-01-30 13:45:31 -08:00
```go
const fmTemplate = `---
date: %s
title: "%s"
slug: %s
url: %s
---
`
filePrepender := func(filename string) string {
now := time.Now().Format(time.RFC3339)
name := filepath.Base(filename)
base := strings.TrimSuffix(name, path.Ext(name))
url := "/commands/" + strings.ToLower(base) + "/"
return fmt.Sprintf(fmTemplate, now, strings.Replace(base, "_", " ", -1), base, url)
}
```
The `linkHandler` can be used to customize the rendered internal links to the commands, given a filename:
```go
linkHandler := func(name string) string {
base := strings.TrimSuffix(name, path.Ext(name))
return "/commands/" + strings.ToLower(base) + "/"
}
```