forked from mirror/cobra
parent
b6cb395893
commit
f95d58bdf3
|
@ -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{
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -13,9 +13,6 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = fmt.Println
|
|
||||||
var _ = os.Stderr
|
|
||||||
|
|
||||||
func translate(in string) string {
|
func translate(in string) string {
|
||||||
return strings.Replace(in, "-", "\\-", -1)
|
return strings.Replace(in, "-", "\\-", -1)
|
||||||
}
|
}
|
||||||
|
@ -153,7 +150,7 @@ func TestGenManTree(t *testing.T) {
|
||||||
header := &GenManHeader{Section: "2"}
|
header := &GenManHeader{Section: "2"}
|
||||||
tmpdir, err := ioutil.TempDir("", "test-gen-man-tree")
|
tmpdir, err := ioutil.TempDir("", "test-gen-man-tree")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to create tempdir: %s", err.Error())
|
t.Fatalf("Failed to create tmpdir: %s", err.Error())
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(tmpdir)
|
defer os.RemoveAll(tmpdir)
|
||||||
|
|
||||||
|
|
|
@ -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 "" }
|
||||||
|
|
|
@ -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.
|
||||||
|
|
|
@ -2,14 +2,14 @@ package doc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
|
||||||
|
|
||||||
var _ = fmt.Println
|
"github.com/spf13/cobra"
|
||||||
var _ = os.Stderr
|
)
|
||||||
|
|
||||||
func TestGenMdDoc(t *testing.T) {
|
func TestGenMdDoc(t *testing.T) {
|
||||||
c := initializeWithRootCmd()
|
c := initializeWithRootCmd()
|
||||||
|
@ -86,3 +86,22 @@ func TestGenMdNoTag(t *testing.T) {
|
||||||
checkStringOmits(t, found, unexpected)
|
checkStringOmits(t, found, unexpected)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGenMdTree(t *testing.T) {
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "do [OPTIONS] arg1 arg2",
|
||||||
|
}
|
||||||
|
tmpdir, err := ioutil.TempDir("", "test-gen-md-tree")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to create tmpdir: %s", err.Error())
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(tmpdir)
|
||||||
|
|
||||||
|
if err := GenMarkdownTree(cmd, tmpdir); err != nil {
|
||||||
|
t.Fatalf("GenMarkdownTree failed: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(filepath.Join(tmpdir, "do.md")); err != nil {
|
||||||
|
t.Fatalf("Expected file 'do.md' to exist")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -2,14 +2,14 @@ package doc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
|
||||||
|
|
||||||
var _ = fmt.Println
|
"github.com/spf13/cobra"
|
||||||
var _ = os.Stderr
|
)
|
||||||
|
|
||||||
func TestGenYamlDoc(t *testing.T) {
|
func TestGenYamlDoc(t *testing.T) {
|
||||||
c := initializeWithRootCmd()
|
c := initializeWithRootCmd()
|
||||||
|
@ -86,3 +86,23 @@ func TestGenYamlNoTag(t *testing.T) {
|
||||||
checkStringOmits(t, found, unexpected)
|
checkStringOmits(t, found, unexpected)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGenYamlTree(t *testing.T) {
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "do [OPTIONS] arg1 arg2",
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpdir, err := ioutil.TempDir("", "test-gen-yaml-tree")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to create tmpdir: %s", err.Error())
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(tmpdir)
|
||||||
|
|
||||||
|
if err := GenYamlTree(cmd, tmpdir); err != nil {
|
||||||
|
t.Fatalf("GenYamlTree failed: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(filepath.Join(tmpdir, "do.yaml")); err != nil {
|
||||||
|
t.Fatalf("Expected file 'do.yaml' to exist")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue