Improve bash completion for `docker swarm {init,join} --{advertise,listen}-addr}`
@@ -414,6 +414,20 @@ __docker_complete_resolved_hostname() {
COMPREPLY=( $(host 2>/dev/null "${cur%:}" | awk '/has address/ {print $4}') )
}
+__docker_local_interfaces() {
+ command -v ip >/dev/null 2>&1 || return
+ ip addr show scope global 2>/dev/null | sed -n 's| \+inet \([0-9.]\+\).* \([^ ]\+\)|\1 \2|p'
+}
+
+__docker_complete_local_interfaces() {
+ local additional_interface
+ if [ "$1" = "--add" ] ; then
+ additional_interface="$2"
+ fi
+
+ COMPREPLY=( $( compgen -W "$(__docker_local_interfaces) $additional_interface" -- "$cur" ) )
+}
+
__docker_complete_capabilities() {
# The list of capabilities is defined in types.go, ALL was added manually.
COMPREPLY=( $( compgen -W "
@@ -1836,15 +1850,21 @@ _docker_swarm() {
_docker_swarm_init() {
case "$prev" in
- --listen-addr)
+ --advertise-addr)
if [[ $cur == *: ]] ; then
COMPREPLY=( $( compgen -W "2377" -- "${cur##*:}" ) )
+ else
+ __docker_complete_local_interfaces
+ __docker_nospace
fi
return
;;
- --advertise-addr)
+ --listen-addr)
if [[ $cur == *: ]] ; then
COMPREPLY=( $( compgen -W "2377" -- "${cur##*:}" ) )
+ else
+ __docker_complete_local_interfaces --add 0.0.0.0
+ __docker_nospace
fi
return
;;
@@ -1859,21 +1879,27 @@ _docker_swarm_init() {
_docker_swarm_join() {
case "$prev" in
- --token)
- return
- ;;
- --listen-addr)
+ --advertise-addr)
if [[ $cur == *: ]] ; then
COMPREPLY=( $( compgen -W "2377" -- "${cur##*:}" ) )
+ else
+ __docker_complete_local_interfaces
+ __docker_nospace
fi
return
;;
- --advertise-addr)
+ --listen-addr)
if [[ $cur == *: ]] ; then
COMPREPLY=( $( compgen -W "2377" -- "${cur##*:}" ) )
+ else
+ __docker_complete_local_interfaces --add 0.0.0.0
+ __docker_nospace
fi
return
;;
+ --token)
+ return
+ ;;
esac
case "$cur" in
