You've already forked cli-docs-tool
mirror of
https://github.com/docker/cli-docs-tool.git
synced 2025-08-08 10:22:04 +03:00
Add GenTree
func
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
committed by
Sebastiaan van Stijn
parent
885c73a35e
commit
8a505799c6
15
docgen.go
15
docgen.go
@@ -1 +1,16 @@
|
||||
package docgen
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func GenTree(cmd *cobra.Command, dir string) error {
|
||||
var err error
|
||||
if err = GenMarkdownTree(cmd, dir); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = GenYamlTree(cmd, dir); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@@ -15,9 +15,11 @@ import (
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
func GenMarkdown(cmd *cobra.Command, dir string) error {
|
||||
// GenMarkdownTree will generate a markdown page for this command and all
|
||||
// descendants in the directory given.
|
||||
func GenMarkdownTree(cmd *cobra.Command, dir string) error {
|
||||
for _, c := range cmd.Commands() {
|
||||
if err := GenMarkdown(c, dir); err != nil {
|
||||
if err := GenMarkdownTree(c, dir); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -25,11 +27,11 @@ func GenMarkdown(cmd *cobra.Command, dir string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Printf("INFO: Generating Markdown docs for %s", cmd.CommandPath())
|
||||
mdFile := mdFilename(cmd)
|
||||
fullPath := filepath.Join(dir, mdFile)
|
||||
|
||||
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
|
||||
log.Printf("INFO: Init markdown for %s", cmd.CommandPath())
|
||||
var icBuf bytes.Buffer
|
||||
icTpl, err := template.New("ic").Option("missingkey=error").Parse(`# {{ .Command }}
|
||||
|
||||
@@ -83,7 +85,6 @@ func GenMarkdown(cmd *cobra.Command, dir string) error {
|
||||
return errors.Wrapf(err, "failed to write %s", fullPath)
|
||||
}
|
||||
|
||||
log.Printf("INFO: Markdown updated for %s", cmd.CommandPath())
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@@ -62,6 +62,9 @@ type cmdDoc struct {
|
||||
// it is undefined which help output will be in the file `cmd-sub-third.1`.
|
||||
func GenYamlTree(cmd *cobra.Command, dir string) error {
|
||||
emptyStr := func(s string) string { return "" }
|
||||
if err := loadLongDescription(cmd, dir); err != nil {
|
||||
return err
|
||||
}
|
||||
return GenYamlTreeCustom(cmd, dir, emptyStr)
|
||||
}
|
||||
|
||||
@@ -320,16 +323,16 @@ func hasSeeAlso(cmd *cobra.Command) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// LoadLongDescription gets long descriptions and examples from markdown.
|
||||
func LoadLongDescription(parentCmd *cobra.Command, path string) error {
|
||||
// loadLongDescription gets long descriptions and examples from markdown.
|
||||
func loadLongDescription(parentCmd *cobra.Command, path string) error {
|
||||
for _, cmd := range parentCmd.Commands() {
|
||||
if cmd.HasSubCommands() {
|
||||
if err := LoadLongDescription(cmd, path); err != nil {
|
||||
if err := loadLongDescription(cmd, path); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
name := cmd.CommandPath()
|
||||
log.Println("INFO: Generating docs for", name)
|
||||
log.Println("INFO: Generating YAML docs for", name)
|
||||
if i := strings.Index(name, " "); i >= 0 {
|
||||
// remove root command / binary name
|
||||
name = name[i+1:]
|
||||
|
@@ -33,19 +33,10 @@ func main() {
|
||||
cwd, _ := os.Getwd()
|
||||
source := filepath.Join(cwd, sourcePath)
|
||||
|
||||
if err = os.MkdirAll(sourcePath, 0755); err != nil {
|
||||
if err = os.MkdirAll(source, 0755); err != nil {
|
||||
log.Printf("ERROR: %+v", err)
|
||||
}
|
||||
|
||||
if err = docgen.GenMarkdown(cmd, sourcePath); err != nil {
|
||||
log.Printf("ERROR: %+v", err)
|
||||
}
|
||||
|
||||
if err := docgen.LoadLongDescription(cmd, source); err != nil {
|
||||
log.Printf("ERROR: %+v", err)
|
||||
}
|
||||
|
||||
if err = docgen.GenYamlTree(cmd, sourcePath); err != nil {
|
||||
if err = docgen.GenTree(cmd, source); err != nil {
|
||||
log.Printf("ERROR: %+v", err)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user