Merge pull request #871 from albers/bash-completion

Rebrand bash completion script
This commit is contained in:
Daniel Nephin 2015-01-22 12:52:27 -05:00
commit ef027599f7
1 changed files with 57 additions and 61 deletions

View File

@ -1,6 +1,6 @@
#!bash #!bash
# #
# bash completion for fig commands # bash completion for docker-compose
# #
# This work is based on the completion for the docker command. # This work is based on the completion for the docker command.
# #
@ -12,46 +12,42 @@
# To enable the completions either: # To enable the completions either:
# - place this file in /etc/bash_completion.d # - place this file in /etc/bash_completion.d
# or # or
# - copy this file and add the line below to your .bashrc after # - copy this file to e.g. ~/.docker-compose-completion.sh and add the line
# bash completion features are loaded # below to your .bashrc after bash completion features are loaded
# . docker.bash # . ~/.docker-compose-completion.sh
#
# Note:
# Some completions require the current user to have sufficient permissions
# to execute the docker command.
# Extracts all service names from the figfile. # Extracts all service names from docker-compose.yml.
___fig_all_services_in_figfile() { ___docker-compose_all_services_in_compose_file() {
awk -F: '/^[a-zA-Z0-9]/{print $1}' "${fig_file:-fig.yml}" awk -F: '/^[a-zA-Z0-9]/{print $1}' "${compose_file:-docker-compose.yml}"
} }
# All services, even those without an existing container # All services, even those without an existing container
__fig_services_all() { __docker-compose_services_all() {
COMPREPLY=( $(compgen -W "$(___fig_all_services_in_figfile)" -- "$cur") ) COMPREPLY=( $(compgen -W "$(___docker-compose_all_services_in_compose_file)" -- "$cur") )
} }
# All services that have an entry with the given key in their figfile section # All services that have an entry with the given key in their docker-compose.yml section
___fig_services_with_key() { ___docker-compose_services_with_key() {
# flatten sections to one line, then filter lines containing the key and return section name. # flatten sections to one line, then filter lines containing the key and return section name.
awk '/^[a-zA-Z0-9]/{printf "\n"};{printf $0;next;}' fig.yml | awk -F: -v key=": +$1:" '$0 ~ key {print $1}' awk '/^[a-zA-Z0-9]/{printf "\n"};{printf $0;next;}' "${compose_file:-docker-compose.yml}" | awk -F: -v key=": +$1:" '$0 ~ key {print $1}'
} }
# All services that are defined by a Dockerfile reference # All services that are defined by a Dockerfile reference
__fig_services_from_build() { __docker-compose_services_from_build() {
COMPREPLY=( $(compgen -W "$(___fig_services_with_key build)" -- "$cur") ) COMPREPLY=( $(compgen -W "$(___docker-compose_services_with_key build)" -- "$cur") )
} }
# All services that are defined by an image # All services that are defined by an image
__fig_services_from_image() { __docker-compose_services_from_image() {
COMPREPLY=( $(compgen -W "$(___fig_services_with_key image)" -- "$cur") ) COMPREPLY=( $(compgen -W "$(___docker-compose_services_with_key image)" -- "$cur") )
} }
# The services for which containers have been created, optionally filtered # The services for which containers have been created, optionally filtered
# by a boolean expression passed in as argument. # by a boolean expression passed in as argument.
__fig_services_with() { __docker-compose_services_with() {
local containers names local containers names
containers="$(fig 2>/dev/null ${fig_file:+-f $fig_file} ${fig_project:+-p $fig_project} ps -q)" containers="$(docker-compose 2>/dev/null ${compose_file:+-f $compose_file} ${compose_project:+-p $compose_project} ps -q)"
names=( $(docker 2>/dev/null inspect --format "{{if ${1:-true}}} {{ .Name }} {{end}}" $containers) ) names=( $(docker 2>/dev/null inspect --format "{{if ${1:-true}}} {{ .Name }} {{end}}" $containers) )
names=( ${names[@]%_*} ) # strip trailing numbers names=( ${names[@]%_*} ) # strip trailing numbers
names=( ${names[@]#*_} ) # strip project name names=( ${names[@]#*_} ) # strip project name
@ -59,29 +55,29 @@ __fig_services_with() {
} }
# The services for which at least one running container exists # The services for which at least one running container exists
__fig_services_running() { __docker-compose_services_running() {
__fig_services_with '.State.Running' __docker-compose_services_with '.State.Running'
} }
# The services for which at least one stopped container exists # The services for which at least one stopped container exists
__fig_services_stopped() { __docker-compose_services_stopped() {
__fig_services_with 'not .State.Running' __docker-compose_services_with 'not .State.Running'
} }
_fig_build() { _docker-compose_build() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "--no-cache" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--no-cache" -- "$cur" ) )
;; ;;
*) *)
__fig_services_from_build __docker-compose_services_from_build
;; ;;
esac esac
} }
_fig_fig() { _docker-compose_docker-compose() {
case "$prev" in case "$prev" in
--file|-f) --file|-f)
_filedir _filedir
@ -103,12 +99,12 @@ _fig_fig() {
} }
_fig_help() { _docker-compose_help() {
COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) ) COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
} }
_fig_kill() { _docker-compose_kill() {
case "$prev" in case "$prev" in
-s) -s)
COMPREPLY=( $( compgen -W "SIGHUP SIGINT SIGKILL SIGUSR1 SIGUSR2" -- "$(echo $cur | tr '[:lower:]' '[:upper:]')" ) ) COMPREPLY=( $( compgen -W "SIGHUP SIGINT SIGKILL SIGUSR1 SIGUSR2" -- "$(echo $cur | tr '[:lower:]' '[:upper:]')" ) )
@ -121,25 +117,25 @@ _fig_kill() {
COMPREPLY=( $( compgen -W "-s" -- "$cur" ) ) COMPREPLY=( $( compgen -W "-s" -- "$cur" ) )
;; ;;
*) *)
__fig_services_running __docker-compose_services_running
;; ;;
esac esac
} }
_fig_logs() { _docker-compose_logs() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "--no-color" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--no-color" -- "$cur" ) )
;; ;;
*) *)
__fig_services_all __docker-compose_services_all
;; ;;
esac esac
} }
_fig_port() { _docker-compose_port() {
case "$prev" in case "$prev" in
--protocol) --protocol)
COMPREPLY=( $( compgen -W "tcp udp" -- "$cur" ) ) COMPREPLY=( $( compgen -W "tcp udp" -- "$cur" ) )
@ -155,54 +151,54 @@ _fig_port() {
COMPREPLY=( $( compgen -W "--protocol --index" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--protocol --index" -- "$cur" ) )
;; ;;
*) *)
__fig_services_all __docker-compose_services_all
;; ;;
esac esac
} }
_fig_ps() { _docker-compose_ps() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-q" -- "$cur" ) ) COMPREPLY=( $( compgen -W "-q" -- "$cur" ) )
;; ;;
*) *)
__fig_services_all __docker-compose_services_all
;; ;;
esac esac
} }
_fig_pull() { _docker-compose_pull() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "--allow-insecure-ssl" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--allow-insecure-ssl" -- "$cur" ) )
;; ;;
*) *)
__fig_services_from_image __docker-compose_services_from_image
;; ;;
esac esac
} }
_fig_restart() { _docker-compose_restart() {
__fig_services_running __docker-compose_services_running
} }
_fig_rm() { _docker-compose_rm() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "--force -v" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--force -v" -- "$cur" ) )
;; ;;
*) *)
__fig_services_stopped __docker-compose_services_stopped
;; ;;
esac esac
} }
_fig_run() { _docker-compose_run() {
case "$prev" in case "$prev" in
-e) -e)
COMPREPLY=( $( compgen -e -- "$cur" ) ) COMPREPLY=( $( compgen -e -- "$cur" ) )
@ -219,48 +215,48 @@ _fig_run() {
COMPREPLY=( $( compgen -W "--allow-insecure-ssl -d --entrypoint -e --no-deps --rm -T" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--allow-insecure-ssl -d --entrypoint -e --no-deps --rm -T" -- "$cur" ) )
;; ;;
*) *)
__fig_services_all __docker-compose_services_all
;; ;;
esac esac
} }
_fig_scale() { _docker-compose_scale() {
case "$prev" in case "$prev" in
=) =)
COMPREPLY=("$cur") COMPREPLY=("$cur")
;; ;;
*) *)
COMPREPLY=( $(compgen -S "=" -W "$(___fig_all_services_in_figfile)" -- "$cur") ) COMPREPLY=( $(compgen -S "=" -W "$(___docker-compose_all_services_in_compose_file)" -- "$cur") )
compopt -o nospace compopt -o nospace
;; ;;
esac esac
} }
_fig_start() { _docker-compose_start() {
__fig_services_stopped __docker-compose_services_stopped
} }
_fig_stop() { _docker-compose_stop() {
__fig_services_running __docker-compose_services_running
} }
_fig_up() { _docker-compose_up() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "--allow-insecure-ssl -d --no-build --no-color --no-deps --no-recreate" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--allow-insecure-ssl -d --no-build --no-color --no-deps --no-recreate" -- "$cur" ) )
;; ;;
*) *)
__fig_services_all __docker-compose_services_all
;; ;;
esac esac
} }
_fig() { _docker-compose() {
local commands=( local commands=(
build build
help help
@ -284,18 +280,18 @@ _fig() {
# search subcommand and invoke its handler. # search subcommand and invoke its handler.
# special treatment of some top-level options # special treatment of some top-level options
local command='fig' local command='docker-compose'
local counter=1 local counter=1
local fig_file fig_project local compose_file compose_project
while [ $counter -lt $cword ]; do while [ $counter -lt $cword ]; do
case "${words[$counter]}" in case "${words[$counter]}" in
-f|--file) -f|--file)
(( counter++ )) (( counter++ ))
fig_file="${words[$counter]}" compose_file="${words[$counter]}"
;; ;;
-p|--project-name) -p|--project-name)
(( counter++ )) (( counter++ ))
fig_project="${words[$counter]}" compose_project="${words[$counter]}"
;; ;;
-*) -*)
;; ;;
@ -307,10 +303,10 @@ _fig() {
(( counter++ )) (( counter++ ))
done done
local completions_func=_fig_${command} local completions_func=_docker-compose_${command}
declare -F $completions_func >/dev/null && $completions_func declare -F $completions_func >/dev/null && $completions_func
return 0 return 0
} }
complete -F _fig fig complete -F _docker-compose docker-compose