shell-check fixes
parent
689758cf71
commit
f5fe3dbeb0
|
|
@ -19,7 +19,7 @@ _KNIFE_AUTOCOMPLETE_MAX_CACHE_AGE=86400
|
||||||
### init
|
### init
|
||||||
_KAC_CACHE_TMP_DIR="$_KNIFE_AUTOCOMPLETE_CACHE_DIR/tmp"
|
_KAC_CACHE_TMP_DIR="$_KNIFE_AUTOCOMPLETE_CACHE_DIR/tmp"
|
||||||
# make sure the cache dir exists
|
# make sure the cache dir exists
|
||||||
mkdir -p $_KAC_CACHE_TMP_DIR
|
mkdir -p "$_KAC_CACHE_TMP_DIR"
|
||||||
|
|
||||||
##############################
|
##############################
|
||||||
### Cache helper functions ###
|
### Cache helper functions ###
|
||||||
|
|
@ -33,7 +33,7 @@ stat -c %Y /dev/null > /dev/null 2>&1 && _KAC_STAT_COMMAND="stat -c %Y" || _KAC_
|
||||||
# returns 1 otherwise
|
# returns 1 otherwise
|
||||||
_KAC_is_file_newer_than() {
|
_KAC_is_file_newer_than() {
|
||||||
[ -f "$1" ] || return 1
|
[ -f "$1" ] || return 1
|
||||||
[ $(($(date +%s) - $($_KAC_STAT_COMMAND "$1"))) -gt $2 ] && return 1 || return 0
|
[ $(($(date +%s) - $($_KAC_STAT_COMMAND "$1"))) -gt "$2" ] && return 1 || return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
# helper function for _KAC_get_and_regen_cache, see doc below
|
# helper function for _KAC_get_and_regen_cache, see doc below
|
||||||
|
|
@ -42,10 +42,10 @@ _KAC_regen_cache() {
|
||||||
local CACHE_PATH="$_KNIFE_AUTOCOMPLETE_CACHE_DIR/$CACHE_NAME"
|
local CACHE_PATH="$_KNIFE_AUTOCOMPLETE_CACHE_DIR/$CACHE_NAME"
|
||||||
local TMP_FILE=$(mktemp "$_KAC_CACHE_TMP_DIR/$CACHE_NAME.XXXX")
|
local TMP_FILE=$(mktemp "$_KAC_CACHE_TMP_DIR/$CACHE_NAME.XXXX")
|
||||||
shift 1
|
shift 1
|
||||||
"$@" > $TMP_FILE 2> /dev/null
|
"$@" > "$TMP_FILE" 2> /dev/null
|
||||||
# discard the temp file if it's empty AND the previous command didn't exit successfully, but still mark the cache as updated
|
# discard the temp file if it's empty AND the previous command didn't exit successfully, but still mark the cache as updated
|
||||||
[[ $? != 0 ]] && [[ $(cat $TMP_FILE | wc -l) == 0 ]] && rm -f $TMP_FILE && touch $CACHE_PATH && return 1 \
|
[[ $? != 0 ]] && [[ $(wc -l "$TMP_FILE") == 0 ]] && rm -f "$TMP_FILE" && touch "$CACHE_PATH" && return 1 \
|
||||||
|| mv -f $TMP_FILE $CACHE_PATH
|
|| mv -f "$TMP_FILE" "$CACHE_PATH"
|
||||||
}
|
}
|
||||||
|
|
||||||
# cached files can't have spaces in their names
|
# cached files can't have spaces in their names
|
||||||
|
|
@ -65,10 +65,14 @@ _KAC_get_command_from_cache_name() {
|
||||||
_KAC_get_and_regen_cache() {
|
_KAC_get_and_regen_cache() {
|
||||||
# the cache name can't have space in it
|
# the cache name can't have space in it
|
||||||
local CACHE_NAME=$(_KAC_get_cache_name_from_command "$@")
|
local CACHE_NAME=$(_KAC_get_cache_name_from_command "$@")
|
||||||
local REGEN_CMD="_KAC_regen_cache $CACHE_NAME $@"
|
local REGEN_CMD="_KAC_regen_cache $CACHE_NAME $*"
|
||||||
_KAC_CACHE_PATH="$_KNIFE_AUTOCOMPLETE_CACHE_DIR/$CACHE_NAME"
|
_KAC_CACHE_PATH="$_KNIFE_AUTOCOMPLETE_CACHE_DIR/$CACHE_NAME"
|
||||||
# no need to wait for the regen if the file already exists
|
# no need to wait for the regen if the file already exists
|
||||||
[ -f $_KAC_CACHE_PATH ] && ($REGEN_CMD &) || $REGEN_CMD
|
if [[ -f "$_KAC_CACHE_PATH" ]]; then
|
||||||
|
($REGEN_CMD &)
|
||||||
|
else
|
||||||
|
$REGEN_CMD
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# performs two things: first, deletes all obsolete temp files
|
# performs two things: first, deletes all obsolete temp files
|
||||||
|
|
@ -76,14 +80,15 @@ _KAC_get_and_regen_cache() {
|
||||||
_KAC_clean_cache() {
|
_KAC_clean_cache() {
|
||||||
local FILE CMD
|
local FILE CMD
|
||||||
# delete all obsolete temp files, could be lingering there for any kind of crash in the caching process
|
# delete all obsolete temp files, could be lingering there for any kind of crash in the caching process
|
||||||
for FILE in $(ls $_KAC_CACHE_TMP_DIR); do
|
for FILE in $(\ls "$_KAC_CACHE_TMP_DIR"); do
|
||||||
_KAC_is_file_newer_than $FILE $_KNIFE_AUTOCOMPLETE_MAX_CACHE_AGE || rm -f $FILE
|
_KAC_is_file_newer_than "$FILE" "$_KNIFE_AUTOCOMPLETE_MAX_CACHE_AGE" || rm -f "$FILE"
|
||||||
done
|
done
|
||||||
# refresh really stale caches
|
# refresh really stale caches
|
||||||
for FILE in $(find $_KNIFE_AUTOCOMPLETE_CACHE_DIR -maxdepth 1 -type f -not -name '.*'); do
|
find "$_KNIFE_AUTOCOMPLETE_CACHE_DIR" -maxdepth 1 -type f -not -name '.*' \
|
||||||
_KAC_is_file_newer_than $FILE $_KNIFE_AUTOCOMPLETE_MAX_CACHE_AGE && continue
|
| while read -r FILE; do
|
||||||
|
_KAC_is_file_newer_than "$FILE" "$_KNIFE_AUTOCOMPLETE_MAX_CACHE_AGE" && continue
|
||||||
# first let's get the original command
|
# first let's get the original command
|
||||||
CMD=$(_KAC_get_command_from_cache_name $(basename "$FILE"))
|
CMD=$(_KAC_get_command_from_cache_name "$(basename "$FILE")")
|
||||||
# then regen the cache
|
# then regen the cache
|
||||||
_KAC_get_and_regen_cache "$CMD" > /dev/null
|
_KAC_get_and_regen_cache "$CMD" > /dev/null
|
||||||
done
|
done
|
||||||
|
|
@ -109,32 +114,32 @@ _KAC_get_current_base_command() {
|
||||||
local PREVIOUS="knife"
|
local PREVIOUS="knife"
|
||||||
local I=1
|
local I=1
|
||||||
local CURRENT
|
local CURRENT
|
||||||
while [ $I -le $COMP_CWORD ]; do
|
while [ $I -le "$COMP_CWORD" ]; do
|
||||||
# command words are all lower-case
|
# command words are all lower-case
|
||||||
echo ${COMP_WORDS[$I]} | grep -E "^[a-z]+$" > /dev/null || break
|
echo "${COMP_WORDS[$I]}" | grep -E "^[a-z]+$" > /dev/null || break
|
||||||
CURRENT="$PREVIOUS ${COMP_WORDS[$I]}"
|
CURRENT="$PREVIOUS ${COMP_WORDS[$I]}"
|
||||||
cat $_KAC_CACHE_PATH | grep -E "^$CURRENT" > /dev/null || break
|
grep -E "^$CURRENT" "$_KAC_CACHE_PATH" > /dev/null || break
|
||||||
PREVIOUS=$CURRENT
|
PREVIOUS=$CURRENT
|
||||||
I=$(($I + 1))
|
I=$((I + 1))
|
||||||
done
|
done
|
||||||
_KAC_CURRENT_COMMAND=$PREVIOUS
|
_KAC_CURRENT_COMMAND=$PREVIOUS
|
||||||
[ $I -le $COMP_CWORD ] && _KAC_CURRENT_COMMAND_NB_WORDS=$I
|
[ $I -le "$COMP_CWORD" ] && _KAC_CURRENT_COMMAND_NB_WORDS=$I
|
||||||
}
|
}
|
||||||
|
|
||||||
# searches the position of the currently completed argument in the current base command
|
# searches the position of the currently completed argument in the current base command
|
||||||
# (i.e. handles "plural" arguments such as knife cookbook upload cookbook1 cookbook2 and so on...)
|
# (i.e. handles "plural" arguments such as knife cookbook upload cookbook1 cookbook2 and so on...)
|
||||||
# assumes the current base command is complete
|
# assumes the current base command is complete
|
||||||
_KAC_get_current_arg_position() {
|
_KAC_get_current_arg_position() {
|
||||||
local CURRENT_ARG_POS=$(($_KAC_CURRENT_COMMAND_NB_WORDS + 1))
|
local CURRENT_ARG_POS=$((_KAC_CURRENT_COMMAND_NB_WORDS + 1))
|
||||||
local COMPLETE_COMMAND=$(cat $_KAC_CACHE_PATH | grep -E "^$_KAC_CURRENT_COMMAND")
|
local COMPLETE_COMMAND=$(grep -E "^$_KAC_CURRENT_COMMAND" "$_KAC_CACHE_PATH")
|
||||||
local CURRENT_ARG
|
local CURRENT_ARG
|
||||||
while [ $CURRENT_ARG_POS -le $COMP_CWORD ]; do
|
while [ "$CURRENT_ARG_POS" -le "$COMP_CWORD" ]; do
|
||||||
CURRENT_ARG=$(echo $COMPLETE_COMMAND | cut -d ' ' -f $CURRENT_ARG_POS)
|
CURRENT_ARG=$(echo "$COMPLETE_COMMAND" | cut -d ' ' -f "$CURRENT_ARG_POS")
|
||||||
# we break if the current arg is a "plural" arg
|
# we break if the current arg is a "plural" arg
|
||||||
echo $CURRENT_ARG | grep -E "^\\[[^]]+(\\.\\.\\.\\]|$)" > /dev/null && break
|
echo "$CURRENT_ARG" | grep -E "^\\[[^]]+(\\.\\.\\.\\]|$)" > /dev/null && break
|
||||||
CURRENT_ARG_POS=$(($CURRENT_ARG_POS + 1))
|
CURRENT_ARG_POS=$((CURRENT_ARG_POS + 1))
|
||||||
done
|
done
|
||||||
echo $CURRENT_ARG_POS
|
echo "$CURRENT_ARG_POS"
|
||||||
}
|
}
|
||||||
|
|
||||||
# the actual auto-complete function
|
# the actual auto-complete function
|
||||||
|
|
@ -143,8 +148,8 @@ _knife() {
|
||||||
local RAW_LIST ITEM REGEN_CMD ARG_POSITION
|
local RAW_LIST ITEM REGEN_CMD ARG_POSITION
|
||||||
COMREPLY=()
|
COMREPLY=()
|
||||||
# get correct command & arg pos
|
# get correct command & arg pos
|
||||||
_KAC_get_current_base_command && ARG_POSITION=$(_KAC_get_current_arg_position) || ARG_POSITION=$(($COMP_CWORD + 1))
|
_KAC_get_current_base_command && ARG_POSITION=$(_KAC_get_current_arg_position) || ARG_POSITION=$((COMP_CWORD + 1))
|
||||||
RAW_LIST=$(cat $_KAC_CACHE_PATH | grep -E "^$_KAC_CURRENT_COMMAND" | cut -d ' ' -f $ARG_POSITION | uniq)
|
RAW_LIST=$(grep -E "^$_KAC_CURRENT_COMMAND" "$_KAC_CACHE_PATH" | cut -d ' ' -f $ARG_POSITION | uniq)
|
||||||
|
|
||||||
# we need to process that raw list a bit, most notably for placeholders
|
# we need to process that raw list a bit, most notably for placeholders
|
||||||
# NOTE: I chose to explicitely fetch & cache _certain_ informations for the server (cookbooks & node names, etc)
|
# NOTE: I chose to explicitely fetch & cache _certain_ informations for the server (cookbooks & node names, etc)
|
||||||
|
|
@ -154,13 +159,13 @@ _knife() {
|
||||||
LIST=""
|
LIST=""
|
||||||
for ITEM in $RAW_LIST; do
|
for ITEM in $RAW_LIST; do
|
||||||
# always relevant if only lower-case chars : continuation of the base command
|
# always relevant if only lower-case chars : continuation of the base command
|
||||||
echo $ITEM | grep -E "^[a-z]+$" > /dev/null && LIST="$LIST $ITEM" && continue
|
echo "$ITEM" | grep -E "^[a-z]+$" > /dev/null && LIST="$LIST $ITEM" && continue
|
||||||
case $ITEM in
|
case "$ITEM" in
|
||||||
*COOKBOOK*)
|
*COOKBOOK*)
|
||||||
# special case for cookbooks : from site or local
|
# special case for cookbooks : from site or local
|
||||||
[[ ${COMP_WORDS[2]} == 'site' ]] && REGEN_CMD="knife cookbook site list" || REGEN_CMD="knife cookbook list"
|
[[ ${COMP_WORDS[2]} == 'site' ]] && REGEN_CMD="knife cookbook site list" || REGEN_CMD="knife cookbook list"
|
||||||
_KAC_get_and_regen_cache $REGEN_CMD
|
_KAC_get_and_regen_cache "$REGEN_CMD"
|
||||||
LIST="$LIST $(cat $_KAC_CACHE_PATH | cut -d ' ' -f 1)"
|
LIST="$LIST $(cut -d ' ' -f 1 < "$_KAC_CACHE_PATH")"
|
||||||
continue
|
continue
|
||||||
;;
|
;;
|
||||||
*ITEM*)
|
*ITEM*)
|
||||||
|
|
@ -182,8 +187,8 @@ _knife() {
|
||||||
# not a generic argument we support...
|
# not a generic argument we support...
|
||||||
*) continue ;;
|
*) continue ;;
|
||||||
esac
|
esac
|
||||||
_KAC_get_and_regen_cache $REGEN_CMD
|
_KAC_get_and_regen_cache "$REGEN_CMD"
|
||||||
LIST="$LIST $(cat $_KAC_CACHE_PATH)"
|
LIST="$LIST $(cat "$_KAC_CACHE_PATH")"
|
||||||
done
|
done
|
||||||
COMPREPLY=($(compgen -W "${LIST}" -- ${COMP_WORDS[COMP_CWORD]}))
|
COMPREPLY=($(compgen -W "${LIST}" -- ${COMP_WORDS[COMP_CWORD]}))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue