1
0
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:
CrazyMax
2021-07-31 00:08:52 +02:00
committed by Sebastiaan van Stijn
parent 885c73a35e
commit 8a505799c6
4 changed files with 29 additions and 19 deletions

View File

@@ -1 +1,16 @@
package docgen 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
}

View File

@@ -15,9 +15,11 @@ import (
"github.com/spf13/pflag" "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() { for _, c := range cmd.Commands() {
if err := GenMarkdown(c, dir); err != nil { if err := GenMarkdownTree(c, dir); err != nil {
return err return err
} }
} }
@@ -25,11 +27,11 @@ func GenMarkdown(cmd *cobra.Command, dir string) error {
return nil return nil
} }
log.Printf("INFO: Generating Markdown docs for %s", cmd.CommandPath())
mdFile := mdFilename(cmd) mdFile := mdFilename(cmd)
fullPath := filepath.Join(dir, mdFile) fullPath := filepath.Join(dir, mdFile)
if _, err := os.Stat(fullPath); os.IsNotExist(err) { if _, err := os.Stat(fullPath); os.IsNotExist(err) {
log.Printf("INFO: Init markdown for %s", cmd.CommandPath())
var icBuf bytes.Buffer var icBuf bytes.Buffer
icTpl, err := template.New("ic").Option("missingkey=error").Parse(`# {{ .Command }} 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) return errors.Wrapf(err, "failed to write %s", fullPath)
} }
log.Printf("INFO: Markdown updated for %s", cmd.CommandPath())
return nil return nil
} }

View File

@@ -62,6 +62,9 @@ type cmdDoc struct {
// 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 {
emptyStr := func(s string) string { return "" } emptyStr := func(s string) string { return "" }
if err := loadLongDescription(cmd, dir); err != nil {
return err
}
return GenYamlTreeCustom(cmd, dir, emptyStr) return GenYamlTreeCustom(cmd, dir, emptyStr)
} }
@@ -320,16 +323,16 @@ func hasSeeAlso(cmd *cobra.Command) bool {
return false return false
} }
// LoadLongDescription gets long descriptions and examples from markdown. // loadLongDescription gets long descriptions and examples from markdown.
func LoadLongDescription(parentCmd *cobra.Command, path string) error { func loadLongDescription(parentCmd *cobra.Command, path string) error {
for _, cmd := range parentCmd.Commands() { for _, cmd := range parentCmd.Commands() {
if cmd.HasSubCommands() { if cmd.HasSubCommands() {
if err := LoadLongDescription(cmd, path); err != nil { if err := loadLongDescription(cmd, path); err != nil {
return err return err
} }
} }
name := cmd.CommandPath() 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 { if i := strings.Index(name, " "); i >= 0 {
// remove root command / binary name // remove root command / binary name
name = name[i+1:] name = name[i+1:]

View File

@@ -33,19 +33,10 @@ func main() {
cwd, _ := os.Getwd() cwd, _ := os.Getwd()
source := filepath.Join(cwd, sourcePath) 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) log.Printf("ERROR: %+v", err)
} }
if err = docgen.GenTree(cmd, source); err != nil {
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 {
log.Printf("ERROR: %+v", err) log.Printf("ERROR: %+v", err)
} }
} }