doc: Add and edit docs

This commit is contained in:
Albert Nigmatzianov 2017-03-24 10:46:49 +05:00
parent 7be4beda01
commit 424a56a206
6 changed files with 47 additions and 13 deletions

View file

@ -30,8 +30,8 @@ import (
// GenManTree will generate a man page for this command and all descendants // GenManTree will generate a man page for this command and all descendants
// in the directory given. The header may be nil. This function may not work // in the directory given. The header may be nil. This function may not work
// correctly if your command names have - in them. If you have `cmd` with two // correctly if your command names have `-` in them. If you have `cmd` with two
// subcmds, `sub` and `sub-third`. And `sub` has a subcommand called `third` // subcmds, `sub` and `sub-third`, and `sub` has a subcommand called `third`
// it is undefined which help output will be in the file `cmd-sub-third.1`. // it is undefined which help output will be in the file `cmd-sub-third.1`.
func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) error { func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) error {
return GenManTreeFromOpts(cmd, GenManTreeOptions{ return GenManTreeFromOpts(cmd, GenManTreeOptions{

View file

@ -6,6 +6,8 @@ Generating man pages from a cobra command is incredibly easy. An example is as f
package main package main
import ( import (
"log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/cobra/doc" "github.com/spf13/cobra/doc"
) )
@ -19,7 +21,10 @@ func main() {
Title: "MINE", Title: "MINE",
Section: "3", Section: "3",
} }
doc.GenManTree(cmd, header, "/tmp") err := doc.GenManTree(cmd, header, "/tmp")
if err != nil {
log.Fatal(err)
}
} }
``` ```

View file

@ -52,10 +52,12 @@ func printOptions(w io.Writer, cmd *cobra.Command, name string) error {
return nil return nil
} }
// GenMarkdown creates markdown output.
func GenMarkdown(cmd *cobra.Command, w io.Writer) error { func GenMarkdown(cmd *cobra.Command, w io.Writer) error {
return GenMarkdownCustom(cmd, w, func(s string) string { return s }) return GenMarkdownCustom(cmd, w, func(s string) string { return s })
} }
// GenMarkdownCustom creates custom markdown output.
func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error { func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {
name := cmd.CommandPath() name := cmd.CommandPath()
@ -141,6 +143,12 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string)
return nil return nil
} }
// GenMarkdownTree will generate a markdown page for this command and all
// descendants in the directory given. The header may be nil.
// This function may not work correctly if your command names have `-` in them.
// If you have `cmd` with two subcmds, `sub` and `sub-third`,
// and `sub` has a subcommand called `third`, it is undefined which
// help output will be in the file `cmd-sub-third.1`.
func GenMarkdownTree(cmd *cobra.Command, dir string) error { func GenMarkdownTree(cmd *cobra.Command, dir string) error {
identity := func(s string) string { return s } identity := func(s string) string { return s }
emptyStr := func(s string) string { return "" } emptyStr := func(s string) string { return "" }

View file

@ -6,6 +6,8 @@ Generating man pages from a cobra command is incredibly easy. An example is as f
package main package main
import ( import (
"log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/cobra/doc" "github.com/spf13/cobra/doc"
) )
@ -15,7 +17,10 @@ func main() {
Use: "test", Use: "test",
Short: "my test program", Short: "my test program",
} }
doc.GenMarkdownTree(cmd, "/tmp") err := doc.GenMarkdownTree(cmd, "/tmp")
if err != nil {
log.Fatal(err)
}
} }
``` ```
@ -29,6 +34,7 @@ This program can actually generate docs for the kubectl command in the kubernete
package main package main
import ( import (
"log"
"io/ioutil" "io/ioutil"
"os" "os"
@ -40,7 +46,10 @@ import (
func main() { func main() {
kubectl := cmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, ioutil.Discard, ioutil.Discard) kubectl := cmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, ioutil.Discard, ioutil.Discard)
doc.GenMarkdownTree(kubectl, "./") err := doc.GenMarkdownTree(kubectl, "./")
if err != nil {
log.Fatal(err)
}
} }
``` ```
@ -52,7 +61,10 @@ You may wish to have more control over the output, or only generate for a single
```go ```go
out := new(bytes.Buffer) out := new(bytes.Buffer)
doc.GenMarkdown(cmd, out) err := doc.GenMarkdown(cmd, out)
if err != nil {
log.Fatal(err)
}
``` ```
This will write the markdown doc for ONLY "cmd" into the out, buffer. This will write the markdown doc for ONLY "cmd" into the out, buffer.

View file

@ -45,8 +45,8 @@ type cmdDoc struct {
// GenYamlTree creates yaml structured ref files for this command and all descendants // GenYamlTree creates yaml structured ref files for this command and all descendants
// in the directory given. This function may not work // in the directory given. This function may not work
// correctly if your command names have - in them. If you have `cmd` with two // correctly if your command names have `-` in them. If you have `cmd` with two
// subcmds, `sub` and `sub-third`. And `sub` has a subcommand called `third` // subcmds, `sub` and `sub-third`, and `sub` has a subcommand called `third`
// it is undefined which help output will be in the file `cmd-sub-third.1`. // it is undefined which help output will be in the file `cmd-sub-third.1`.
func GenYamlTree(cmd *cobra.Command, dir string) error { func GenYamlTree(cmd *cobra.Command, dir string) error {
identity := func(s string) string { return s } identity := func(s string) string { return s }
@ -54,7 +54,7 @@ func GenYamlTree(cmd *cobra.Command, dir string) error {
return GenYamlTreeCustom(cmd, dir, emptyStr, identity) return GenYamlTreeCustom(cmd, dir, emptyStr, identity)
} }
// GenYamlTreeCustom creates yaml structured ref files // GenYamlTreeCustom creates yaml structured ref files.
func GenYamlTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandler func(string) string) error { func GenYamlTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandler func(string) string) error {
for _, c := range cmd.Commands() { for _, c := range cmd.Commands() {
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() { if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
@ -82,12 +82,12 @@ func GenYamlTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandle
return nil return nil
} }
// GenYaml creates yaml output // GenYaml creates yaml output.
func GenYaml(cmd *cobra.Command, w io.Writer) error { func GenYaml(cmd *cobra.Command, w io.Writer) error {
return GenYamlCustom(cmd, w, func(s string) string { return s }) return GenYamlCustom(cmd, w, func(s string) string { return s })
} }
// GenYamlCustom creates custom yaml output // GenYamlCustom creates custom yaml output.
func GenYamlCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error { func GenYamlCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {
yamlDoc := cmdDoc{} yamlDoc := cmdDoc{}
yamlDoc.Name = cmd.CommandPath() yamlDoc.Name = cmd.CommandPath()

View file

@ -6,6 +6,8 @@ Generating yaml files from a cobra command is incredibly easy. An example is as
package main package main
import ( import (
"log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/cobra/doc" "github.com/spf13/cobra/doc"
) )
@ -15,7 +17,10 @@ func main() {
Use: "test", Use: "test",
Short: "my test program", Short: "my test program",
} }
doc.GenYamlTree(cmd, "/tmp") err := doc.GenYamlTree(cmd, "/tmp")
if err != nil {
log.Fatal(err)
}
} }
``` ```
@ -30,6 +35,7 @@ package main
import ( import (
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"k8s.io/kubernetes/pkg/kubectl/cmd" "k8s.io/kubernetes/pkg/kubectl/cmd"
@ -40,7 +46,10 @@ import (
func main() { func main() {
kubectl := cmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, ioutil.Discard, ioutil.Discard) kubectl := cmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, ioutil.Discard, ioutil.Discard)
doc.GenYamlTree(kubectl, "./") err := doc.GenYamlTree(kubectl, "./")
if err != nil {
log.Fatal(err)
}
} }
``` ```