Merge pull request #47261 from shtrom/bash-completion-log-level-and-function-cache

Bash completion log level and function cache
This commit is contained in:
Nicole Thomas 2018-05-24 12:37:11 -04:00 committed by GitHub
commit 2019cc8960
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 23 deletions

View File

@ -13,17 +13,17 @@
_salt_get_grains(){
if [ "$1" = 'local' ] ; then
salt-call --out=txt -- grains.ls | sed 's/^.*\[//' | tr -d ",']" |sed 's:\([a-z0-9]\) :\1\: :g'
salt-call --log-level=error --out=txt -- grains.ls | sed 's/^.*\[//' | tr -d ",']" |sed 's:\([a-z0-9]\) :\1\: :g'
else
salt '*' --timeout 2 --hide-timeout --out=txt -- grains.ls | sed 's/^.*\[//' | tr -d ",']" |sed 's:\([a-z0-9]\) :\1\: :g'
salt '*' --timeout 2 --hide-timeout --log-level=error --out=txt -- grains.ls | sed 's/^.*\[//' | tr -d ",']" |sed 's:\([a-z0-9]\) :\1\: :g'
fi
}
_salt_get_grain_values(){
if [ "$1" = 'local' ] ; then
salt-call --out=txt -- grains.item $1 |sed 's/^\S*:\s//' |grep -v '^\s*$'
salt-call --log-level=error --out=txt -- grains.item $1 |sed 's/^\S*:\s//' |grep -v '^\s*$'
else
salt '*' --timeout 2 --hide-timeout --out=txt -- grains.item $1 |sed 's/^\S*:\s//' |grep -v '^\s*$'
salt '*' --timeout 2 --hide-timeout --log-level=error --out=txt -- grains.item $1 |sed 's/^\S*:\s//' |grep -v '^\s*$'
fi
}
@ -34,8 +34,24 @@ _salt_get_keys(){
done
}
_salt(){
CACHE_DIR="$HOME/.cache/salt-comp-cache_functions"
_salt_list_functions(){
# salt-call: get all functions on this minion
# 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
if [ "$1" = 'local' ] ; then
salt-call --log-level=quiet --out=txt -- sys.list_functions \
| sed "s/^.*\[//;s/[],']//g;s/ /\n/g" \
| sort -u
else
salt '*' --timeout 2 --hide-timeout --log-level=quiet --out=txt -- sys.list_functions \
| sed "s/^.*\[//;s/[],']//g;s/ /\n/g" \
| sort -u
fi
}
_salt_get_coms() {
CACHE_DIR="$HOME/.cache/salt-${1}-comp-cache_functions"
local _salt_cache_functions=${SALT_COMP_CACHE_FUNCTIONS:=$CACHE_DIR}
local _salt_cache_timeout=${SALT_COMP_CACHE_TIMEOUT:='last hour'}
@ -43,6 +59,17 @@ _salt(){
mkdir -p "$(dirname ${_salt_cache_functions})"
fi
# Regenerate cache if timed out
if [[ "$(stat --format=%Z ${_salt_cache_functions} 2>/dev/null)" -lt "$(date --date="${_salt_cache_timeout}" +%s)" ]]; then
_salt_list_functions $1 > "${_salt_cache_functions}"
fi
# filter results, to only print the part to next dot (or end of function)
sed 's/^\('${cur}'\(\.\|[^.]*\)\)\?.*/\1/' "${_salt_cache_functions}" | sort -u
}
_salt(){
local cur prev opts _salt_grains _salt_coms pprev ppprev
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
@ -129,22 +156,10 @@ _salt(){
;;
esac
# 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
# 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)"
_salt_coms=$(_salt_get_coms remote)
# If there are still dots in the suggestion, do not append space
grep "^${cur}.*\." "${_salt_cache_functions}" &>/dev/null && compopt -o nospace
grep "^${cur}.*\." "${_salt_coms}" &>/dev/null && compopt -o nospace
all="${opts} ${_salt_coms}"
COMPREPLY=( $(compgen -W "${all}" -- ${cur}) )
@ -276,7 +291,11 @@ _saltcall(){
;;
esac
_salt_coms="$(salt-call --out=txt -- sys.list_functions|sed 's/^.*\[//' | tr -d ",']" )"
_salt_coms=$(_salt_get_coms local)
# If there are still dots in the suggestion, do not append space
grep "^${cur}.*\." "${_salt_coms}" &>/dev/null && compopt -o nospace
COMPREPLY=( $(compgen -W "${opts} ${_salt_coms}" -- ${cur} ))
return 0
}

View File

@ -25,7 +25,7 @@ _modules(){
fi
if _cache_invalid salt/modules || ! _retrieve_cache salt/modules; then
_funcs=( ${${(Q)${${(s. .)"$(_call_program salt-call-cmd salt-call --local --out txt sys.list_functions)"}%%[],]##}#\[}:#local:} )
_funcs=( ${${(Q)${${(s. .)"$(_call_program salt-call-cmd salt-call --local --log-level error --out txt sys.list_functions)"}%%[],]##}#\[}:#local:} )
_store_cache salt/modules _funcs
fi
@ -40,7 +40,7 @@ _runners(){
fi
if _cache_invalid salt/runners || ! _retrieve_cache salt/runners; then
_runs=( ${${(Q)${${(s. .)"$(_call_program salt-call-cmd salt-call --local --out txt sys.list_runner_functions)"}%%[],]##}#\[}:#local:} )
_runs=( ${${(Q)${${(s. .)"$(_call_program salt-call-cmd salt-call --local --log-level error --out txt sys.list_runner_functions)"}%%[],]##}#\[}:#local:} )
_store_cache salt/runners _runs
fi