Skip to content

Conversation

@thaJeztah
Copy link
Member

@thaJeztah thaJeztah commented Jun 28, 2022

Cobra allows for aliases to be defined for a command, but only allows these
to be defined at the same level (for example, docker image ls as alias for
docker image list). Our CLI has some commands that are available both as a
top-level shorthand as well as docker <object> <verb> subcommands. For example,
docker ps is a shorthand for docker container ps / docker container ls.

This patch introduces a custom "aliases" annotation that can be used to print
all available aliases for a command. While this requires these aliases to be
defined manually, in practice the list of aliases rarely changes, so maintenance
should be minimal.

As a convention, we could consider the first command in this list to be the
canonical command, so that we can use this information to add redirects in
our documentation in future.

Before this patch:

docker images --help

Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Options:
  -a, --all             Show all images (default hides intermediate images)
  ...

With this patch:

docker images --help

Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Aliases:
  docker image ls, docker image list, docker images

Options:
  -a, --all             Show all images (default hides intermediate images)
  ...

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

@codecov-commenter
Copy link

codecov-commenter commented Jun 28, 2022

Codecov Report

❗ No coverage uploaded for pull request base (master@05d288e). Click here to learn what that means.
The diff coverage is 83.33%.

❗ Current head 5097367 differs from pull request most recent head 80b1285. Consider uploading reports for the commit 80b1285 to get more accurate results

@@            Coverage Diff            @@
##             master    #3694   +/-   ##
=========================================
  Coverage          ?   59.02%           
=========================================
  Files             ?      289           
  Lines             ?    24651           
  Branches          ?        0           
=========================================
  Hits              ?    14551           
  Misses            ?     9226           
  Partials          ?      874           

// formatted as the full command as they're called (contrary to the default
// Aliases function, which only returns the subcommand).
func commandAliases(cmd *cobra.Command) string {
if cmd.Annotations["aliases"] != "" {
Copy link
Member

@crazy-max crazy-max Jun 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we expose the "aliases" annotation name in cobra pkg to be used in cli plugins projects? Pretty much like https://github.com/docker/cli-docs-tool/blob/main/annotation/annotation.go?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm.. yes, so was a bit on the fence where to define it, as it's not "just" docs of course (we're also using it for the cli usage output).

Perhaps we should define a list of consts for annotations though. Was also thinking "should it have a prefix", but we already have used (e.g.) version for a long time, so I think that ship has sailed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes indeed there is the cli usage output

Cobra allows for aliases to be defined for a command, but only allows these
to be defined at the same level (for example, `docker image ls` as alias for
`docker image list`). Our CLI has some commands that are available both as a
top-level shorthand as well as `docker <object> <verb>` subcommands. For example,
`docker ps` is a shorthand for `docker container ps` / `docker container ls`.

This patch introduces a custom "aliases" annotation that can be used to print
all available aliases for a command. While this requires these aliases to be
defined manually, in practice the list of aliases rarely changes, so maintenance
should be minimal.

As a convention, we could consider the first command in this list to be the
canonical command, so that we can use this information to add redirects in
our documentation in future.

Before this patch:

    docker images --help

    Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]

    List images

    Options:
      -a, --all             Show all images (default hides intermediate images)
      ...

With this patch:

    docker images --help

    Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]

    List images

    Aliases:
      docker image ls, docker image list, docker images

    Options:
      -a, --all             Show all images (default hides intermediate images)
      ...

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah thaJeztah force-pushed the reference_canonical_aliases branch from 5097367 to 80b1285 Compare June 28, 2022 15:32
@thaJeztah thaJeztah marked this pull request as ready for review June 28, 2022 15:32
@thaJeztah thaJeztah added this to the 22.06.0 milestone Jun 30, 2022
Copy link
Contributor

@tianon tianon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀

@thaJeztah
Copy link
Member Author

thx! Let me bring this one in

@thaJeztah thaJeztah merged commit 4460252 into docker:master Jun 30, 2022
@thaJeztah thaJeztah deleted the reference_canonical_aliases branch June 30, 2022 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants