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
Provenance
- Commit
f95d58b- Tree
0034c2c- Author
Albert Nigmatzianov <albertnigma@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
b6cb395
9 files changed+95 added−25 removed
modified:doc/man_docs_test.go+1 added-4 removed
11 unmodified lines12 13 "github.com/spf13/cobra"14 )15 16-var _ = fmt.Println17-var _ = os.Stderr18-16 func translate(in string) string {17 return strings.Replace(in, "-", "\\-", -1)18 }19 129 unmodified lines149 }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)156 157 if err := GenManTree(cmd, header, tmpdir); err != nil {42 unmodified linesmodified:doc/man_docs.go+2 added-2 removed
28 unmodified lines29 )30 31 // 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`.36 func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) error {37 return GenManTreeFromOpts(cmd, GenManTreeOptions{38 Header: header,193 unmodified linesmodified:doc/man_docs.md+6 added-1 removed
4 unmodified lines5678910111213145 unmodified lines202122232224252627282930314 unmodified lines```gopackage main
import ( "log"
"github.com/spf13/cobra" "github.com/spf13/cobra/doc")
5 unmodified lines header := &doc.GenManHeader{ Title: "MINE", Section: "3", } doc.GenManTree(cmd, header, "/tmp") err := doc.GenManTree(cmd, header, "/tmp") if err != nil { log.Fatal(err) }}```
That will get you a man page `/tmp/test.3`modified:doc/md_docs_test.go+23 added-4 removed
1 package doc2 3 import (4 "bytes"5- "fmt"5+ "io/ioutil"6 "os"7+ "path/filepath"8 "strings"9 "testing"10+11+ "github.com/spf13/cobra"12 )13 11-var _ = fmt.Println12-var _ = os.Stderr13-14 func TestGenMdDoc(t *testing.T) {15 c := initializeWithRootCmd()16 // Need two commands to run the command alphabetical sort17 cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)67 unmodified lines85 unexpected := "Auto generated"86 checkStringOmits(t, found, unexpected)87 88 }89+90+func 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)99+100+ if err := GenMarkdownTree(cmd, tmpdir); err != nil {101+ t.Fatalf("GenMarkdownTree failed: %s", err.Error())102+ }103+104+ 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 lines51 }52 return nil53 }54 55+// GenMarkdown creates markdown output.56 func GenMarkdown(cmd *cobra.Command, w io.Writer) error {57 return GenMarkdownCustom(cmd, w, func(s string) string { return s })58 }59 60+// GenMarkdownCustom creates custom markdown output.61 func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {62 name := cmd.CommandPath()63 64 short := cmd.Short77 unmodified lines142 }143 return nil144 }145 146+// 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`.152 func 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 linesmodified:doc/md_docs.md+15 added-3 removed
4 unmodified lines56789101112131415161718191820212223242526275 unmodified lines3334353637383940413 unmodified lines454647484349505152535455563 unmodified lines6061626355646566676869707144 unmodified lines4 unmodified lines```gopackage main
import ( "log"
"github.com/spf13/cobra" "github.com/spf13/cobra/doc")
func main() { cmd := &cobra.Command{ Use: "test", Short: "my test program", } doc.GenMarkdownTree(cmd, "/tmp") err := doc.GenMarkdownTree(cmd, "/tmp") if err != nil { log.Fatal(err) }}```
That will get you a Markdown document `/tmp/test.md`5 unmodified lines```gopackage main
import ( "log" "io/ioutil" "os"
"k8s.io/kubernetes/pkg/kubectl/cmd"3 unmodified lines)
func main() { 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) }}```
This will generate a whole series of files, one for each command in the tree, in the directory specified (in this case "./")3 unmodified linesYou 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 `GenMarkdown` instead of `GenMarkdownTree`
```go 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.
44 unmodified linesmodified:doc/yaml_docs_test.go+24 added-4 removed
1 package doc2 3 import (4 "bytes"5- "fmt"5+ "io/ioutil"6 "os"7+ "path/filepath"8 "strings"9 "testing"10+11+ "github.com/spf13/cobra"12 )13 11-var _ = fmt.Println12-var _ = os.Stderr13-14 func TestGenYamlDoc(t *testing.T) {15 c := initializeWithRootCmd()16 // Need two commands to run the command alphabetical sort17 cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)67 unmodified lines85 unexpected := "Auto generated"86 checkStringOmits(t, found, unexpected)87 88 }89+90+func TestGenYamlTree(t *testing.T) {91+ cmd := &cobra.Command{92+ Use: "do [OPTIONS] arg1 arg2",93+ }94+95+ 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)100+101+ if err := GenYamlTree(cmd, tmpdir); err != nil {102+ t.Fatalf("GenYamlTree failed: %s", err.Error())103+ }104+105+ 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 lines44 }45 46 // 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`.51 func 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 }56 57-// GenYamlTreeCustom creates yaml structured ref files57+// GenYamlTreeCustom creates yaml structured ref files.58 func 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 continue19 unmodified lines81 }82 return nil83 }84 85-// GenYaml creates yaml output85+// GenYaml creates yaml output.86 func GenYaml(cmd *cobra.Command, w io.Writer) error {87 return GenYamlCustom(cmd, w, func(s string) string { return s })88 }89 90-// GenYamlCustom creates custom yaml output90+// GenYamlCustom creates custom yaml output.91 func GenYamlCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {92 yamlDoc := cmdDoc{}93 yamlDoc.Name = cmd.CommandPath()94 71 unmodified linesmodified:doc/yaml_docs.md+11 added-2 removed
4 unmodified lines5 ```go6 package main7 8 import (9+ "log"10+11 "github.com/spf13/cobra"12 "github.com/spf13/cobra/doc"13 )14 1 unmodified line16 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 ```26 27 That will get you a Yaml document `/tmp/test.yaml`6 unmodified lines34 package main35 36 import (37 "io/ioutil"38+ "log"39 "os"40 41 "k8s.io/kubernetes/pkg/kubectl/cmd"42 cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"2 unmodified lines45 )46 47 func 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 ```55 56 This will generate a whole series of files, one for each command in the tree, in the directory specified (in this case "./")56 unmodified lines