[zsh] add more error reporting to zplg

This commit is contained in:
Dmytro Meleshko 2019-09-16 19:31:07 +03:00
parent 0e201ba4ea
commit 59fc6fee46

View file

@ -229,56 +229,56 @@ plugin() {
# parse basic arguments {{{ # parse basic arguments {{{
if (( $# < 2 )); then if (( $# < 2 )); then
_zplg_error "usage: $0 <id> <url> [option...]" _zplg_error "usage: $0 <id> <url> [option...]"
return 1 return 1
fi fi
local plugin_id="$1" local plugin_id="$1"
local plugin_url="$2" local plugin_url="$2"
if [[ ! "$plugin_id" =~ '^[a-zA-Z0-9_\-][a-zA-Z0-9._\-]*$' ]]; then if [[ ! "$plugin_id" =~ '^[a-zA-Z0-9_\-][a-zA-Z0-9._\-]*$' ]]; then
_zplg_error "invalid plugin ID" _zplg_error "invalid plugin ID"
return 1 return 1
fi fi
if [[ -z "$plugin_url" ]]; then if [[ -z "$plugin_url" ]]; then
_zplg_error "invalid plugin URL" _zplg_error "invalid plugin URL"
return 1 return 1
fi fi
# Don't even try to continue if the plugin has already been loaded. This is # Don't even try to continue if the plugin has already been loaded. This is
# not or problem. Plugin manager loads plugins and shouldn't bother # not or problem. Plugin manager loads plugins and shouldn't bother
# unloading them. # unloading them.
if _zplg_is_plugin_loaded "$plugin_id"; then if _zplg_is_plugin_loaded "$plugin_id"; then
_zplg_error "plugin $plugin_id has already been loaded" _zplg_error "plugin $plugin_id has already been loaded"
return 1 return 1
fi fi
# }}} # }}}
# parse options {{{ # parse options {{{
local plugin_from="$ZPLG_DEFAULT_SOURCE" local plugin_from="$ZPLG_DEFAULT_SOURCE"
local -a plugin_build plugin_before_load plugin_after_load plugin_load plugin_ignore local -a plugin_build plugin_before_load plugin_after_load plugin_load plugin_ignore
local option key value; shift 2; for option in "$@"; do local option key value; shift 2; for option in "$@"; do
# globs are faster than regular expressions # globs are faster than regular expressions
if [[ "$option" != *?=?* ]]; then if [[ "$option" != *?=?* ]]; then
_zplg_error "options must have the following format: <key>=<value>" _zplg_error "options must have the following format: <key>=<value>"
return 1 return 1
fi fi
# split 'option' at the first occurence of '=' # split 'option' at the first occurence of '='
key="${option%%=*}" value="${option#*=}" key="${option%%=*}" value="${option#*=}"
case "$key" in case "$key" in
from) from)
eval "plugin_$key=\"\$value\"" ;; eval "plugin_$key=\"\$value\"" ;;
build|before_load|after_load|load|ignore) build|before_load|after_load|load|ignore)
eval "plugin_$key+=(\"\$value\")" ;; eval "plugin_$key+=(\"\$value\")" ;;
*) *)
_zplg_error "unknown option: $key" _zplg_error "unknown option: $key"
return 1 ;; return 1 ;;
esac esac
done; unset option key value done; unset option key value
# }}} # }}}
@ -293,22 +293,24 @@ plugin() {
# download plugin {{{ # download plugin {{{
local plugin_dir="$_ZPLG_PLUGINS_DIR/$plugin_id" local plugin_dir="$_ZPLG_PLUGINS_DIR/$plugin_id"
# simple check whether the plugin directory exists is enough for me # simple check whether the plugin directory exists is enough for me
if [[ ! -d "$plugin_dir" ]]; then if [[ ! -d "$plugin_dir" ]]; then
_zplg_log "downloading $plugin_id" _zplg_log "downloading $plugin_id"
_zplg_source_"$plugin_from"_download "$plugin_url" "$plugin_dir" || return "$?" _zplg_source_"$plugin_from"_download "$plugin_url" "$plugin_dir" || return "$?"
if (( ${#plugin_build} > 0 )); then if (( ${#plugin_build} > 0 )); then
_zplg_log "building $plugin_id" _zplg_log "building $plugin_id"
( cd "$plugin_dir" && _zplg_run_commands plugin_build ) || return "$?" ( cd "$plugin_dir" && _zplg_run_commands plugin_build ) || return "$?"
fi
fi fi
fi
# }}} # }}}
# load plugin {{{ # load plugin {{{
{
_zplg_run_commands plugin_before_load || return "$?" _zplg_run_commands plugin_before_load || return "$?"
local load_pattern ignore_pattern script_path; local -a script_paths local load_pattern ignore_pattern script_path; local -a script_paths
@ -346,6 +348,12 @@ plugin() {
unset plugin_build_quoted unset plugin_build_quoted
fi fi
} always {
if [[ "$?" != 0 ]]; then
_zplg_error "an error occured while loading $plugin_id"
fi
}
# }}} # }}}
} }