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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-06-28 12:07:52 +02:00
Родитель b4dab44b58
Коммит 23f22d3ca8
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 76698F39D527CE8C
5 изменённых файлов: 22 добавлений и 8 удалений

Просмотреть файл

@ -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
}

Просмотреть файл

@ -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")
}

Просмотреть файл

@ -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,

Просмотреть файл

@ -5,7 +5,7 @@ Start a build
### Aliases
`build`, `b`
`docker buildx build`, `docker buildx b`
### Options

Просмотреть файл

@ -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 | -