Skip to content
Myr

Fix #401 (#402)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* doc: Add and edit docs * doc: Add tests

authored by Albert Nigmatzianov · committed by GitHub · parent b6cb395 · Browse files · 9 files+95 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

9 files+95 added−25 removed

  • other:+95 added−25 removed

9 files+95 added−25 removed

modified:doc/man_docs_test.go+1 added-4 removed
11 unmodified lines
1213 "github.com/spf13/cobra"14)1516var _ = fmt.Println17var _ = os.Stderr1816func translate(in string) string {17 return strings.Replace(in, "-", "\\-", -1)18}19
129 unmodified lines
149 }150 header := &GenManHeader{Section: "2"}151 tmpdir, err := ioutil.TempDir("", "test-gen-man-tree")152 if err != nil {156 t.Fatalf("Failed to create tempdir: %s", err.Error())153 t.Fatalf("Failed to create tmpdir: %s", err.Error())154 }155 defer os.RemoveAll(tmpdir)156157 if err := GenManTree(cmd, header, tmpdir); err != nil {
42 unmodified lines
modified:doc/man_docs.go+2 added-2 removed
28 unmodified lines
29)3031// GenManTree will generate a man page for this command and all descendants32// in the directory given. The header may be nil. This function may not work33// correctly if your command names have - in them. If you have `cmd` with two34// subcmds, `sub` and `sub-third`. And `sub` has a subcommand called `third`33// correctly if your command names have `-` in them. If you have `cmd` with two34// subcmds, `sub` and `sub-third`, and `sub` has a subcommand called `third`35// it is undefined which help output will be in the file `cmd-sub-third.1`.36func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) error {37 return GenManTreeFromOpts(cmd, GenManTreeOptions{38 Header: header,
193 unmodified lines
modified:doc/man_docs.md+6 added-1 removed
modified:doc/md_docs_test.go+23 added-4 removed
1package doc23import (4 "bytes"5 "fmt"5 "io/ioutil"6 "os"7 "path/filepath"8 "strings"9 "testing"1011 "github.com/spf13/cobra"12)1311var _ = fmt.Println12var _ = os.Stderr1314func TestGenMdDoc(t *testing.T) {15 c := initializeWithRootCmd()16 // Need two commands to run the command alphabetical sort17 cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)
67 unmodified lines
85 unexpected := "Auto generated"86 checkStringOmits(t, found, unexpected)8788}8990func TestGenMdTree(t *testing.T) {91 cmd := &cobra.Command{92 Use: "do [OPTIONS] arg1 arg2",93 }94 tmpdir, err := ioutil.TempDir("", "test-gen-md-tree")95 if err != nil {96 t.Fatalf("Failed to create tmpdir: %s", err.Error())97 }98 defer os.RemoveAll(tmpdir)99100 if err := GenMarkdownTree(cmd, tmpdir); err != nil {101 t.Fatalf("GenMarkdownTree failed: %s", err.Error())102 }103104 if _, err := os.Stat(filepath.Join(tmpdir, "do.md")); err != nil {105 t.Fatalf("Expected file 'do.md' to exist")106 }107}
modified:doc/md_docs.go+8 added
50 unmodified lines
51 }52 return nil53}5455// GenMarkdown creates markdown output.56func GenMarkdown(cmd *cobra.Command, w io.Writer) error {57 return GenMarkdownCustom(cmd, w, func(s string) string { return s })58}5960// GenMarkdownCustom creates custom markdown output.61func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {62 name := cmd.CommandPath()6364 short := cmd.Short
77 unmodified lines
142 }143 return nil144}145146// GenMarkdownTree will generate a markdown page for this command and all147// descendants in the directory given. The header may be nil.148// This function may not work correctly if your command names have `-` in them.149// If you have `cmd` with two subcmds, `sub` and `sub-third`,150// and `sub` has a subcommand called `third`, it is undefined which151// help output will be in the file `cmd-sub-third.1`.152func GenMarkdownTree(cmd *cobra.Command, dir string) error {153 identity := func(s string) string { return s }154 emptyStr := func(s string) string { return "" }155 return GenMarkdownTreeCustom(cmd, dir, emptyStr, identity)
28 unmodified lines
modified:doc/md_docs.md+15 added-3 removed
modified:doc/yaml_docs_test.go+24 added-4 removed
1package doc23import (4 "bytes"5 "fmt"5 "io/ioutil"6 "os"7 "path/filepath"8 "strings"9 "testing"1011 "github.com/spf13/cobra"12)1311var _ = fmt.Println12var _ = os.Stderr1314func TestGenYamlDoc(t *testing.T) {15 c := initializeWithRootCmd()16 // Need two commands to run the command alphabetical sort17 cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)
67 unmodified lines
85 unexpected := "Auto generated"86 checkStringOmits(t, found, unexpected)8788}8990func TestGenYamlTree(t *testing.T) {91 cmd := &cobra.Command{92 Use: "do [OPTIONS] arg1 arg2",93 }9495 tmpdir, err := ioutil.TempDir("", "test-gen-yaml-tree")96 if err != nil {97 t.Fatalf("Failed to create tmpdir: %s", err.Error())98 }99 defer os.RemoveAll(tmpdir)100101 if err := GenYamlTree(cmd, tmpdir); err != nil {102 t.Fatalf("GenYamlTree failed: %s", err.Error())103 }104105 if _, err := os.Stat(filepath.Join(tmpdir, "do.yaml")); err != nil {106 t.Fatalf("Expected file 'do.yaml' to exist")107 }108}
modified:doc/yaml_docs.go+5 added-5 removed
43 unmodified lines
44}4546// GenYamlTree creates yaml structured ref files for this command and all descendants47// in the directory given. This function may not work48// correctly if your command names have - in them. If you have `cmd` with two49// subcmds, `sub` and `sub-third`. And `sub` has a subcommand called `third`48// correctly if your command names have `-` in them. If you have `cmd` with two49// subcmds, `sub` and `sub-third`, and `sub` has a subcommand called `third`50// it is undefined which help output will be in the file `cmd-sub-third.1`.51func GenYamlTree(cmd *cobra.Command, dir string) error {52 identity := func(s string) string { return s }53 emptyStr := func(s string) string { return "" }54 return GenYamlTreeCustom(cmd, dir, emptyStr, identity)55}5657// GenYamlTreeCustom creates yaml structured ref files57// GenYamlTreeCustom creates yaml structured ref files.58func GenYamlTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandler func(string) string) error {59 for _, c := range cmd.Commands() {60 if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {61 continue
19 unmodified lines
81 }82 return nil83}8485// GenYaml creates yaml output85// GenYaml creates yaml output.86func GenYaml(cmd *cobra.Command, w io.Writer) error {87 return GenYamlCustom(cmd, w, func(s string) string { return s })88}8990// GenYamlCustom creates custom yaml output90// GenYamlCustom creates custom yaml output.91func GenYamlCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {92 yamlDoc := cmdDoc{}93 yamlDoc.Name = cmd.CommandPath()94
71 unmodified lines
modified:doc/yaml_docs.md+11 added-2 removed
4 unmodified lines
5```go6package main78import (9 "log"1011 "github.com/spf13/cobra"12 "github.com/spf13/cobra/doc"13)14
1 unmodified line
16 cmd := &cobra.Command{17 Use: "test",18 Short: "my test program",19 }18 doc.GenYamlTree(cmd, "/tmp")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`
6 unmodified lines
34package main3536import (37 "io/ioutil"38 "log"39 "os"4041 "k8s.io/kubernetes/pkg/kubectl/cmd"42 cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
2 unmodified lines
45)4647func main() {48 kubectl := cmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, ioutil.Discard, ioutil.Discard)43 doc.GenYamlTree(kubectl, "./")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 "./")
56 unmodified lines