add better completions for homebrew

This commit is contained in:
Steffen Vogel 2019-07-21 15:01:24 +02:00
parent 3a8905c59b
commit a667ce65ff
7 changed files with 58 additions and 2010 deletions

View File

@ -1,430 +0,0 @@
#!bash
#
# bash completion for docker-compose
#
# This work is based on the completion for the docker command.
#
# This script provides completion of:
# - commands and their options
# - service names
# - filepaths
#
# To enable the completions either:
# - place this file in /etc/bash_completion.d
# or
# - copy this file to e.g. ~/.docker-compose-completion.sh and add the line
# below to your .bashrc after bash completion features are loaded
# . ~/.docker-compose-completion.sh
# For compatibility reasons, Compose and therefore its completion supports several
# stack compositon files as listed here, in descending priority.
# Support for these filenames might be dropped in some future version.
__docker_compose_compose_file() {
local file
for file in docker-compose.y{,a}ml fig.y{,a}ml ; do
[ -e $file ] && {
echo $file
return
}
done
echo docker-compose.yml
}
# Extracts all service names from the compose file.
___docker_compose_all_services_in_compose_file() {
awk -F: '/^[a-zA-Z0-9]/{print $1}' "${compose_file:-$(__docker_compose_compose_file)}" 2>/dev/null
}
# All services, even those without an existing container
__docker_compose_services_all() {
COMPREPLY=( $(compgen -W "$(___docker_compose_all_services_in_compose_file)" -- "$cur") )
}
# All services that have an entry with the given key in their compose_file section
___docker_compose_services_with_key() {
# 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;}' "${compose_file:-$(__docker_compose_compose_file)}" | awk -F: -v key=": +$1:" '$0 ~ key {print $1}'
}
# All services that are defined by a Dockerfile reference
__docker_compose_services_from_build() {
COMPREPLY=( $(compgen -W "$(___docker_compose_services_with_key build)" -- "$cur") )
}
# All services that are defined by an image
__docker_compose_services_from_image() {
COMPREPLY=( $(compgen -W "$(___docker_compose_services_with_key image)" -- "$cur") )
}
# The services for which containers have been created, optionally filtered
# by a boolean expression passed in as argument.
__docker_compose_services_with() {
local containers names
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=( ${names[@]%_*} ) # strip trailing numbers
names=( ${names[@]#*_} ) # strip project name
COMPREPLY=( $(compgen -W "${names[*]}" -- "$cur") )
}
# The services for which at least one paused container exists
__docker_compose_services_paused() {
__docker_compose_services_with '.State.Paused'
}
# The services for which at least one running container exists
__docker_compose_services_running() {
__docker_compose_services_with '.State.Running'
}
# The services for which at least one stopped container exists
__docker_compose_services_stopped() {
__docker_compose_services_with 'not .State.Running'
}
_docker_compose_build() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help --no-cache" -- "$cur" ) )
;;
*)
__docker_compose_services_from_build
;;
esac
}
_docker_compose_docker_compose() {
case "$prev" in
--file|-f)
_filedir "y?(a)ml"
return
;;
--project-name|-p)
return
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help -h --verbose --version -v --file -f --project-name -p" -- "$cur" ) )
;;
*)
COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
;;
esac
}
_docker_compose_help() {
COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
}
_docker_compose_kill() {
case "$prev" in
-s)
COMPREPLY=( $( compgen -W "SIGHUP SIGINT SIGKILL SIGUSR1 SIGUSR2" -- "$(echo $cur | tr '[:lower:]' '[:upper:]')" ) )
return
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help -s" -- "$cur" ) )
;;
*)
__docker_compose_services_running
;;
esac
}
_docker_compose_logs() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help --no-color" -- "$cur" ) )
;;
*)
__docker_compose_services_all
;;
esac
}
_docker_compose_migrate_to_labels() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
;;
esac
}
_docker_compose_pause() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
;;
*)
__docker_compose_services_running
;;
esac
}
_docker_compose_port() {
case "$prev" in
--protocol)
COMPREPLY=( $( compgen -W "tcp udp" -- "$cur" ) )
return;
;;
--index)
return;
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help --index --protocol" -- "$cur" ) )
;;
*)
__docker_compose_services_all
;;
esac
}
_docker_compose_ps() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help -q" -- "$cur" ) )
;;
*)
__docker_compose_services_all
;;
esac
}
_docker_compose_pull() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
;;
*)
__docker_compose_services_from_image
;;
esac
}
_docker_compose_restart() {
case "$prev" in
--timeout|-t)
return
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help --timeout -t" -- "$cur" ) )
;;
*)
__docker_compose_services_running
;;
esac
}
_docker_compose_rm() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--force -f --help -v" -- "$cur" ) )
;;
*)
__docker_compose_services_stopped
;;
esac
}
_docker_compose_run() {
case "$prev" in
-e)
COMPREPLY=( $( compgen -e -- "$cur" ) )
compopt -o nospace
return
;;
--entrypoint|--user|-u)
return
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "-d --entrypoint -e --help --no-deps --rm --service-ports --publish -p -T --user -u" -- "$cur" ) )
;;
*)
__docker_compose_services_all
;;
esac
}
_docker_compose_scale() {
case "$prev" in
=)
COMPREPLY=("$cur")
return
;;
--timeout|-t)
return
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help --timeout -t" -- "$cur" ) )
;;
*)
COMPREPLY=( $(compgen -S "=" -W "$(___docker_compose_all_services_in_compose_file)" -- "$cur") )
compopt -o nospace
;;
esac
}
_docker_compose_start() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
;;
*)
__docker_compose_services_stopped
;;
esac
}
_docker_compose_stop() {
case "$prev" in
--timeout|-t)
return
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help --timeout -t" -- "$cur" ) )
;;
*)
__docker_compose_services_running
;;
esac
}
_docker_compose_unpause() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
;;
*)
__docker_compose_services_paused
;;
esac
}
_docker_compose_up() {
case "$prev" in
--timeout|-t)
return
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "-d --help --no-build --no-color --no-deps --no-recreate --force-recreate --timeout -t" -- "$cur" ) )
;;
*)
__docker_compose_services_all
;;
esac
}
_docker_compose_version() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--short" -- "$cur" ) )
;;
esac
}
_docker_compose() {
local previous_extglob_setting=$(shopt -p extglob)
shopt -s extglob
local commands=(
build
help
kill
logs
migrate-to-labels
pause
port
ps
pull
restart
rm
run
scale
start
stop
unpause
up
version
)
COMPREPLY=()
local cur prev words cword
_get_comp_words_by_ref -n : cur prev words cword
# search subcommand and invoke its handler.
# special treatment of some top-level options
local command='docker_compose'
local counter=1
local compose_file compose_project
while [ $counter -lt $cword ]; do
case "${words[$counter]}" in
--file|-f)
(( counter++ ))
compose_file="${words[$counter]}"
;;
--project-name|p)
(( counter++ ))
compose_project="${words[$counter]}"
;;
-*)
;;
*)
command="${words[$counter]}"
break
;;
esac
(( counter++ ))
done
local completions_func=_docker_compose_${command//-/_}
declare -F $completions_func >/dev/null && $completions_func
eval "$previous_extglob_setting"
return 0
}
complete -F _docker_compose docker-compose

File diff suppressed because it is too large Load Diff

View File

@ -21,6 +21,14 @@ alias a=ansible
alias c=code-insiders
alias e=$EDITOR
# Completion for aliases
# See: https://github.com/cykerway/complete-alias
complete -F _complete_alias g
complete -F _complete_alias k
complete -F _complete_alias a
complete -F _complete_alias c
complete -F _complete_alias e
# Some shortcuts for me
function moby() {
[ -f ~/.bash_env ] && source ~/.bash_env

View File

@ -34,15 +34,15 @@ if type kubectl > /dev/null 2>&1; then
source <(kubectl completion bash)
fi
if type helm > /dev/null 2>&1; then
source <(helm completion bash)
fi
if type doctl > /dev/null 2>&1; then
source <(doctl completion bash)
fi
if command -v brew; then
if type helm > /dev/null 2>&1; then
source <(helm completion bash)
fi
if type brew > /dev/null 2>&1; then
HOMEBREW_PREFIX=$(brew --prefix)
for COMPLETION in "$HOMEBREW_PREFIX"/etc/bash_completion.d/*; do
[[ -f $COMPLETION ]] && source "$COMPLETION"
@ -51,3 +51,12 @@ if command -v brew; then
source "${HOMEBREW_PREFIX}/etc/profile.d/bash_completion.sh"
fi
fi
# Docker completions
for COMPLETION in /Applications/Docker.app/Contents/Resources/etc/*.bash-completion; do
[ -f ${COMPLETION} ] && source ${COMPLETION}
done
# Add completion to aliases
# See: https://github.com/cykerway/complete-alias
source ~/.homesick/repos/complete-alias/bash_completion.sh

View File

@ -1,25 +1,22 @@
export GOPATH=~/build/go
source ${HOME}/.cargo/env
if [ -d ~/bin ]; then
export PATH=${PATH}:~/bin/scripts:~/bin
fi
export GOPATH=~/build/go
export PATH=~/bin/scripts:~/bin:/usr/local/bin:${PATH}
if [ -d /var/lib/snapd/snap/bin/code ]; then
export PATH="$PATH:/var/lib/snapd/snap/bin/code"
export PATH="/var/lib/snapd/snap/bin/code:${PATH}"
fi
if [ -d ${KREW_ROOT:-$HOME/.krew}/bin ]; then
PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
PATH="${KREW_ROOT:-${HOME}/.krew}/bin:${PATH}"
fi
if [ "$(id -u)" -eq "0" ]; then
if [ -d /opt/local/sbin ]; then
export PATH=/opt/local/sbin:$PATH
export PATH=/opt/local/sbin:${PATH}
fi
export PATH=/usr/local/sbin:$PATH
export PATH=/usr/local/sbin:${PATH}
fi
if [ -d ~/.npm-global/bin ]; then
@ -31,14 +28,19 @@ if [ -d ${GOPATH}/bin ]; then
fi
# OS X stuff if availabe
# MacGPG2
if [ -d /usr/local/MacGPG2/bin/ ]; then
export PATH=/usr/local/MacGPG2/bin/:$PATH
fi
if type brew > /dev/null 2>&1; then
PATH=$(brew --prefix coreutils)/libexec/gnubin:${PATH}
fi
# Linux man-pages
if [ -d /opt/man-pages ]; then
export MANPATH=/opt/man-pages:$MANPATH
export MANPATH=/opt/man-pages:${MANPATH}
fi
if [ -x /usr/local/bin/code-insiders ]; then
@ -54,8 +56,6 @@ export GCC_COLORS="error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quo
export DEBEMAIL="post@steffenvogel.de"
export DEBFULLNAME="Steffen Vogel"
export DROPBOX=/Volumes/Extern/Sync/Dropbox
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

View File

@ -27,10 +27,10 @@ if [ -x /usr/bin/lesspipe ]; then eval "$(lesspipe)"; fi
source ~/.homesick/repos/homeshick/homeshick.sh
source ~/.homesick/repos/homeshick/completions/homeshick-completion.bash
source ~/.bash_aliases
source ~/.bash_env
source ~/.bash_prompt
source ~/.bash_completion
source ~/.bash_aliases
# enable color support of ls and also add handy aliases
if hash dircolors 2>/dev/null; then

View File

@ -70,6 +70,29 @@ brew install \
ripgrep \
wget \
wireshark \
binutils \
diffutils \
findutils --with-default-names \
gawk \
gnu-indent --with-default-names \
gnu-sed --with-default-names \
gnu-tar --with-default-names \
gnu-which --with-default-names \
gnutls \
grep --with-default-names \
gzip \
screen \
watch \
wdiff --with-gettext \
tmux \
bash \
gdb \
gpatch \
m4 \
make \
nano \
less \
rsync
brew cask install \
meld