Last active
September 2, 2016 09:56
docker ip script with bash auto completion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
alias docker-ip="docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'" | |
function _docker-ip() { | |
# No need to worry about spaces in this output, because | |
# docker only allows names in the form [a-zA-Z0-9][a-zA-Z0-9_.-]* | |
local pos_comps=$(docker ps --format "{{.ID}} {{.Names}}") | |
# $COMP_WORDS array with the current line | |
# $COMP_WORD the current word we're comping | |
# e.g. | |
# "docker-ip bana$" COMP_WORDS=(docker-ip bana) COMP_WORD=1 | |
# "docker-ip bana $" COMP_WORDS=(docker-ip bana "") COMP_WORD=2 | |
# compgen -W "list of possible completions" -- "current word to complete" | |
# We need to populate $COMPREPLY with an _array_ of possibilities. | |
COMPREPLY=( $(compgen -W "${pos_comps}" -- ${COMP_WORDS[$COMP_CWORD]}) ) | |
return 0 | |
} | |
complete -o nospace -F _docker-ip docker-ip |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment