Merge pull request #774 from simnalamburt/mssh-errorhandling
_mssh: Handle error cases more gracefully
This commit is contained in:
commit
9dfd5c6670
33
src/_mssh
33
src/_mssh
|
@ -39,6 +39,11 @@
|
||||||
#
|
#
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# Do nothing if there's no AWS CLI
|
||||||
|
if (( ! $+commands[aws] )); then
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
# Define function only when it doesn't exist
|
# Define function only when it doesn't exist
|
||||||
(( $+functions[_mssh_cache_policy] )) || _mssh_cache_policy() {
|
(( $+functions[_mssh_cache_policy] )) || _mssh_cache_policy() {
|
||||||
# Cache invalidates after 30 seconds
|
# Cache invalidates after 30 seconds
|
||||||
|
@ -66,14 +71,34 @@ fi
|
||||||
|
|
||||||
local -a instances
|
local -a instances
|
||||||
if _cache_invalid mssh_instances || ! _retrieve_cache mssh_instances; then
|
if _cache_invalid mssh_instances || ! _retrieve_cache mssh_instances; then
|
||||||
# Cache is invalid or caching feature is disabled
|
# Cache is invalid or caching retrieval failed (usually due to disabled cache)
|
||||||
IFS=$'\n\t' instances=($(
|
|
||||||
|
# Store the output of the AWS CLI separately
|
||||||
|
#
|
||||||
|
# Reference:
|
||||||
|
# https://unix.stackexchange.com/a/430182
|
||||||
|
local stderr
|
||||||
|
local -i exit_code
|
||||||
|
() {
|
||||||
aws ec2 describe-instances \
|
aws ec2 describe-instances \
|
||||||
--query 'Reservations[].Instances[] | [?State.Name == `running`].join(`:`, [InstanceId, Tags[?Key == `Name`].Value | [0]])' \
|
--query 'Reservations[].Instances[] | [?State.Name == `running`].join(`:`, [InstanceId, Tags[?Key == `Name`].Value | [0]])' \
|
||||||
--output text
|
--output text \
|
||||||
))
|
>${1} 2>${2}
|
||||||
|
exit_code=${?}
|
||||||
|
IFS=$'\n\t' instances=($(<${1}))
|
||||||
|
stderr=$(<${2})
|
||||||
|
} =(:) =(:)
|
||||||
|
|
||||||
|
if (( $exit_code == 0 )); then
|
||||||
|
# AWS CLI successfully executed
|
||||||
_store_cache mssh_instances instances
|
_store_cache mssh_instances instances
|
||||||
|
else
|
||||||
|
# AWS CLI failed, abort autocompletion
|
||||||
|
_message -r "\
|
||||||
|
Failed autocomplete due to following reason:
|
||||||
|
${stderr}"
|
||||||
|
return
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
_describe 'command' instances
|
_describe 'command' instances
|
||||||
|
|
Loading…
Reference in New Issue