├── docker-complete └── README.creole /docker-complete: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Bash completion for docker 4 | # Currently does sub commands and then names for kill 5 | # 6 | # Depends: socat, jq, sed, awk 7 | 8 | _docker_names() 9 | { 10 | printf "GET /containers/json HTTP/1.1\r\n\r\n" \ 11 | | sudo socat - unix-connect:/var/run/docker.sock \ 12 | | tr -d '\r' \ 13 | | ( DOIT=0 ; while read line 14 | do 15 | if [ "$DOIT" == "1" ] ; then echo $line ; fi 16 | if [ "$line" == "" ] ; then DOIT="1" ; fi 17 | done ; echo "]" // bizarrely socat drops the end "]"?? 18 | ) \ 19 | | jq -r 'map(.Names | .[0]) | .[]' 2> /dev/null \ 20 | | sed -rne 's|/(.*)|\1|p' 21 | } 22 | 23 | _docker() 24 | { 25 | local cur prev opts 26 | COMPREPLY=() 27 | cur="${COMP_WORDS[COMP_CWORD]}" 28 | prev="${COMP_WORDS[COMP_CWORD-1]}" 29 | opts="$(docker help 2>&1 | awk '/Commands:/ {DOIT=1; next} DOIT==1 {print $1}' | tr '\n' ' ')" 30 | 31 | case "${prev}" in 32 | kill|logs|inspect) 33 | local names="$(_docker_names)" 34 | COMPREPLY=( $(compgen -W "${names}" -- ${cur}) ) 35 | return 0 36 | ;; 37 | esac 38 | 39 | COMPREPLY=($(compgen -W "${opts}" -- ${cur})) 40 | return 0 41 | } 42 | 43 | 44 | case "$1" in 45 | depends) 46 | which jq && exit 1 47 | which socat && exit 2 48 | ;; 49 | 50 | names) 51 | _docker_names 52 | ;; 53 | 54 | *) 55 | # Setup the completion by default 56 | complete -F _docker docker 57 | ;; 58 | esac 59 | 60 | # docker-complete ends here 61 | -------------------------------------------------------------------------------- /README.creole: -------------------------------------------------------------------------------- 1 | auto completion for docker with the BASH shell. 2 | 3 | === Try the official one instead? === 4 | 5 | https://github.com/docker/docker/blob/master/contrib/completion/bash/docker 6 | 7 | I wrote this because I couldn't find the official one but also because 8 | it was interesting. 9 | 10 | But it turns out I can't get the official one to work very well. The 11 | official ones uses the standard output of the commands and this one 12 | uses the API. Maybe that makes a difference? 13 | 14 | 15 | === installation === 16 | 17 | You first need the following dependencies: 18 | 19 | * socat 20 | ** available in ubuntu with: 21 | 22 | {{{ 23 | apt-get install socat 24 | }}} 25 | 26 | * jq 27 | ** the indispensible jq program 28 | ** available in ubuntu with: 29 | 30 | {{{ 31 | apt-get install jq 32 | }}} 33 | 34 | * GNU Sed 35 | ** should be on all good unix systems 36 | 37 | Then just pull this file and include it in your shell. 38 | 39 | {{{ 40 | $ curl -o ~/.docker-complete https://raw.githubusercontent.com/nicferrier/docker-bash-completion/master/docker-complete 41 | $ source ~/.docker-complete 42 | }}} 43 | 44 | Of course, you could also add it to your {{{.bashrc}}} 45 | 46 | 47 | Alternately, clone this repo on your boxes: 48 | 49 | {{{ 50 | $ git clone https://github.com/nicferrier/docker-bash-completion.git 51 | }}} 52 | 53 | and then you can: 54 | 55 | {{{ 56 | $ source docker-bash-completion/docker-complete 57 | }}} 58 | 59 | 60 | === docker commands === 61 | 62 | This ac will complete docker sub-commands (ps, images, etc...) for 63 | you. It does this automatically based on the docker help. 64 | 65 | It won't do any further completion except in specific cases. 66 | 67 | Example: 68 | 69 | {{{ 70 | $ docker s[TAB] 71 | save search start stop 72 | $ docker se[TAB] 73 | $ docker search 74 | }}} 75 | 76 | === docker kill === 77 | 78 | This ac will complete docker names for you (based on the short name 79 | output by the {{{docker ps}}} command). 80 | 81 | {{{ 82 | $ docker ps 83 | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 84 | aa571058142e nicferrier/elnode:latest /bin/sh -c '/usr/loc 4 hours ago Up 4 hours 6001/tcp, 0.0.0.0:8000->8000/tcp berserk_hoover 85 | $ docker kill [TAB] 86 | $ docker kill berserk_hoover 87 | }}} 88 | 89 | This was actually why I wrote it but because the output of {{{docker 90 | ps}}} isn't parseable it's also this that requires the {{{socat}}} and 91 | {{{jq}}} dependencies. Frustrating. All tools everywhere should always 92 | provide parseable output and no, "you can use the API" isn't really an 93 | excuse. 94 | 95 | === docker inspect, docker logs === 96 | 97 | Just do the same as {{{docker kill}}} 98 | 99 | Short names only. 100 | --------------------------------------------------------------------------------