Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit ee8847c

Browse files
committed
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 e1b362847fc3b34e46cfa9aad7d82c73cf2a1299) Signed-off-by: Sebastiaan van Stijn <github@gone.nl> Upstream-commit: c936ea96931abc40731b5ca7ef8ba0a9f70491fa Component: cli
1 parent 240eda9 commit ee8847c

1 file changed

Lines changed: 24 additions & 17 deletions

File tree

components/cli/docs/yaml/generate.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func generateCliYaml(opts *options) error {
2525
commands.AddCommands(cmd, dockerCli)
2626
disableFlagsInUseLine(cmd)
2727
source := filepath.Join(opts.source, descriptionSourcePath)
28+
fmt.Println("Markdown source:", source)
2829
if err := loadLongDescription(cmd, source); err != nil {
2930
return err
3031
}
@@ -50,23 +51,29 @@ func visitAll(root *cobra.Command, fn func(*cobra.Command)) {
5051
fn(root)
5152
}
5253

53-
func loadLongDescription(cmd *cobra.Command, path ...string) error {
54-
for _, cmd := range cmd.Commands() {
55-
if cmd.Name() == "" {
56-
continue
57-
}
58-
fullpath := filepath.Join(path[0], strings.Join(append(path[1:], cmd.Name()), "_")+".md")
59-
54+
func loadLongDescription(parentCmd *cobra.Command, path string) error {
55+
for _, cmd := range parentCmd.Commands() {
6056
if cmd.HasSubCommands() {
61-
loadLongDescription(cmd, path[0], cmd.Name())
57+
if err := loadLongDescription(cmd, path); err != nil {
58+
return err
59+
}
6260
}
63-
64-
if _, err := os.Stat(fullpath); err != nil {
65-
log.Printf("WARN: %s does not exist, skipping\n", fullpath)
61+
name := cmd.CommandPath()
62+
log.Println("INFO: Generating docs for", name)
63+
if i := strings.Index(name, " "); i >= 0 {
64+
// remove root command / binary name
65+
name = name[i+1:]
66+
}
67+
if name == "" {
68+
continue
69+
}
70+
mdFile := strings.ReplaceAll(name, " ", "_") + ".md"
71+
fullPath := filepath.Join(path, mdFile)
72+
content, err := ioutil.ReadFile(fullPath)
73+
if os.IsNotExist(err) {
74+
log.Printf("WARN: %s does not exist, skipping\n", mdFile)
6675
continue
6776
}
68-
69-
content, err := ioutil.ReadFile(fullpath)
7077
if err != nil {
7178
return err
7279
}
@@ -95,11 +102,11 @@ func parseArgs() (*options, error) {
95102
func main() {
96103
opts, err := parseArgs()
97104
if err != nil {
98-
fmt.Fprintln(os.Stderr, err.Error())
105+
log.Println(err)
99106
}
100-
fmt.Printf("Project root: %s\n", opts.source)
101-
fmt.Printf("Generating yaml files into %s\n", opts.target)
107+
fmt.Println("Project root: ", opts.source)
108+
fmt.Println("YAML output dir:", opts.target)
102109
if err := generateCliYaml(opts); err != nil {
103-
fmt.Fprintf(os.Stderr, "Failed to generate yaml files: %s\n", err.Error())
110+
log.Println("Failed to generate yaml files:", err)
104111
}
105112
}

0 commit comments

Comments
 (0)