From 23f22d3ca849e7665b2a94d3f707a0764ef2c499 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 28 Jun 2022 12:07:52 +0200 Subject: [PATCH] use full command as aliases The default output for Cobra aliases only shows the subcommand as alias, which is not very intuitive. This patch changes the output to use the full command as it would be called by the user. Before this patch: aliases: build, b After this patch: aliases: docker buildx build, docker buildx b Note that there's still some improvements to be made; due to how aliases must be set-up in Cobra, aliases at different "levels" are still not shown. So for example, `docker build --help` will not show `docker buildx build` as alias, and vice-versa. This will require additional changes, and can possibly be resolved using custom metadata/annotations. Signed-off-by: Sebastiaan van Stijn --- clidocstool.go | 16 ++++++++++++++++ clidocstool_md.go | 8 +++----- clidocstool_yaml.go | 2 +- fixtures/buildx_build.md | 2 +- fixtures/docker_buildx_build.yaml | 2 +- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/clidocstool.go b/clidocstool.go index 302fc59..7f1b8a0 100644 --- a/clidocstool.go +++ b/clidocstool.go @@ -97,3 +97,19 @@ func copyFile(src string, dst string) error { _, err = io.Copy(df, sf) return err } + +func getAliases(cmd *cobra.Command) []string { + if len(cmd.Aliases) == 0 { + return cmd.Aliases + } + + var parentPath string + if cmd.HasParent() { + parentPath = cmd.Parent().CommandPath() + " " + } + aliases := []string{cmd.CommandPath()} + for _, a := range cmd.Aliases { + aliases = append(aliases, parentPath+a) + } + return aliases +} diff --git a/clidocstool_md.go b/clidocstool_md.go index 55cb38d..24efee4 100644 --- a/clidocstool_md.go +++ b/clidocstool_md.go @@ -155,11 +155,9 @@ func mdCmdOutput(cmd *cobra.Command, old string) (string, error) { fmt.Fprintf(b, "%s\n\n", desc) } - if len(cmd.Aliases) != 0 { - fmt.Fprintf(b, "### Aliases\n\n`%s`", cmd.Name()) - for _, a := range cmd.Aliases { - fmt.Fprintf(b, ", `%s`", a) - } + if aliases := getAliases(cmd); len(aliases) != 0 { + fmt.Fprint(b, "### Aliases\n\n") + fmt.Fprint(b, "`"+strings.Join(aliases, "`, `")+"`") fmt.Fprint(b, "\n\n") } diff --git a/clidocstool_yaml.go b/clidocstool_yaml.go index dfccfca..8175671 100644 --- a/clidocstool_yaml.go +++ b/clidocstool_yaml.go @@ -150,7 +150,7 @@ func (c *Client) genYamlCustom(cmd *cobra.Command, w io.Writer) error { cliDoc := cmdDoc{ Name: cmd.CommandPath(), - Aliases: strings.Join(cmd.Aliases, ", "), + Aliases: strings.Join(getAliases(cmd), ", "), Short: forceMultiLine(cmd.Short, shortMaxWidth), Long: forceMultiLine(cmd.Long, longMaxWidth), Example: cmd.Example, diff --git a/fixtures/buildx_build.md b/fixtures/buildx_build.md index 9560363..7248112 100644 --- a/fixtures/buildx_build.md +++ b/fixtures/buildx_build.md @@ -5,7 +5,7 @@ Start a build ### Aliases -`build`, `b` +`docker buildx build`, `docker buildx b` ### Options diff --git a/fixtures/docker_buildx_build.yaml b/fixtures/docker_buildx_build.yaml index 68cc04a..049c831 100644 --- a/fixtures/docker_buildx_build.yaml +++ b/fixtures/docker_buildx_build.yaml @@ -1,5 +1,5 @@ command: docker buildx build -aliases: b +aliases: docker buildx build, docker buildx b short: Start a build long: Start a build usage: docker buildx build [OPTIONS] PATH | URL | -