Skip to content

Instantly share code, notes, and snippets.

@neoshrew
Last active September 2, 2016 09:56
Show Gist options
  • Save neoshrew/28a69c547432f5e90a47f1c95d499f79 to your computer and use it in GitHub Desktop.
Save neoshrew/28a69c547432f5e90a47f1c95d499f79 to your computer and use it in GitHub Desktop.
docker ip script with bash auto completion
#!/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