diff --git a/container/src/finance/lib/internal/lib_plugins.bash b/container/src/finance/lib/internal/lib_plugins.bash index 9fe3c52..e15994f 100644 --- a/container/src/finance/lib/internal/lib_plugins.bash +++ b/container/src/finance/lib/internal/lib_plugins.bash @@ -30,7 +30,7 @@ source "${DOCKER_FINANCE_CONTAINER_REPO}/src/finance/lib/internal/lib_utils.bash function lib_plugins::plugins() { - # NOTE: no args to parse, pass directly to plugin + lib_plugins::__parse_args "$@" lib_plugins::__plugins "$@" lib_utils::catch $? } @@ -39,34 +39,79 @@ function lib_plugins::plugins() # Implementation # -function lib_plugins::__plugins() +function lib_plugins::__parse_args() { - [ -z "$global_usage" ] && lib_utils::die_fatal + # + # Expected format: + # + # $global_usage repo${global_arg_delim_1}file [args] + # - Executes plugin in repository + # + # $global_usage custom${global_arg_delim_1}file [args] + # - Executes plugin outside of repository + # - [ -z "$DOCKER_FINANCE_CONTAINER_FLOW" ] && lib_utils::die_fatal - local -r _path="${DOCKER_FINANCE_CONTAINER_FLOW}/plugins" + [ -z "$global_usage" ] && lib_utils::die_fatal + [ -z "$global_arg_delim_1" ] && lib_utils::die_fatal + + [ -z "$DOCKER_FINANCE_CONTAINER_REPO" ] && lib_utils::die_fatal + local -r _repo="${DOCKER_FINANCE_CONTAINER_REPO}/plugins/finance" + [ ! -d "$_repo" ] && lib_utils::die_fatal + + [ -z "$DOCKER_FINANCE_CONTAINER_PLUGINS" ] && lib_utils::die_fatal + local -r _custom="${DOCKER_FINANCE_CONTAINER_PLUGINS}/finance" + [ ! -d "$_custom" ] && lib_utils::die_fatal local -r _usage=" \e[32mDescription:\e[0m - Execute a plugin within the flow plugins path + Execute a categorical plugin \e[32mUsage:\e[0m - $ $global_usage [args] + $ $global_usage [args] \e[32mExamples:\e[0m - \e[37;2m# Execute a custom plugin in ${_path}\e[0m - $ $global_usage example.bash \"hello\" + \e[37;2m# Execute a repository plugin in '${_repo}'\e[0m + $ $global_usage repo${global_arg_delim_1}example.bash \"I'm in repo\" + + \e[37;2m# Execute a custom plugin in '${_custom}'\e[0m + $ $global_usage custom${global_arg_delim_1}example.bash \"I'm in custom\" " - if [ "$#" -eq 0 ]; then - lib_utils::die_usage "$_usage" + [ $# -eq 0 ] && lib_utils::die_usage "$_usage" + + local -r _arg="$1" + + [[ ! "$_arg" =~ (^repo${global_arg_delim_1}|^custom${global_arg_delim_1}) ]] && lib_utils::die_usage "$_usage" + + local -r _key="${_arg%${global_arg_delim_1}*}" + local -r _len="$((${#_key} + 1))" + + if [[ "$_key" =~ ^repo$|^custom$ ]]; then + local -r _arg_type="${_arg:${_len}}" + [ -z "$_arg_type" ] && lib_utils::die_usage "$_usage" fi - exec ${_path}/$* + local _path + case "$_key" in + repo) + _path="${_repo}/${_arg_type}" + ;; + custom) + _path="${_custom}/${_arg_type}" + ;; + *) + lib_utils::die_usage "$_usage" + ;; + esac + declare -gr global_arg_path="$_path" +} +function lib_plugins::__plugins() +{ + exec $global_arg_path "${@:2}" return $? }