From 6e5c5986c3b3d3d0bafa1632daf4285a4ad8fe39 Mon Sep 17 00:00:00 2001 From: Tony Tkacik Date: Fri, 11 Nov 2016 19:54:18 +0100 Subject: [PATCH] Refactored some common functions to nux-base. Signed-off-by: Tony Tkacik --- bin/nux-runner | 29 ++++++++++------------- inc/nux-base.inc.sh | 56 +++++++++++++++++++++++++++++++++++++++++++++ inc/nux-base.sh | 23 ------------------- 3 files changed, 68 insertions(+), 40 deletions(-) create mode 100644 inc/nux-base.inc.sh delete mode 100644 inc/nux-base.sh diff --git a/bin/nux-runner b/bin/nux-runner index 74f29b9..522c99b 100755 --- a/bin/nux-runner +++ b/bin/nux-runner @@ -6,14 +6,9 @@ ### (similar in usage such as apt, git). ### - -# .---------- constant part! -# vvvv vvvv-- the code from above - - readonly NUX_RUNNER=$0; readonly NUX_RUNNER_BIN_DIR=$(dirname $(realpath ${BASH_SOURCE[0]})) -source $NUX_RUNNER_BIN_DIR/../inc/nux-base.sh +source $NUX_RUNNER_BIN_DIR/../inc/nux-base.inc.sh @@ -22,21 +17,17 @@ function is_function () { return 1 } -log.debug() { - : -} - runner.run() { TASK=$1; shift; # Determines task if is_function task.$TASK ; then - log.debug "Running task: $TASK"; + nux.log debug "Running task: $TASK"; task.$TASK "$@" # Runs task else - log.debug "Including script: $NUX_SCRIPT" + nux.log debug "Including script: $NUX_SCRIPT" source $NUX_SCRIPT; # Includes script if is_function task.$TASK ; then - log.debug "Running task: $TASK"; + nux.log debug "Running task: $TASK"; task.$TASK "$@" # Runs task else echo "$NUX_SCRIPTNAME: Unrecognized task ''$TASK' not available." @@ -45,16 +36,20 @@ runner.run() { fi fi } - ## ## Tasks provided by 'nux-runner': ## debug - Runs specified task with debug messages enabled. task.debug() { - log.debug() { - echo -e "${NC_Debug}[DEBUG] $@ ${NC_No}" >&2 - } + N_LOG_debug=1 + runner.run "$@" +} + +## trace - Runs specified task with debug & trace enabled. +task.trace() { + N_LOG_debug=1; + N_LOG_trace=2; runner.run "$@" } diff --git a/inc/nux-base.inc.sh b/inc/nux-base.inc.sh new file mode 100644 index 0000000..73974ba --- /dev/null +++ b/inc/nux-base.inc.sh @@ -0,0 +1,56 @@ + +readonly NUX_INC_DIR=$(dirname $(realpath ${BASH_SOURCE[0]})) + +# Color defintions + +readonly NC_No='\033[0m' # No Color +readonly NC_Black='\033[0;30m' +readonly NC_Green='\033[0;32m' +readonly NC_Red='\033[0;31m' +readonly NC_BrownOrange='\033[0;33m' +readonly NC_Blue='\033[0;34m' +readonly NC_Purple='\033[0;35m' +readonly NC_Cyan='\033[0;36m' +readonly NC_LightGray='\033[0;37m' +readonly NC_DarkGray='\033[1;30m' +readonly NC_LightRed='\033[1;31m' +readonly NC_LightGreen='\033[1;32m' +readonly NC_Yellow='\033[1;33m' +readonly NC_LightBlue='\033[1;34m' +readonly NC_LightPurple='\033[1;35m' +readonly NC_LightCyan='\033[1;36m' +readonly NC_White='\033[1;37m' + +# Color for message levels +NC_info=$NC_LightGray +NC_error=$NC_LightRed +NC_warning=$NC_Yellow +NC_debug=$NC_White + +N_LOG_info=1 +N_LOG_error=2 +N_LOG_warning=3 + +function nux.log { + local level=$1 + local color=NC_$level + local setting=N_LOG_$level + shift; + if [ ! -z ${!setting+x} ]; then + echo -e "${!color}[$level]$NC_No $*$NC_No" >&2 + fi +} + +function nux.echo.error { + echo -e "${NC_error}$* ${NC_No}"; +} + +function nux.echo.warning { + echo -e "${NC_warning}$* ${NC_No}"; +} + + +function nux.include { + local incfile="$1.inc.sh" + source "$NUX_INC_DIR/$incfile" +} diff --git a/inc/nux-base.sh b/inc/nux-base.sh deleted file mode 100644 index 18af751..0000000 --- a/inc/nux-base.sh +++ /dev/null @@ -1,23 +0,0 @@ - -readonly NC_No='\033[0m' # No Color -readonly NC_Black='\033[0;30m' -readonly NC_Green='\033[0;32m' -readonly NC_Red='\033[0;31m' -readonly NC_BrownOrange='\033[0;33m' -readonly NC_Blue='\033[0;34m' -readonly NC_Purple='\033[0;35m' -readonly NC_Cyan='\033[0;36m' -readonly NC_LightGray='\033[0;37m' - -readonly NC_DarkGray='\033[1;30m' -readonly NC_LightRed='\033[1;31m' -readonly NC_LightGreen='\033[1;32m' -readonly NC_Yellow='\033[1;33m' -readonly NC_LightBlue='\033[1;34m' -readonly NC_LightPurple='\033[1;35m' -readonly NC_LightCyan='\033[1;36m' -readonly NC_White='\033[1;37m' - -NC_Debug=$NC_LightGray -NC_Error=$NC_LightRed -NC_Warning=$NC_Yellow