From 449c1b928926a312f3fa4a59f153a8c899268d22 Mon Sep 17 00:00:00 2001 From: Patrick Stadler Date: Sat, 21 Mar 2015 22:03:29 +0100 Subject: [PATCH] enable -m / -r --- README.md | 1 - lib/main.sh | 49 ++++++++++++++++++++++++--------------------- lib/utils/config.sh | 24 ++++++++++++++++++++++ 3 files changed, 50 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index a7e8707..f589797 100644 --- a/README.md +++ b/README.md @@ -56,5 +56,4 @@ TODO: how to write custom reporters - README - config file docs - load custom/contrib metrics and reporters -- enable -m / -r - allow multiple reporters? \ No newline at end of file diff --git a/lib/main.sh b/lib/main.sh index ceae8ea..481061c 100644 --- a/lib/main.sh +++ b/lib/main.sh @@ -30,8 +30,6 @@ main_load () { local filename=$(basename $file) local reporter=${filename%.*} - load_reporter_with_prefix __r_${reporter}_ $file - __AVAILABLE_REPORTERS=$(trim "$__AVAILABLE_REPORTERS $reporter") done @@ -52,35 +50,23 @@ main_init () { # create temp dir TEMP_DIR=$(make_temp_dir) +} +main_collect () { # check if reporter exists - if ! in_array $__REPORTER "$__AVAILABLE_REPORTERS"; then + if ! in_array $(resolve_reporter $__REPORTER) "$__AVAILABLE_REPORTERS"; then echo "Error: reporter '$__REPORTER' is not available" exit 1 fi # check if metrics exist for metric in $__METRICS; do - metric=$(get_name $metric) - if ! in_array $metric "$__AVAILABLE_METRICS"; then + if ! in_array $(resolve_metric $metric) "$__AVAILABLE_METRICS"; then echo "Error: metric '$metric' is not available" exit 1 fi done - # init reporter - if is_function __r_${__REPORTER}_defaults; then - __r_${__REPORTER}_defaults - fi - if is_function __r_${__REPORTER}_config; then - __r_${__REPORTER}_config - fi - if is_function __r_${__REPORTER}_start; then - __r_${__REPORTER}_start - fi -} - -main_collect () { # register trap trap ' trap "" 13 @@ -89,15 +75,31 @@ main_collect () { kill -13 -$$ ' 13 INT TERM EXIT + # init reporter + local reporter_alias=$(resolve_reporter $__REPORTER) + local reporter_file=$(resolve_reporter $__REPORTER) + load_reporter_with_prefix __r_${reporter_alias}_ ./reporters/${reporter_file}.sh + + if is_function __r_${reporter_alias}_defaults; then + __r_${reporter_alias}_defaults + fi + if is_function __r_${reporter_alias}_config; then + __r_${reporter_alias}_config + fi + if is_function __r_${reporter_alias}_start; then + __r_${reporter_alias}_start + fi + # collect metrics for metric in $__METRICS; do # run in subshell to isolate scope ( local metric_name=$(get_name $metric) local metric_alias=$(get_alias $metric) + local metric_file=$(resolve_metric $metric) # init metric - load_metric_with_prefix __m_${metric_alias}_ ./metrics/${metric_name}.sh + load_metric_with_prefix __m_${metric_alias}_ ./metrics/${metric_file}.sh if is_function __m_${metric_alias}_defaults; then __m_${metric_alias}_defaults @@ -135,7 +137,7 @@ main_collect () { _r_result="$2" fi if is_number $_r_result; then - __r_${__REPORTER}_report $_r_label $_r_result + __r_${reporter_alias}_report $_r_label $_r_result fi } @@ -156,9 +158,10 @@ main_collect () { main_terminate () { # stop reporter - verbose "Stopping reporter '${__REPORTER}'" - if is_function __r_${__REPORTER}_stop; then - __r_${__REPORTER}_stop + local reporter_alias=$(get_alias $__REPORTER) + verbose "Stopping reporter '${reporter_alias}'" + if is_function __r_${reporter_alias}_stop; then + __r_${reporter_alias}_stop fi verbose "Cleaning up..." diff --git a/lib/utils/config.sh b/lib/utils/config.sh index c37e273..ccb910e 100644 --- a/lib/utils/config.sh +++ b/lib/utils/config.sh @@ -104,4 +104,28 @@ get_alias () { _alias=$(echo $1 | awk 'BEGIN { FS=":" } { print $1 }') fi echo $_alias +} + +resolve_reporter () { + local _alias=$(get_alias $1) + for reporter in $__CFG_REPORTERS; do + local __alias=$(get_alias $reporter) + if [ $_alias = $__alias ]; then + echo $(get_name $reporter) + return + fi + done + echo $_alias +} + +resolve_metric () { + local _alias=$(get_alias $1) + for metric in $__CFG_METRICS; do + local __alias=$(get_alias $metric) + if [ $_alias = $__alias ]; then + echo $(get_name $metric) + return + fi + done + echo $_alias } \ No newline at end of file