completions/salt: Fix quoting for "compgen -W" and others

This commit is contained in:
Koichi Murase
2021-12-28 19:59:28 +09:00
parent eaa147afa6
commit 2027dd42a4

View File

@@ -30,15 +30,15 @@ _salt_get_grain_values(){
_salt(){ _salt(){
local cur prev opts _salt_grains _salt_coms pprev ppprev local cur prev opts pprev ppprev
COMPREPLY=() COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}" cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}" prev="${COMP_WORDS[COMP_CWORD-1]}"
if [ ${COMP_CWORD} -gt 2 ]; then if [ ${COMP_CWORD} -gt 2 ]; then
pprev="${COMP_WORDS[COMP_CWORD-2]}" pprev="${COMP_WORDS[COMP_CWORD-2]}"
fi fi
if [ ${COMP_CWORD} -gt 3 ]; then if [ ${COMP_CWORD} -gt 3 ]; then
ppprev="${COMP_WORDS[COMP_CWORD-3]}" ppprev="${COMP_WORDS[COMP_CWORD-3]}"
fi fi
opts="-h --help -d --doc --documentation --version --versions-report -c \ opts="-h --help -d --doc --documentation --version --versions-report -c \
@@ -50,7 +50,7 @@ _salt(){
--out=raw --out=highstate --out=key --out=txt --no-color --out-indent= " --out=raw --out=highstate --out=key --out=txt --no-color --out-indent= "
if [[ "${cur}" == -* ]] ; then if [[ "${cur}" == -* ]] ; then
COMPREPLY=($(compgen -W "${opts}" -- ${cur})) COMPREPLY=($(compgen -W '$opts' -- "$cur"))
return 0 return 0
fi fi
@@ -58,7 +58,7 @@ _salt(){
case "${pprev}" in case "${pprev}" in
-G|--grain|--grain-pcre) -G|--grain|--grain-pcre)
if [ "${cur}" = ":" ]; then if [ "${cur}" = ":" ]; then
COMPREPLY=($(compgen -W "`_salt_get_grain_values ${prev}`" )) COMPREPLY=($(compgen -W '$(_salt_get_grain_values "$prev")'))
return 0 return 0
fi fi
;; ;;
@@ -66,7 +66,7 @@ _salt(){
case "${ppprev}" in case "${ppprev}" in
-G|--grain|--grain-pcre) -G|--grain|--grain-pcre)
if [ "${prev}" = ":" ]; then if [ "${prev}" = ":" ]; then
COMPREPLY=( $(compgen -W "`_salt_get_grain_values ${pprev}`" -- ${cur}) ) COMPREPLY=($(compgen -W '$(_salt_get_grain_values "$pprev")' -- "$cur"))
return 0 return 0
fi fi
;; ;;
@@ -82,27 +82,27 @@ _salt(){
case "${prev}" in case "${prev}" in
-c|--config) -c|--config)
COMPREPLY=($(compgen -f -- ${cur})) COMPREPLY=($(compgen -f -- "$cur"))
return 0 return 0
;; ;;
salt) salt)
COMPREPLY=($(compgen -W "\'*\' ${opts} `salt-key --no-color -l acc`" -- ${cur})) COMPREPLY=($(compgen -W "\'*\' \$opts \$(salt-key --no-color -l acc)" -- "$cur"))
return 0 return 0
;; ;;
-E|--pcre) -E|--pcre)
COMPREPLY=($(compgen -W "`salt-key --no-color -l acc`" -- ${cur})) COMPREPLY=($(compgen -W '$(salt-key --no-color -l acc)' -- "$cur"))
return 0 return 0
;; ;;
-G|--grain|--grain-pcre) -G|--grain|--grain-pcre)
COMPREPLY=($(compgen -W "$(_salt_get_grains)" -- ${cur})) COMPREPLY=($(compgen -W '$(_salt_get_grains)' -- "$cur"))
return 0 return 0
;; ;;
-C|--compound) -C|--compound)
COMPREPLY=() # TODO: finish this one? how? COMPREPLY=() # TODO: finish this one? how?
return 0 return 0
;; ;;
-t|--timeout) -t|--timeout)
COMPREPLY=($( compgen -W "1 2 3 4 5 6 7 8 9 10 15 20 30 40 60 90 120 180" -- ${cur})) COMPREPLY=($(compgen -W "1 2 3 4 5 6 7 8 9 10 15 20 30 40 60 90 120 180" -- "$cur"))
return 0 return 0
;; ;;
-b|--batch|--batch-size) -b|--batch|--batch-size)
@@ -110,15 +110,16 @@ _salt(){
return 0 return 0
;; ;;
-N|--nodegroup) -N|--nodegroup)
MASTER_CONFIG='/etc/salt/master' local MASTER_CONFIG='/etc/salt/master'
COMPREPLY=($(compgen -W "`awk -F ':' 'BEGIN {print_line = 0}; /^nodegroups/ {print_line = 1;getline } print_line && /^ */ {print $1} /^[^ ]/ {print_line = 0}' <${MASTER_CONFIG}`" -- ${cur})) local all=$(awk -F ':' 'BEGIN {print_line = 0}; /^nodegroups/ {print_line = 1;getline } print_line && /^ */ {print $1} /^[^ ]/ {print_line = 0}' < "$MASTER_CONFIG")
COMPREPLY=($(compgen -W '$all' -- "$cur"))
return 0 return 0
;; ;;
esac esac
_salt_coms="$(salt '*' --timeout 2 --out=txt -- sys.list_functions | sed 's/^.*\[//' | tr -d ",']" )" local _salt_coms=$(salt '*' --timeout 2 --out=txt -- sys.list_functions | sed 's/^.*\[//' | tr -d ",']" )
all="${opts} ${_salt_coms}" local all="${opts} ${_salt_coms}"
COMPREPLY=( $(compgen -W "${all}" -- ${cur}) ) COMPREPLY=($(compgen -W '$all' -- "$cur"))
return 0 return 0
} }
@@ -145,7 +146,7 @@ _saltkey(){
ppprev="${COMP_WORDS[COMP_CWORD-3]}" ppprev="${COMP_WORDS[COMP_CWORD-3]}"
fi fi
if [[ "${cur}" == -* ]] ; then if [[ "${cur}" == -* ]] ; then
COMPREPLY=($(compgen -W "${opts}" -- ${cur})) COMPREPLY=($(compgen -W '$opts' -- "$cur"))
return 0 return 0
fi fi
@@ -158,52 +159,52 @@ _saltkey(){
case "${prev}" in case "${prev}" in
-a|--accept) -a|--accept)
COMPREPLY=($(compgen -W "$(salt-key -l un --no-color; salt-key -l rej --no-color)" -- ${cur})) COMPREPLY=($(compgen -W '$(salt-key -l un --no-color; salt-key -l rej --no-color)' -- "$cur"))
return 0 return 0
;; ;;
-r|--reject) -r|--reject)
COMPREPLY=($(compgen -W "$(salt-key -l acc --no-color)" -- ${cur})) COMPREPLY=($(compgen -W '$(salt-key -l acc --no-color)' -- "$cur"))
return 0 return 0
;; ;;
-d|--delete) -d|--delete)
COMPREPLY=($(compgen -W "$(salt-key -l acc --no-color; salt-key -l un --no-color; salt-key -l rej --no-color)" -- ${cur})) COMPREPLY=($(compgen -W '$(salt-key -l acc --no-color; salt-key -l un --no-color; salt-key -l rej --no-color)' -- "$cur"))
return 0 return 0
;; ;;
-c|--config) -c|--config)
COMPREPLY=($(compgen -f -- ${cur})) COMPREPLY=($(compgen -f -- "$cur"))
return 0 return 0
;; ;;
--keysize) --keysize)
COMPREPLY=($(compgen -W "2048 3072 4096 5120 6144" -- ${cur})) COMPREPLY=($(compgen -W "2048 3072 4096 5120 6144" -- "$cur"))
return 0 return 0
;; ;;
--gen-keys) --gen-keys)
return 0 return 0
;; ;;
--gen-keys-dir) --gen-keys-dir)
COMPREPLY=($(compgen -d -- ${cur})) COMPREPLY=($(compgen -d -- "$cur"))
return 0 return 0
;; ;;
-p|--print) -p|--print)
COMPREPLY=($(compgen -W "$(salt-key -l acc --no-color; salt-key -l un --no-color; salt-key -l rej --no-color)" -- ${cur})) COMPREPLY=($(compgen -W '$(salt-key -l acc --no-color; salt-key -l un --no-color; salt-key -l rej --no-color)' -- "$cur"))
return 0 return 0
;; ;;
-l|--list) -l|--list)
COMPREPLY=($(compgen -W "pre un acc accepted unaccepted rej rejected all" -- ${cur})) COMPREPLY=($(compgen -W "pre un acc accepted unaccepted rej rejected all" -- "$cur"))
return 0 return 0
;; ;;
--accept-all) --accept-all)
return 0 return 0
;; ;;
esac esac
COMPREPLY=($(compgen -W "${opts} " -- ${cur})) COMPREPLY=($(compgen -W '$opts' -- "$cur"))
return 0 return 0
} }
complete -F _saltkey salt-key complete -F _saltkey salt-key
_saltcall(){ _saltcall(){
local cur prev opts _salt_coms pprev ppprev local cur prev opts pprev ppprev
COMPREPLY=() COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}" cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}" prev="${COMP_WORDS[COMP_CWORD-1]}"
@@ -218,7 +219,7 @@ _saltcall(){
ppprev="${COMP_WORDS[COMP_CWORD-3]}" ppprev="${COMP_WORDS[COMP_CWORD-3]}"
fi fi
if [[ "${cur}" == -* ]] ; then if [[ "${cur}" == -* ]] ; then
COMPREPLY=($(compgen -W "${opts}" -- ${cur})) COMPREPLY=($(compgen -W '$opts' -- "$cur"))
return 0 return 0
fi fi
@@ -231,24 +232,24 @@ _saltcall(){
case ${prev} in case ${prev} in
-m|--module-dirs) -m|--module-dirs)
COMPREPLY=( $(compgen -d ${cur} )) COMPREPLY=($(compgen -d -- "$cur"))
return 0
;;
-l|--log-level)
COMPREPLY=( $(compgen -W "info none garbage trace warning error debug" -- ${cur}))
return 0
;;
-g|grains)
return 0 return 0
;; ;;
salt-call) -l|--log-level)
COMPREPLY=($(compgen -W "${opts}" -- ${cur})) COMPREPLY=($(compgen -W "info none garbage trace warning error debug" -- "$cur"))
return 0 return 0
;; ;;
-g|grains)
return 0
;;
salt-call)
COMPREPLY=($(compgen -W '$opts' -- "$cur"))
return 0
;;
esac esac
_salt_coms="$(salt-call --out=txt -- sys.list_functions|sed 's/^.*\[//' | tr -d ",']" )" local _salt_coms=$(salt-call --out=txt -- sys.list_functions|sed 's/^.*\[//' | tr -d ",']")
COMPREPLY=( $(compgen -W "${opts} ${_salt_coms}" -- ${cur} )) COMPREPLY=($(compgen -W '$opts $_salt_coms' -- "$cur"))
return 0 return 0
} }
@@ -267,7 +268,7 @@ _saltcp(){
--out=pprint --out=yaml --out=overstatestage --out=json --out=raw \ --out=pprint --out=yaml --out=overstatestage --out=json --out=raw \
--out=highstate --out=key --out=txt --no-color --out-indent= " --out=highstate --out=key --out=txt --no-color --out-indent= "
if [[ "${cur}" == -* ]] ; then if [[ "${cur}" == -* ]] ; then
COMPREPLY=($(compgen -W "${opts}" -- ${cur})) COMPREPLY=($(compgen -W '$opts' -- "$cur"))
return 0 return 0
fi fi
@@ -279,51 +280,50 @@ _saltcp(){
fi fi
case ${prev} in case ${prev} in
salt-cp) salt-cp)
COMPREPLY=($(compgen -W "${opts} `salt-key -l acc --no-color`" -- ${cur})) COMPREPLY=($(compgen -W '$opts $(salt-key -l acc --no-color)' -- "$cur"))
return 0 return 0
;;
-t|--timeout)
# those numbers are just a hint
COMPREPLY=($(compgen -W "2 3 4 8 10 15 20 25 30 40 60 90 120 180 240 300" -- ${cur} ))
return 0
;; ;;
-E|--pcre) -t|--timeout)
COMPREPLY=($(compgen -W "`salt-key -l acc --no-color`" -- ${cur})) # those numbers are just a hint
COMPREPLY=($(compgen -W "2 3 4 8 10 15 20 25 30 40 60 90 120 180 240 300" -- "$cur"))
return 0 return 0
;; ;;
-L|--list) -E|--pcre)
# IMPROVEMENTS ARE WELCOME COMPREPLY=($(compgen -W '$(salt-key -l acc --no-color)' -- "$cur"))
prefpart="${cur%,*}," return 0
postpart=${cur##*,} ;;
filt="^\($(echo ${cur}| sed 's:,:\\|:g')\)$" -L|--list)
# IMPROVEMENTS ARE WELCOME
prefpart="${cur%,*},"
postpart=${cur##*,}
filt="^\($(echo ${cur}| sed 's:,:\\|:g')\)$"
helper=($(salt-key -l acc --no-color | grep -v "${filt}" | sed "s/^/${prefpart}/")) helper=($(salt-key -l acc --no-color | grep -v "${filt}" | sed "s/^/${prefpart}/"))
COMPREPLY=($(compgen -W "${helper[*]}" -- ${cur})) COMPREPLY=($(compgen -W '"${helper[@]}"' -- "$cur"))
return 0
;;
-G|--grain|--grain-pcre)
COMPREPLY=($(compgen -W "$(_salt_get_grains)" -- ${cur}))
return 0 return 0
;; ;;
# FIXME -G|--grain|--grain-pcre)
-R|--range) COMPREPLY=($(compgen -W '$(_salt_get_grains)' -- "$cur"))
# FIXME ?? return 0
return 0 ;;
;; # FIXME
-C|--compound) -R|--range)
# FIXME ?? # FIXME ??
return 0 return 0
;; ;;
-c|--config) -C|--compound)
COMPREPLY=($(compgen -f -- ${cur})) # FIXME ??
return 0 return 0
;; ;;
-c|--config)
COMPREPLY=($(compgen -f -- "$cur"))
return 0
;;
esac esac
# default is using opts: # default is using opts:
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) COMPREPLY=($(compgen -W '$opts' -- "$cur"))
} }
complete -F _saltcp salt-cp complete -F _saltcp salt-cp