compose icon indicating copy to clipboard operation
compose copied to clipboard

Command-line completion?

Open sbellem opened this issue 3 years ago • 41 comments

I just wonder if there's a command completion like there is for the docker-compose command.

sbellem avatar Feb 22 '21 21:02 sbellem

Should be easy to generate one, as Cobra command library includes support for generating completion scripts https://github.com/spf13/cobra/blob/master/shell_completions.md

ndeloof avatar Feb 22 '21 22:02 ndeloof

Are we looking to reach parity with docker-compose or is this not a requirement?

mat007 avatar May 26 '21 13:05 mat007

@mat007 we don't have any shell completion support for docker compose yet, so we can start with a minimal one, then would be nice we can rely on cobra's BashCompletionFunction or ValidArgsFunction. This would require the backend to be involved providing valid values, so maybe not as simple as it sounds with the current "backend-neutral" architecture.

ndeloof avatar May 26 '21 14:05 ndeloof

In the mean time, does anyone know if there's a way to configure docker compose completion to fall back to the old docker-compose completion?

glenjamin avatar Aug 24 '21 15:08 glenjamin

In the mean time, does anyone know if there's a way to configure docker compose completion to fall back to the old docker-compose completion?

This might suggest a way to do that: https://stackoverflow.com/questions/55447023/how-do-i-defer-shell-completion-to-another-command-in-bash-and-zsh

Mahoney avatar Nov 16 '21 14:11 Mahoney

There was some completion but it was removed in https://github.com/docker/compose/commit/c28aec2308a24f589814cf4ac55a225d144f557d

SuperSandro2000 avatar Nov 25 '21 16:11 SuperSandro2000

AFAICT offering command line completion for docker compose (space) isn't trivial due to the CLI plugin architecture, but we could automatically produce completion for docker-compose (when used standalone) using Cobra command support for this feature. Wdyt @ulyssessouza @mat007 ?

ndeloof avatar Nov 27 '21 15:11 ndeloof

I was pretty sure this was already in place, but apparently it’s either not fully implemented or not working. We’ll have a look: internally tracked as https://docker.atlassian.net/browse/IL-768

mat007 avatar Nov 27 '21 16:11 mat007

IIRC @ulyssessouza experimented with this, but he was blocked by docker CLI plugin architecture (we can't extend the docker CLI completion script) But for direct use of docker-compose binary, we could adopt https://gist.github.com/jsteenb2/0973c838b00940ee8ccdf958a79756af and also add support for completion functions to auto-complete flags / args values.

ndeloof avatar Nov 27 '21 17:11 ndeloof

This is very strange indeed. What happened with the bash auto completion. Otherwise I would have created a PR for upgrading docker-compose to v2.2.2 on https://github.com/docker/docker.github.io/

melroy89 avatar Dec 14 '21 03:12 melroy89

Please note the existing docker-compose bash completion still works with compose v2 when used as a standalone binary. But it would beed some update to add support for additional commands and flags

ndeloof avatar Dec 14 '21 08:12 ndeloof

This problem has been solved? in some version?

stefanocudini avatar Feb 02 '22 10:02 stefanocudini

Should be easy to generate one, as Cobra command library includes support for generating completion scripts https://github.com/spf13/cobra/blob/master/shell_completions.md @ndeloof were you able to generate completion scripts? for compose v2?

stefanocudini avatar Feb 02 '22 10:02 stefanocudini

This problem has been solved? in some version?

No, this doesn't seem to be solved yet.

PHLAK avatar Feb 02 '22 17:02 PHLAK

Ok i think i missed something :thinking: : Is docker compose invoking docker-compose or is it compiled into the docker binary?

rsteube avatar Feb 08 '22 23:02 rsteube

docker compose aka docker-compose v2 is a plugin for the docker command written in go and is not using any python stuff from docker-compose v1.

SuperSandro2000 avatar Feb 09 '22 01:02 SuperSandro2000

@SuperSandro2000 thanks. I got the shift from python to go, but I didn't catch that it is integrated as plugin.

rsteube avatar Feb 09 '22 02:02 rsteube

for completeness: under the hood, docker's cli-plugins handling does invoke a docker-compose executable (which happens to be written in golang these days indeed), which typically lives in /usr/lib/docker/cli-plugins/docker-compose and should not be called directly. (this is similar to how git handles subcommand ‘plugins’)

wbolster avatar Feb 09 '22 16:02 wbolster

docker-compose executable (which happens to be written in golang these days indeed), which typically lives in /usr/lib/docker/cli-plugins/docker-compose and should not be called directly. (this is similar to how git handles subcommand ‘plugins’)

Yeah, I was worried about that as I've been invoking docker-compose (which still exists in my PATH) for the completions - should be docker compose then.

Think i got it working though: asciicast

rsteube avatar Feb 09 '22 23:02 rsteube

IIRC @ulyssessouza experimented with this, but he was blocked by docker CLI plugin architecture (we can't extend the docker CLI completion script)

@ndeloof i think this should be possible if you can update the docker cli to a newer cobra version. It changed in recent versions from full completion script generation to invoking the binary.

In docker cli you just add a subcommand for each (installed) plugin with flag parsing disabled like here. Then in the ValidArgsFunction invoke the __complete subcommand of the plugin with args and toComplete as arguments similar to this (ignore the directive handling as it is not needed in this case).

The __complete command outputs sth. like this:

value
value\twithDescription
:4
# empty line

So just return lines[:len(lines)-2] and :4 parsed as integer (ShellCompDirecive).

rsteube avatar Feb 15 '22 10:02 rsteube

Started experimenting with cobra 1.3.0 to adopt cobra (dynamic) completion v2 : https://github.com/docker/cli/pull/3429

ndeloof avatar Feb 17 '22 13:02 ndeloof

Nice. If it's any help for anyone in the meantime: got my completer at carapace-bin. Just source the script from carapace docker and it should work with the compose and buildx subcommands.

rsteube avatar Feb 17 '22 14:02 rsteube

As temporal workaround I have installed compose switch and applied v1 completion. it requires me to use it as docker-compose (not as docker compose) but completion works

zaverden avatar Mar 11 '22 04:03 zaverden

(Not sure if a new issue needs to be opned but i am adding it here since it's a related thing).

The documentation provides bash script for auto-completion (for version 2.4) but the link to the script (https://docs.docker.com/compose/completion/) is broken.

Place the completion script in /etc/bash_completion.d/.

$  sudo curl \
    -L https://raw.githubusercontent.com/docker/compose/v2.4.1/contrib/completion/bash/docker-compose \
    -o /etc/bash_completion.d/docker-compose

$ cat /etc/bash_completion.d/docker-compose 
404: Not Found

Also, as a quick fix, adding aliases to ~/.bash_aliases worked for me (on Linux Mint OS):

alias dc='docker compose'
alias dcf='docker compose -f'

After this, auto-complete atleast for file names worked:

// hit tab after typing dcf
dcf <start typing first few letters of file-name& hit TAB>

IamGroot19 avatar May 01 '22 04:05 IamGroot19

Command line completion for docker compose as a docker CLI plugin will be enabled by https://github.com/docker/cli/pull/3429

ndeloof avatar May 02 '22 07:05 ndeloof

https://docs.docker.com/compose/completion/ states that sudo curl -L https://raw.githubusercontent.com/docker/compose/v2.5.0/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose is a valid way to go, but https://raw.githubusercontent.com/docker/compose/v2.5.0/contrib/completion/bash/docker-compose is a 404.

konstruktoid avatar May 06 '22 08:05 konstruktoid

@konstruktoid thanks for reporting. I guess doc was updated for v2.5 release but this is actually link for docker-compose 1.x completion

ndeloof avatar May 06 '22 08:05 ndeloof

https://docs.docker.com/compose/completion/ states that sudo curl -L https://raw.githubusercontent.com/docker/compose/v2.5.0/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose is a valid way to go, but https://raw.githubusercontent.com/docker/compose/v2.5.0/contrib/completion/bash/docker-compose is a 404.

Ran into this today, still not fixed... Can you link to the Issue?

rollsover avatar May 11 '22 13:05 rollsover

https://github.com/docker/docker.github.io/issues/14692

konstruktoid avatar May 11 '22 13:05 konstruktoid

Updating the Docker CLI completion ( curl -L https://raw.githubusercontent.com/docker/cli/master/contrib/completion/bash/docker -o /usr/share/bash-completion/completions/docker ) did the tirck while waiting for https://github.com/docker/cli/pull/3429 become releesed (v20.10.17?).

sebaspf avatar Jun 01 '22 11:06 sebaspf