2012-08-09 23:59:01 +00:00
|
|
|
# written by David Pravec
|
|
|
|
# - feel free to /msg alekibango on IRC if you want to talk about this file
|
|
|
|
|
|
|
|
# TODO: check if --config|-c was used and use configured config file for queries
|
|
|
|
# TODO: solve somehow completion for salt -G pythonversion:[tab]
|
|
|
|
# (not sure what to do with lists)
|
|
|
|
# TODO: --range[tab] -- how?
|
|
|
|
# TODO: --compound[tab] -- how?
|
|
|
|
# TODO: use history to extract some words, esp. if ${cur} is empty
|
2014-01-27 02:38:03 +00:00
|
|
|
# TODO: TEST EVERYTHING a lot
|
2012-08-09 23:59:01 +00:00
|
|
|
# TODO: is it ok to use '--timeout 2' ?
|
|
|
|
|
|
|
|
|
|
|
|
_salt_get_grains(){
|
2016-03-01 15:37:18 +00:00
|
|
|
if [ "$1" = 'local' ] ; then
|
2012-12-06 09:02:26 +00:00
|
|
|
salt-call --out=txt -- grains.ls | sed 's/^.*\[//' | tr -d ",']" |sed 's:\([a-z0-9]\) :\1\: :g'
|
2012-08-09 23:59:01 +00:00
|
|
|
else
|
2016-07-25 19:16:43 +00:00
|
|
|
salt '*' --timeout 2 --hide-timeout --out=txt -- grains.ls | sed 's/^.*\[//' | tr -d ",']" |sed 's:\([a-z0-9]\) :\1\: :g'
|
2012-08-09 23:59:01 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
_salt_get_grain_values(){
|
|
|
|
if [ "$1" = 'local' ] ; then
|
2016-03-01 15:37:18 +00:00
|
|
|
salt-call --out=txt -- grains.item $1 |sed 's/^\S*:\s//' |grep -v '^\s*$'
|
2012-08-09 23:59:01 +00:00
|
|
|
else
|
2016-07-25 19:16:43 +00:00
|
|
|
salt '*' --timeout 2 --hide-timeout --out=txt -- grains.item $1 |sed 's/^\S*:\s//' |grep -v '^\s*$'
|
2012-08-09 23:59:01 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-07-26 12:56:20 +00:00
|
|
|
_salt_get_keys(){
|
|
|
|
for type in $*; do
|
|
|
|
# remove header from data:
|
|
|
|
salt-key --no-color -l $type | tail -n+2
|
|
|
|
done
|
|
|
|
}
|
2012-08-09 23:59:01 +00:00
|
|
|
|
|
|
|
_salt(){
|
2017-09-11 17:57:28 +00:00
|
|
|
CACHE_DIR="$HOME/.cache/salt-comp-cache_functions"
|
|
|
|
local _salt_cache_functions=${SALT_COMP_CACHE_FUNCTIONS:=$CACHE_DIR}
|
2016-07-25 19:58:22 +00:00
|
|
|
local _salt_cache_timeout=${SALT_COMP_CACHE_TIMEOUT:='last hour'}
|
|
|
|
|
|
|
|
if [ ! -d "$(dirname ${_salt_cache_functions})" ]; then
|
|
|
|
mkdir -p "$(dirname ${_salt_cache_functions})"
|
|
|
|
fi
|
|
|
|
|
2016-03-01 15:37:18 +00:00
|
|
|
local cur prev opts _salt_grains _salt_coms pprev ppprev
|
2012-08-09 23:59:01 +00:00
|
|
|
COMPREPLY=()
|
|
|
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
|
|
|
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
|
|
if [ ${COMP_CWORD} -gt 2 ]; then
|
2017-05-18 16:53:27 +00:00
|
|
|
pprev="${COMP_WORDS[COMP_CWORD-2]}"
|
2012-08-09 23:59:01 +00:00
|
|
|
fi
|
|
|
|
if [ ${COMP_CWORD} -gt 3 ]; then
|
2017-05-18 16:53:27 +00:00
|
|
|
ppprev="${COMP_WORDS[COMP_CWORD-3]}"
|
2012-08-09 23:59:01 +00:00
|
|
|
fi
|
|
|
|
|
2012-12-07 00:42:09 +00:00
|
|
|
opts="-h --help -d --doc --documentation --version --versions-report -c \
|
|
|
|
--config-dir= -v --verbose -t --timeout= -s --static -b --batch= \
|
|
|
|
--batch-size= -E --pcre -L --list -G --grain --grain-pcre -N \
|
2014-05-29 00:32:54 +00:00
|
|
|
--nodegroup -R --range -C --compound -I --pillar \
|
2012-12-07 00:42:09 +00:00
|
|
|
--return= -a --auth= --eauth= --extended-auth= -T --make-token -S \
|
|
|
|
--ipcidr --out=pprint --out=yaml --out=overstatestage --out=json \
|
|
|
|
--out=raw --out=highstate --out=key --out=txt --no-color --out-indent= "
|
|
|
|
|
2012-08-09 23:59:01 +00:00
|
|
|
if [[ "${cur}" == -* ]] ; then
|
|
|
|
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# 2 special cases for filling up grain values
|
|
|
|
case "${pprev}" in
|
|
|
|
-G|--grain|--grain-pcre)
|
|
|
|
if [ "${cur}" = ":" ]; then
|
2017-05-18 16:53:27 +00:00
|
|
|
COMPREPLY=($(compgen -W "`_salt_get_grain_values ${prev}`"))
|
2012-08-09 23:59:01 +00:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
;;
|
2016-03-01 15:37:18 +00:00
|
|
|
esac
|
2012-08-09 23:59:01 +00:00
|
|
|
case "${ppprev}" in
|
|
|
|
-G|--grain|--grain-pcre)
|
|
|
|
if [ "${prev}" = ":" ]; then
|
|
|
|
COMPREPLY=( $(compgen -W "`_salt_get_grain_values ${pprev}`" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
;;
|
2016-03-01 15:37:18 +00:00
|
|
|
esac
|
|
|
|
|
2012-08-09 23:59:01 +00:00
|
|
|
if [ "${cur}" = "=" ] && [[ "${prev}" == --* ]]; then
|
2016-03-01 15:37:18 +00:00
|
|
|
cur=""
|
2012-08-09 23:59:01 +00:00
|
|
|
fi
|
|
|
|
if [ "${prev}" = "=" ] && [[ "${pprev}" == --* ]]; then
|
|
|
|
prev="${pprev}"
|
|
|
|
fi
|
2016-03-01 15:37:18 +00:00
|
|
|
|
2012-08-09 23:59:01 +00:00
|
|
|
case "${prev}" in
|
2016-03-01 15:37:18 +00:00
|
|
|
|
2012-08-09 23:59:01 +00:00
|
|
|
-c|--config)
|
|
|
|
COMPREPLY=($(compgen -f -- ${cur}))
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
salt)
|
2016-07-26 12:58:59 +00:00
|
|
|
COMPREPLY=($(compgen -W "\'*\' ${opts} $(_salt_get_keys acc)" -- ${cur}))
|
2012-08-09 23:59:01 +00:00
|
|
|
return 0
|
|
|
|
;;
|
2016-03-01 15:37:18 +00:00
|
|
|
-E|--pcre)
|
2016-07-26 12:58:59 +00:00
|
|
|
COMPREPLY=($(compgen -W "$(_salt_get_keys acc)" -- ${cur}))
|
2012-08-09 23:59:01 +00:00
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
-G|--grain|--grain-pcre)
|
2016-03-01 15:37:18 +00:00
|
|
|
COMPREPLY=($(compgen -W "$(_salt_get_grains)" -- ${cur}))
|
2012-08-09 23:59:01 +00:00
|
|
|
return 0
|
2017-05-18 16:53:27 +00:00
|
|
|
;;
|
2012-08-09 23:59:01 +00:00
|
|
|
-C|--compound)
|
|
|
|
COMPREPLY=() # TODO: finish this one? how?
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
-t|--timeout)
|
|
|
|
COMPREPLY=($( compgen -W "1 2 3 4 5 6 7 8 9 10 15 20 30 40 60 90 120 180" -- ${cur}))
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
-b|--batch|--batch-size)
|
|
|
|
COMPREPLY=($(compgen -W "1 2 3 4 5 6 7 8 9 10 15 20 30 40 50 60 70 80 90 100 120 150 200"))
|
|
|
|
return 0
|
|
|
|
;;
|
2016-03-01 15:37:18 +00:00
|
|
|
-N|--nodegroup)
|
2017-05-18 16:53:27 +00:00
|
|
|
MASTER_CONFIG='/etc/salt/master'
|
2016-03-01 15:37:18 +00:00
|
|
|
COMPREPLY=($(compgen -W "`awk -F ':' 'BEGIN {print_line = 0}; /^nodegroups/ {print_line = 1;getline } print_line && /^ */ {print $1} /^[^ ]/ {print_line = 0}' <${MASTER_CONFIG}`" -- ${cur}))
|
|
|
|
return 0
|
2012-08-09 23:59:01 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2016-07-25 19:58:22 +00:00
|
|
|
# Regenerate cache if timed out
|
|
|
|
if [[ "$(stat --format=%Z ${_salt_cache_functions} 2>/dev/null)" -lt "$(date --date="${_salt_cache_timeout}" +%s)" ]]; then
|
|
|
|
# salt: get all functions on all minions
|
|
|
|
# sed: remove all array overhead and convert to newline separated list
|
|
|
|
# sort: chop out doubled entries, so overhead is minimal later during actual completion
|
|
|
|
salt '*' --timeout 2 --hide-timeout --out=txt -- sys.list_functions \
|
|
|
|
| sed "s/^.*\[//;s/[],']//g;s/ /\n/g" \
|
|
|
|
| sort -u \
|
|
|
|
> "${_salt_cache_functions}"
|
|
|
|
fi
|
|
|
|
|
2016-07-25 20:26:31 +00:00
|
|
|
# filter results, to only print the part to next dot (or end of function)
|
|
|
|
_salt_coms="$(sed 's/^\('${cur}'\(\.\|[^.]*\)\)\?.*/\1/' "${_salt_cache_functions}" | sort -u)"
|
|
|
|
|
|
|
|
# If there are still dots in the suggestion, do not append space
|
|
|
|
grep "^${cur}.*\." "${_salt_cache_functions}" &>/dev/null && compopt -o nospace
|
2016-07-25 19:58:22 +00:00
|
|
|
|
2012-08-09 23:59:01 +00:00
|
|
|
all="${opts} ${_salt_coms}"
|
|
|
|
COMPREPLY=( $(compgen -W "${all}" -- ${cur}) )
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
complete -F _salt salt
|
|
|
|
|
|
|
|
|
|
|
|
_saltkey(){
|
|
|
|
local cur prev opts prev pprev
|
|
|
|
COMPREPLY=()
|
|
|
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
|
|
|
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
2012-12-07 00:42:09 +00:00
|
|
|
opts="-c --config-dir= -h --help --version --versions-report -q --quiet \
|
|
|
|
-y --yes --gen-keys= --gen-keys-dir= --keysize= --key-logfile= \
|
2016-03-01 15:37:18 +00:00
|
|
|
-l --list= -L --list-all -a --accept= -A --accept-all \
|
|
|
|
-r --reject= -R --reject-all -p --print= -P --print-all \
|
2012-12-07 00:42:09 +00:00
|
|
|
-d --delete= -D --delete-all -f --finger= -F --finger-all \
|
|
|
|
--out=pprint --out=yaml --out=overstatestage --out=json --out=raw \
|
|
|
|
--out=highstate --out=key --out=txt --no-color --out-indent= "
|
2012-08-09 23:59:01 +00:00
|
|
|
if [ ${COMP_CWORD} -gt 2 ]; then
|
|
|
|
pprev="${COMP_WORDS[COMP_CWORD-2]}"
|
|
|
|
fi
|
|
|
|
if [ ${COMP_CWORD} -gt 3 ]; then
|
|
|
|
ppprev="${COMP_WORDS[COMP_CWORD-3]}"
|
|
|
|
fi
|
|
|
|
if [[ "${cur}" == -* ]] ; then
|
|
|
|
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "${cur}" = "=" ] && [[ "${prev}" == --* ]]; then
|
2016-03-01 15:37:18 +00:00
|
|
|
cur=""
|
2012-08-09 23:59:01 +00:00
|
|
|
fi
|
|
|
|
if [ "${prev}" = "=" ] && [[ "${pprev}" == --* ]]; then
|
|
|
|
prev="${pprev}"
|
|
|
|
fi
|
|
|
|
|
2016-03-01 15:37:18 +00:00
|
|
|
case "${prev}" in
|
2012-08-09 23:59:01 +00:00
|
|
|
-a|--accept)
|
2016-07-26 12:56:20 +00:00
|
|
|
COMPREPLY=($(compgen -W "$(_salt_get_keys un rej)" -- ${cur}))
|
2012-08-09 23:59:01 +00:00
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
-r|--reject)
|
2016-07-26 12:56:20 +00:00
|
|
|
COMPREPLY=($(compgen -W "$(_salt_get_keys acc)" -- ${cur}))
|
2012-08-09 23:59:01 +00:00
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
-d|--delete)
|
2016-07-26 12:56:20 +00:00
|
|
|
COMPREPLY=($(compgen -W "$(_salt_get_keys acc un rej)" -- ${cur}))
|
2012-08-09 23:59:01 +00:00
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
-c|--config)
|
|
|
|
COMPREPLY=($(compgen -f -- ${cur}))
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
--keysize)
|
|
|
|
COMPREPLY=($(compgen -W "2048 3072 4096 5120 6144" -- ${cur}))
|
|
|
|
return 0
|
|
|
|
;;
|
2016-03-01 15:37:18 +00:00
|
|
|
--gen-keys)
|
2012-08-09 23:59:01 +00:00
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
--gen-keys-dir)
|
|
|
|
COMPREPLY=($(compgen -d -- ${cur}))
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
-p|--print)
|
2016-07-26 12:56:20 +00:00
|
|
|
COMPREPLY=($(compgen -W "$(_salt_get_keys acc un rej)" -- ${cur}))
|
2012-08-09 23:59:01 +00:00
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
-l|--list)
|
|
|
|
COMPREPLY=($(compgen -W "pre un acc accepted unaccepted rej rejected all" -- ${cur}))
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
--accept-all)
|
2017-05-18 16:53:27 +00:00
|
|
|
return 0
|
2012-08-09 23:59:01 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
COMPREPLY=($(compgen -W "${opts} " -- ${cur}))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
complete -F _saltkey salt-key
|
|
|
|
|
|
|
|
_saltcall(){
|
|
|
|
local cur prev opts _salt_coms pprev ppprev
|
|
|
|
COMPREPLY=()
|
|
|
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
|
|
|
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
2012-12-07 00:42:09 +00:00
|
|
|
opts="-h --help -d --doc --documentation --version --versions-report \
|
|
|
|
-m --module-dirs= -g --grains --return= --local -c --config-dir= -l --log-level= \
|
|
|
|
--out=pprint --out=yaml --out=overstatestage --out=json --out=raw \
|
|
|
|
--out=highstate --out=key --out=txt --no-color --out-indent= "
|
2012-08-09 23:59:01 +00:00
|
|
|
if [ ${COMP_CWORD} -gt 2 ]; then
|
|
|
|
pprev="${COMP_WORDS[COMP_CWORD-2]}"
|
|
|
|
fi
|
|
|
|
if [ ${COMP_CWORD} -gt 3 ]; then
|
|
|
|
ppprev="${COMP_WORDS[COMP_CWORD-3]}"
|
|
|
|
fi
|
|
|
|
if [[ "${cur}" == -* ]] ; then
|
|
|
|
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
|
|
|
|
return 0
|
|
|
|
fi
|
2016-03-01 15:37:18 +00:00
|
|
|
|
2012-08-09 23:59:01 +00:00
|
|
|
if [ "${cur}" = "=" ] && [[ ${prev} == --* ]]; then
|
|
|
|
cur=""
|
|
|
|
fi
|
|
|
|
if [ "${prev}" = "=" ] && [[ ${pprev} == --* ]]; then
|
|
|
|
prev="${pprev}"
|
|
|
|
fi
|
2016-03-01 15:37:18 +00:00
|
|
|
|
2012-08-09 23:59:01 +00:00
|
|
|
case ${prev} in
|
|
|
|
-m|--module-dirs)
|
|
|
|
COMPREPLY=( $(compgen -d ${cur} ))
|
|
|
|
return 0
|
2017-05-18 16:53:27 +00:00
|
|
|
;;
|
|
|
|
-l|--log-level)
|
|
|
|
COMPREPLY=( $(compgen -W "info none garbage trace warning error debug" -- ${cur}))
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
-g|grains)
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
salt-call)
|
2012-08-09 23:59:01 +00:00
|
|
|
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
|
2017-05-18 16:53:27 +00:00
|
|
|
return 0
|
|
|
|
;;
|
2012-08-09 23:59:01 +00:00
|
|
|
esac
|
|
|
|
|
2012-12-06 09:02:26 +00:00
|
|
|
_salt_coms="$(salt-call --out=txt -- sys.list_functions|sed 's/^.*\[//' | tr -d ",']" )"
|
2012-08-09 23:59:01 +00:00
|
|
|
COMPREPLY=( $(compgen -W "${opts} ${_salt_coms}" -- ${cur} ))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
complete -F _saltcall salt-call
|
|
|
|
|
|
|
|
|
|
|
|
_saltcp(){
|
|
|
|
local cur prev opts target prefpart postpart helper filt pprev ppprev
|
|
|
|
COMPREPLY=()
|
|
|
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
|
|
|
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
2012-12-07 00:42:09 +00:00
|
|
|
opts="-t --timeout= -s --static -b --batch= --batch-size= \
|
|
|
|
-h --help --version --versions-report -c --config-dir= \
|
2016-03-01 15:37:18 +00:00
|
|
|
-E --pcre -L --list -G --grain --grain-pcre -N --nodegroup \
|
2014-05-29 00:32:54 +00:00
|
|
|
-R --range -C --compound -I --pillar \
|
2012-12-07 00:42:09 +00:00
|
|
|
--out=pprint --out=yaml --out=overstatestage --out=json --out=raw \
|
|
|
|
--out=highstate --out=key --out=txt --no-color --out-indent= "
|
2012-08-09 23:59:01 +00:00
|
|
|
if [[ "${cur}" == -* ]] ; then
|
|
|
|
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
|
|
|
|
return 0
|
|
|
|
fi
|
2016-03-01 15:37:18 +00:00
|
|
|
|
2012-08-09 23:59:01 +00:00
|
|
|
if [ "${cur}" = "=" ] && [[ "${prev}" == --* ]]; then
|
2016-03-01 15:37:18 +00:00
|
|
|
cur=""
|
2012-08-09 23:59:01 +00:00
|
|
|
fi
|
|
|
|
if [ "${prev}" = "=" ] && [[ "${pprev}" == --* ]]; then
|
|
|
|
prev=${pprev}
|
|
|
|
fi
|
2016-03-01 15:37:18 +00:00
|
|
|
|
2012-08-09 23:59:01 +00:00
|
|
|
case ${prev} in
|
2017-05-18 16:53:27 +00:00
|
|
|
salt-cp)
|
|
|
|
COMPREPLY=($(compgen -W "${opts} $(_salt_get_keys acc)" -- ${cur}))
|
|
|
|
return 0
|
|
|
|
;;
|
2012-08-09 23:59:01 +00:00
|
|
|
-t|--timeout)
|
2017-05-18 16:53:27 +00:00
|
|
|
# those numbers are just a hint
|
2012-08-09 23:59:01 +00:00
|
|
|
COMPREPLY=($(compgen -W "2 3 4 8 10 15 20 25 30 40 60 90 120 180 240 300" -- ${cur} ))
|
2017-05-18 16:53:27 +00:00
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
-E|--pcre)
|
2016-07-26 12:58:59 +00:00
|
|
|
COMPREPLY=($(compgen -W "$(_salt_get_keys acc)" -- ${cur}))
|
2012-08-09 23:59:01 +00:00
|
|
|
return 0
|
2017-05-18 16:53:27 +00:00
|
|
|
;;
|
|
|
|
-L|--list)
|
|
|
|
# IMPROVEMENTS ARE WELCOME
|
|
|
|
prefpart="${cur%,*},"
|
|
|
|
postpart=${cur##*,}
|
|
|
|
filt="^\($(echo ${cur}| sed 's:,:\\|:g')\)$"
|
2016-07-26 12:56:20 +00:00
|
|
|
helper=($(_salt_get_keys acc | grep -v "${filt}" | sed "s/^/${prefpart}/"))
|
2017-05-18 16:53:27 +00:00
|
|
|
COMPREPLY=($(compgen -W "${helper[*]}" -- ${cur}))
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
-G|--grain|--grain-pcre)
|
2016-03-01 15:37:18 +00:00
|
|
|
COMPREPLY=($(compgen -W "$(_salt_get_grains)" -- ${cur}))
|
2012-08-09 23:59:01 +00:00
|
|
|
return 0
|
2017-05-18 16:53:27 +00:00
|
|
|
;;
|
|
|
|
# FIXME
|
|
|
|
-R|--range)
|
|
|
|
# FIXME ??
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
-C|--compound)
|
|
|
|
# FIXME ??
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
-c|--config)
|
|
|
|
COMPREPLY=($(compgen -f -- ${cur}))
|
|
|
|
return 0
|
|
|
|
;;
|
2012-08-09 23:59:01 +00:00
|
|
|
esac
|
2016-03-01 15:37:18 +00:00
|
|
|
|
2012-08-09 23:59:01 +00:00
|
|
|
# default is using opts:
|
|
|
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
|
|
|
}
|
|
|
|
|
|
|
|
complete -F _saltcp salt-cp
|