Skip to content

Conversation

@thaJeztah
Copy link
Member

This required a minor hack to accommodate Cobra's logic (and a TODO was added to see if we can improve that logic in upstream). Some changes also had to be made to our "Usage" template, as our custom template did not take into account subcommands and long descriptions for commands. We should review these templates, as some additional features were added in upstream Cobra that we currently may not be using.

With this patch:

instructions for the (hidden) completion subcommand:

docker completion --help

Usage:  docker completion COMMAND

Generate the autocompletion script for docker for the specified shell.
See each sub-command's help for details on how to use the generated script.

Commands:
  bash        Generate the autocompletion script for bash
  fish        Generate the autocompletion script for fish
  powershell  Generate the autocompletion script for powershell
  zsh         Generate the autocompletion script for zsh

Run 'docker completion COMMAND --help' for more information on a command.

and instructions for installing:

dockerd completion bash --help

Usage:	dockerd completion bash

Generate the autocompletion script for the bash shell.

This script depends on the 'bash-completion' package.
If it is not installed already, you can install it via your OS's package manager.

To load completions in your current shell session:

    source <(dockerd completion bash)

To load completions for every new session, execute once:

#### Linux:

    dockerd completion bash > /etc/bash_completion.d/dockerd

#### macOS:

    dockerd completion bash > $(brew --prefix)/etc/bash_completion.d/dockerd

You will need to start a new shell for this setup to take effect.

Options:
      --help              Print usage
      --no-descriptions   disable completion descriptions

- What I did

- How I did it

- How to verify it

- Description for the changelog

dockerd:  add (hidden) `completion` subcommand to generate shell-completion scripts

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

This required a minor hack to accommodate Cobra's logic (and a TODO was
added to see if we can improve that logic in upstream). Some changes also
had to be made to our "Usage" template, as our custom template did not
take into account subcommands and long descriptions for commands. We
should review these templates, as some additional features were added
in upstream Cobra that we currently may not be using.

With this patch:

instructions for the (hidden) completion subcommand:

    docker completion --help

    Usage:  docker completion COMMAND

    Generate the autocompletion script for docker for the specified shell.
    See each sub-command's help for details on how to use the generated script.

    Commands:
      bash        Generate the autocompletion script for bash
      fish        Generate the autocompletion script for fish
      powershell  Generate the autocompletion script for powershell
      zsh         Generate the autocompletion script for zsh

    Run 'docker completion COMMAND --help' for more information on a command.

and instructions for installing:

    dockerd completion bash --help

    Usage:	dockerd completion bash

    Generate the autocompletion script for the bash shell.

    This script depends on the 'bash-completion' package.
    If it is not installed already, you can install it via your OS's package manager.

    To load completions in your current shell session:

        source <(dockerd completion bash)

    To load completions for every new session, execute once:

    #### Linux:

        dockerd completion bash > /etc/bash_completion.d/dockerd

    #### macOS:

        dockerd completion bash > $(brew --prefix)/etc/bash_completion.d/dockerd

    You will need to start a new shell for this setup to take effect.

    Options:
          --help              Print usage
          --no-descriptions   disable completion descriptions

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah thaJeztah added area/cli Client status/2-code-review kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. impact/changelog impact/cli docs/revisit labels Jan 26, 2025
@thaJeztah thaJeztah added this to the 28.0.0 milestone Jan 26, 2025
@thaJeztah thaJeztah self-assigned this Jan 26, 2025
@thaJeztah
Copy link
Member Author

With this PR:

source <(dockerd completion bash)

dockerd --default-<TAB>
--default-address-pool   (Default address pools for node specific local networks)
--default-cgroupns-mode  (Default mode for containers cgroup namespace ("host" | "private"))
--default-gateway        (Container default gateway IPv4 address)
--default-gateway-v6     (Container default gateway IPv6 address)
--default-ipc-mode       (Default mode for containers ipc ("shareable" | "private"))
--default-network-opt    (Default network options)
--default-runtime        (Default OCI runtime for containers)
--default-shm-size       (Default shm size for containers)
--default-ulimit         (Default ulimits for containers)

After this PR, we need to improve the code to provide completion for flag values, so this is just the initial start.

Copy link
Contributor

@vvoland vvoland left a comment

Choose a reason for hiding this comment

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

LGTM

@thaJeztah thaJeztah merged commit e2eea37 into moby:master Jan 27, 2025
171 checks passed
@thaJeztah thaJeztah deleted the enable_shell_completion branch January 27, 2025 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/cli Client docs/revisit impact/changelog impact/cli kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. status/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants