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 <github@gone.nl>
(cherry picked from commit e1b362847f)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2020-03-15 13:13:38 +01:00
parent d484456c29
commit c936ea9693
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 24 additions and 17 deletions

View File

@ -25,6 +25,7 @@ func generateCliYaml(opts *options) error {
commands.AddCommands(cmd, dockerCli) commands.AddCommands(cmd, dockerCli)
disableFlagsInUseLine(cmd) disableFlagsInUseLine(cmd)
source := filepath.Join(opts.source, descriptionSourcePath) source := filepath.Join(opts.source, descriptionSourcePath)
fmt.Println("Markdown source:", source)
if err := loadLongDescription(cmd, source); err != nil { if err := loadLongDescription(cmd, source); err != nil {
return err return err
} }
@ -50,23 +51,29 @@ func visitAll(root *cobra.Command, fn func(*cobra.Command)) {
fn(root) fn(root)
} }
func loadLongDescription(cmd *cobra.Command, path ...string) error { func loadLongDescription(parentCmd *cobra.Command, path string) error {
for _, cmd := range cmd.Commands() { for _, cmd := range parentCmd.Commands() {
if cmd.Name() == "" {
continue
}
fullpath := filepath.Join(path[0], strings.Join(append(path[1:], cmd.Name()), "_")+".md")
if cmd.HasSubCommands() { if cmd.HasSubCommands() {
loadLongDescription(cmd, path[0], cmd.Name()) if err := loadLongDescription(cmd, path); err != nil {
return err
}
} }
name := cmd.CommandPath()
if _, err := os.Stat(fullpath); err != nil { log.Println("INFO: Generating docs for", name)
log.Printf("WARN: %s does not exist, skipping\n", fullpath) 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 continue
} }
content, err := ioutil.ReadFile(fullpath)
if err != nil { if err != nil {
return err return err
} }
@ -95,11 +102,11 @@ func parseArgs() (*options, error) {
func main() { func main() {
opts, err := parseArgs() opts, err := parseArgs()
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err.Error()) log.Println(err)
} }
fmt.Printf("Project root: %s\n", opts.source) fmt.Println("Project root: ", opts.source)
fmt.Printf("Generating yaml files into %s\n", opts.target) fmt.Println("YAML output dir:", opts.target)
if err := generateCliYaml(opts); err != nil { 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)
} }
} }