From c936ea96931abc40731b5ca7ef8ba0a9f70491fa Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 15 Mar 2020 13:13:38 +0100 Subject: [PATCH] Fix yamldocs generator to accomodate nested subcommands The script was written to only take subcommands at the first and second level into account, but failed to find the Markdown files for extended descriptions of subcommands at the third level, such as `docker trust key generate`, and `docker trust key load`: WARN: /go/src/github.com/docker/cli/docs/reference/commandline/key_generate.md does not exist, skipping WARN: /go/src/github.com/docker/cli/docs/reference/commandline/key_load.md does not exist, skipping WARN: /go/src/github.com/docker/cli/docs/reference/commandline/signer_add.md does not exist, skipping WARN: /go/src/github.com/docker/cli/docs/reference/commandline/signer_remove.md does not exist, skipping This patch updates the script to accomodate subcommands that are more deeply nested. While at it, some minor cleaning and linting issues were also addressed. Signed-off-by: Sebastiaan van Stijn (cherry picked from commit e1b362847fc3b34e46cfa9aad7d82c73cf2a1299) Signed-off-by: Sebastiaan van Stijn --- docs/yaml/generate.go | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/docs/yaml/generate.go b/docs/yaml/generate.go index fdc5a2522f..7244f02e69 100644 --- a/docs/yaml/generate.go +++ b/docs/yaml/generate.go @@ -25,6 +25,7 @@ func generateCliYaml(opts *options) error { commands.AddCommands(cmd, dockerCli) disableFlagsInUseLine(cmd) source := filepath.Join(opts.source, descriptionSourcePath) + fmt.Println("Markdown source:", source) if err := loadLongDescription(cmd, source); err != nil { return err } @@ -50,23 +51,29 @@ func visitAll(root *cobra.Command, fn func(*cobra.Command)) { fn(root) } -func loadLongDescription(cmd *cobra.Command, path ...string) error { - for _, cmd := range cmd.Commands() { - if cmd.Name() == "" { - continue - } - fullpath := filepath.Join(path[0], strings.Join(append(path[1:], cmd.Name()), "_")+".md") - +func loadLongDescription(parentCmd *cobra.Command, path string) error { + for _, cmd := range parentCmd.Commands() { if cmd.HasSubCommands() { - loadLongDescription(cmd, path[0], cmd.Name()) + if err := loadLongDescription(cmd, path); err != nil { + return err + } } - - if _, err := os.Stat(fullpath); err != nil { - log.Printf("WARN: %s does not exist, skipping\n", fullpath) + name := cmd.CommandPath() + log.Println("INFO: Generating docs for", name) + if i := strings.Index(name, " "); i >= 0 { + // remove root command / binary name + name = name[i+1:] + } + if name == "" { + continue + } + mdFile := strings.ReplaceAll(name, " ", "_") + ".md" + fullPath := filepath.Join(path, mdFile) + content, err := ioutil.ReadFile(fullPath) + if os.IsNotExist(err) { + log.Printf("WARN: %s does not exist, skipping\n", mdFile) continue } - - content, err := ioutil.ReadFile(fullpath) if err != nil { return err } @@ -95,11 +102,11 @@ func parseArgs() (*options, error) { func main() { opts, err := parseArgs() if err != nil { - fmt.Fprintln(os.Stderr, err.Error()) + log.Println(err) } - fmt.Printf("Project root: %s\n", opts.source) - fmt.Printf("Generating yaml files into %s\n", opts.target) + fmt.Println("Project root: ", opts.source) + fmt.Println("YAML output dir:", opts.target) if err := generateCliYaml(opts); err != nil { - fmt.Fprintf(os.Stderr, "Failed to generate yaml files: %s\n", err.Error()) + log.Println("Failed to generate yaml files:", err) } }