pre-commit fixes.
parent
889259dfec
commit
689758cf71
|
|
@ -31,15 +31,13 @@ stat -c %Y /dev/null > /dev/null 2>&1 && _KAC_STAT_COMMAND="stat -c %Y" || _KAC_
|
||||||
# returns 0 iff the file whose path is given as 1st argument
|
# returns 0 iff the file whose path is given as 1st argument
|
||||||
# exists and has last been modified in the last $2 seconds
|
# exists and has last been modified in the last $2 seconds
|
||||||
# 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
|
||||||
_KAC_regen_cache()
|
_KAC_regen_cache() {
|
||||||
{
|
|
||||||
local CACHE_NAME=$1
|
local CACHE_NAME=$1
|
||||||
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")
|
||||||
|
|
@ -51,14 +49,12 @@ _KAC_regen_cache()
|
||||||
}
|
}
|
||||||
|
|
||||||
# cached files can't have spaces in their names
|
# cached files can't have spaces in their names
|
||||||
_KAC_get_cache_name_from_command()
|
_KAC_get_cache_name_from_command() {
|
||||||
{
|
|
||||||
echo "$@" | sed 's/ /_SPACE_/g'
|
echo "$@" | sed 's/ /_SPACE_/g'
|
||||||
}
|
}
|
||||||
|
|
||||||
# the reverse operation from the function above
|
# the reverse operation from the function above
|
||||||
_KAC_get_command_from_cache_name()
|
_KAC_get_command_from_cache_name() {
|
||||||
{
|
|
||||||
echo "$@" | sed 's/_SPACE_/ /g'
|
echo "$@" | sed 's/_SPACE_/ /g'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -66,8 +62,7 @@ _KAC_get_command_from_cache_name()
|
||||||
# otherwise it waits for the cache to be generated
|
# otherwise it waits for the cache to be generated
|
||||||
# in either case, it regenerates the cache, and sets the _KAC_CACHE_PATH env variable
|
# in either case, it regenerates the cache, and sets the _KAC_CACHE_PATH env variable
|
||||||
# for obvious reason, do NOT call that in a sub-shell (in particular, no piping)
|
# for obvious reason, do NOT call that in a sub-shell (in particular, no piping)
|
||||||
_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 $@"
|
||||||
|
|
@ -78,17 +73,14 @@ _KAC_get_and_regen_cache()
|
||||||
|
|
||||||
# performs two things: first, deletes all obsolete temp files
|
# performs two things: first, deletes all obsolete temp files
|
||||||
# then refreshes stale caches that haven't been called in a long time
|
# then refreshes stale caches that haven't been called in a long time
|
||||||
_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)
|
for FILE in $(ls $_KAC_CACHE_TMP_DIR); do
|
||||||
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 '.*')
|
for FILE in $(find $_KNIFE_AUTOCOMPLETE_CACHE_DIR -maxdepth 1 -type f -not -name '.*'); do
|
||||||
do
|
|
||||||
_KAC_is_file_newer_than $FILE $_KNIFE_AUTOCOMPLETE_MAX_CACHE_AGE && continue
|
_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"))
|
||||||
|
|
@ -105,23 +97,19 @@ _KAC_clean_cache()
|
||||||
### End of cache helper functions ###
|
### End of cache helper functions ###
|
||||||
#####################################
|
#####################################
|
||||||
|
|
||||||
|
|
||||||
# returns all the possible knife sub-commands
|
# returns all the possible knife sub-commands
|
||||||
_KAC_knife_commands()
|
_KAC_knife_commands() {
|
||||||
{
|
|
||||||
knife --help | grep -E "^knife" | sed -E 's/ \(options\)//g'
|
knife --help | grep -E "^knife" | sed -E 's/ \(options\)//g'
|
||||||
}
|
}
|
||||||
|
|
||||||
# rebuilds the knife base command currently being completed, and assigns it to $_KAC_CURRENT_COMMAND
|
# rebuilds the knife base command currently being completed, and assigns it to $_KAC_CURRENT_COMMAND
|
||||||
# additionnally, returns 1 iff the current base command is not complete, 0 otherwise
|
# additionnally, returns 1 iff the current base command is not complete, 0 otherwise
|
||||||
# also sets $_KAC_CURRENT_COMMAND_NB_WORDS if the base command is complete
|
# also sets $_KAC_CURRENT_COMMAND_NB_WORDS if the base command is complete
|
||||||
_KAC_get_current_base_command()
|
_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 ]
|
while [ $I -le $COMP_CWORD ]; do
|
||||||
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]}"
|
||||||
|
|
@ -136,13 +124,11 @@ _KAC_get_current_base_command()
|
||||||
# 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=$(cat $_KAC_CACHE_PATH | grep -E "^$_KAC_CURRENT_COMMAND")
|
||||||
local CURRENT_ARG
|
local CURRENT_ARG
|
||||||
while [ $CURRENT_ARG_POS -le $COMP_CWORD ]
|
while [ $CURRENT_ARG_POS -le $COMP_CWORD ]; do
|
||||||
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
|
||||||
|
|
@ -152,8 +138,7 @@ _KAC_get_current_arg_position()
|
||||||
}
|
}
|
||||||
|
|
||||||
# the actual auto-complete function
|
# the actual auto-complete function
|
||||||
_knife()
|
_knife() {
|
||||||
{
|
|
||||||
_KAC_get_and_regen_cache _KAC_knife_commands
|
_KAC_get_and_regen_cache _KAC_knife_commands
|
||||||
local RAW_LIST ITEM REGEN_CMD ARG_POSITION
|
local RAW_LIST ITEM REGEN_CMD ARG_POSITION
|
||||||
COMREPLY=()
|
COMREPLY=()
|
||||||
|
|
@ -167,8 +152,7 @@ _knife()
|
||||||
# current base command - that might limit my script in some situation, but that way I'm sure it caches only
|
# current base command - that might limit my script in some situation, but that way I'm sure it caches only
|
||||||
# not-sensitive stuff (a generic approach could be pretty bad e.g. with the knife-rackspace plugin)
|
# not-sensitive stuff (a generic approach could be pretty bad e.g. with the knife-rackspace plugin)
|
||||||
LIST=""
|
LIST=""
|
||||||
for ITEM in $RAW_LIST
|
for ITEM in $RAW_LIST; do
|
||||||
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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue