Commit e380b399 authored by Emeric Verschuur's avatar Emeric Verschuur Committed by Emeric Verschuur
Browse files

Replace message functions by bashopts_log function

parent b099d64e
......@@ -45,29 +45,34 @@ trap 'bashopts_exit_handle' ERR
set -o errtrace
# display a error (fatal)
bashopts_critical() {
>&2 printf "[ERROR] %s\n" "$@"
bashopts_log() {
if [ -n "$bashopts_log_handler" ]; then
$bashopts_log_handler "$@"
return;
fi
local level=$1;
shift || bashopts_log C "Usage bashopts_log <level> message"
case "${level,,}" in
c|critical)
>&2 printf "[CRIT] %s\n" "$@"
exit 1
;;
e|error)
>&2 printf "[ERRO] %s\n" "$@"
;;
w|warning)
>&2 printf "[WARN] %s\n" "$@"
;;
*)
bashopts_log C "Invalid log level: $level"
;;
esac
}
if [ ! "${BASH_VERSINFO[0]}" -ge 4 ]; then
bashopts_critical "bashopts require BASH version 4 or greater"
bashopts_log C "bashopts require BASH version 4 or greater"
fi
# display a error (non fatal)
bashopts_error() {
>&2 printf "[ERROR] %s\n" "$@"
}
# display a warning (non fatal)
bashopts_warning() {
>&2 printf "[WARNING] %s\n" "$@"
}
bashopts_regex_escape () {
echo $1 | sed 's/[][()\.^$\/?*+]/\\&/g'
}
# extract the value part of a declaration ("the value")
bashopts_get_def() {
declare | grep "^$1=" | sed -E 's/^[^=]+=//g'
......@@ -88,7 +93,7 @@ bashopts_check_opt_name() {
echo $1
return 0
fi
bashopts_error "'$1' is not a valid variable name"
bashopts_log E "'$1' is not a valid variable name"
return 1
}
......@@ -101,23 +106,23 @@ bashopts_check_number() {
echo $1
return 0
fi
bashopts_error "'$1' is not a valid number"
bashopts_log E "'$1' is not a valid number"
return 1
}
# check and format a boolean value
bashopts_check_boolean() {
case "$1" in
''|f|false|F|FALSE|False|0)
case "${1,,}" in
''|f|false|0)
echo "false"
return 0
;;
t|true|T|TRUE|True|1)
t|true|1)
echo "true"
return 0
;;
*)
bashopts_error "'$1' is not a valid boolean value"
bashopts_log E "'$1' is not a valid boolean value"
return 1
;;
esac
......@@ -155,7 +160,7 @@ bashopts_tool_name=$0
bashopts_setup() {
local arg arglist no_default_opts non_interactive disable_interactive
if ! arglist=$(getopt -o "n:d:u:s:yxp" -n "$0 " -- "$@"); then
bashopts_critical "Usage bashopts_setup:" \
bashopts_log C "Usage bashopts_setup:" \
" -n <val> Tool name" \
" -d <val> Tool description" \
" -u <val> Tool usage description" \
......@@ -178,14 +183,14 @@ bashopts_setup() {
-x) disable_interactive="true";;
-p) bashopts_tool_settings_force_write="true";;
--) break;;
*) bashopts_critical "Fatal error";;
*) bashopts_log C "Fatal error";;
esac
done
if [ -z "$bashopts_tool_name" ]; then
bashopts_critical "Undefined tool name"
bashopts_log C "Undefined tool name"
fi
if [ -z "$bashopts_tool_description" ]; then
bashopts_critical "Undefined tool description"
bashopts_log C "Undefined tool description"
fi
bashopts_tool_usage=${bashopts_tool_usage:-"$bashopts_tool_name [options and commands] [-- [extra args]]"}
# add the default options
......@@ -205,7 +210,7 @@ bashopts_setup() {
bashopts_declare() {
local arg arglist options
if ! arglist=$(getopt -o "n:v:e:o:l:d:t:m:k:rsi" -n "$0 " -- "$@"); then
bashopts_critical "Usage bashopts_declare:" \
bashopts_log C "Usage bashopts_declare:" \
" -n <val> Name" \
" -v <val> Default value" \
" -e <val> Bash expression: like default but this expression is computed and can contain variables and other bash expression" \
......@@ -239,12 +244,12 @@ bashopts_declare() {
-i) options[interactive]="true";;
-r) options[req_value]="true";;
--) break;;
*) bashopts_critical "Fatal error";;
*) bashopts_log C "Fatal error";;
esac
done
# Check incompatible -v and -r options
if [ -n "${options[default]}" ] && [ "${options[req_value]}" == "true" ]; then
bashopts_critical "bashopts_declare: -r and -v options cannot be activated at the same time"
bashopts_log C "bashopts_declare: -r and -v options cannot be activated at the same time"
fi
# format the type and check/format the default value
case "${options[type]}" in
......@@ -258,7 +263,7 @@ bashopts_declare() {
options[type]="number"
;;
*)
bashopts_critical "Invalid type ${options[type]}"
bashopts_log C "Invalid type ${options[type]}"
;;
esac
# Setup check value method
......@@ -273,7 +278,7 @@ bashopts_declare() {
if [ "${options[type]}" != "string" ] || [[ -v options[default] ]]; then
# Check the default value format
options[default]="$(${options[check]} "${options[default]}")" \
|| bashopts_critical "Invalid default value for ${options[name]} option"
|| bashopts_log C "Invalid default value for ${options[name]} option"
fi
;;
a|add)
......@@ -281,30 +286,30 @@ bashopts_declare() {
options[method]="add"
;;
*)
bashopts_critical "Invalid method ${options[method]}"
bashopts_log C "Invalid method ${options[method]}"
;;
esac
# Check option name
if [[ -v bashopts_optprop_name[${options[name]}] ]]; then
bashopts_critical "Dupplicate option name '${options[name]}'"
bashopts_log C "Dupplicate option name '${options[name]}'"
fi
# check the short option
if [[ -v options[short_opt] ]]; then
if ! [[ ${options[short_opt]} =~ ^[a-zA-Z0-9_-]$ ]]; then
bashopts_critical "Invalid short option ${options[short_opt]}"
bashopts_log C "Invalid short option ${options[short_opt]}"
fi
if [[ -v bashopts_arg2op[-${options[short_opt]}] ]]; then
bashopts_critical "Dupplicate short option '${options[short_opt]}'"
bashopts_log C "Dupplicate short option '${options[short_opt]}'"
fi
bashopts_arg2op[-${options[short_opt]}]=${options[name]}
fi
# check the long option
if [[ -v options[long_opt] ]]; then
if ! [[ ${options[long_opt]} =~ ^[a-zA-Z0-9_-]{2,}$ ]]; then
bashopts_critical "Invalid long option ${options[long_opt]}"
bashopts_log C "Invalid long option ${options[long_opt]}"
fi
if [[ -v bashopts_arg2op[--${options[long_opt]}] ]]; then
bashopts_critical "Dupplicate long option '${options[long_opt]}'"
bashopts_log C "Dupplicate long option '${options[long_opt]}'"
fi
bashopts_arg2op[--${options[long_opt]}]=${options[name]}
fi
......@@ -328,14 +333,14 @@ bashopts_math_min() {
# join array element
bashopts_join_by() {
local IFS="$1"
shift || bashopts_critical "Usage: bashopts_join_by <character> [elt1 [elt2...]]"
shift || bashopts_log C "Usage: bashopts_join_by <character> [elt1 [elt2...]]"
echo "$*"
}
# dump an option value by its name
bashopts_dump_value() {
local op=$1
shift || bashopts_critical "Usage: bashopts_dump_value op_name"
shift || bashopts_log C "Usage: bashopts_dump_value op_name"
[[ -v "$op" ]] || return 0
if [ "${bashopts_optprop_method[$op]}" == "set" ]; then
if [ "${bashopts_optprop_type[$op]}" == "string" ]; then
......@@ -507,7 +512,7 @@ bashopts_parse_args() {
esac
;;
*)
bashopts_critical "Fatal error: args"
bashopts_log C "Fatal error: args"
;;
esac
done
......@@ -519,7 +524,7 @@ bashopts_parse_args() {
# display an array: [val1, val2, ...]
bashopts_dump_array() {
local type=$1
shift || bashopts_critical "Usage: bashopts_dump_array type elt1 [elt2...]"
shift || bashopts_log C "Usage: bashopts_dump_array type elt1 [elt2...]"
echo -n "["
if [ "$type" == "string" ]; then
echo -n "\"${1//\"/\\\"}\""
......@@ -542,7 +547,7 @@ bashopts_dump_array() {
bashopts_process_option() {
local dval tval ival op arg arglist check val_req edit_req
if ! arglist=$(getopt -o "n:k:r" -n "bashopts_process_option " -- "$@"); then
bashopts_critical "Usage bashopts_process_opt" \
bashopts_log C "Usage bashopts_process_opt" \
" -n <val> property name" \
" -k <val> override value check function" \
" -r At least one value required"
......@@ -557,11 +562,11 @@ bashopts_process_option() {
-k) check=$1; shift;;
-r) val_req="true";;
--) break;;
*) bashopts_critical "Fatal error";;
*) bashopts_log C "Fatal error";;
esac
done
test -n "$op" || \
bashopts_critical "bashopts_process_option: missing -n option"
bashopts_log C "bashopts_process_option: missing -n option"
if [ -z "$check" ]; then
check="${bashopts_optprop_check[$op]}"
fi
......@@ -594,7 +599,7 @@ bashopts_process_option() {
for (( i=0; i<${#tval[@]}; i++)); do
if ! $check "${tval[$i]}" > /dev/null; then
if [ "$BASHOPTS_INTERACTIVE" != "true" ]; then
bashopts_critical "Non interactive mode: Exit due to one or more error"
bashopts_log C "Non interactive mode: Exit due to one or more error"
fi
# (re)enable edition
edit_req="true"
......@@ -602,9 +607,9 @@ bashopts_process_option() {
fi
done
elif [ "$val_req" == "true" ]; then
bashopts_error "At least one value required"
bashopts_log E "At least one value required"
if [ "$BASHOPTS_INTERACTIVE" != "true" ]; then
bashopts_critical "Non interactive mode: Exit due to one or more error"
bashopts_log C "Non interactive mode: Exit due to one or more error"
fi
# (re)enable edition
edit_req="true"
......@@ -625,7 +630,7 @@ bashopts_process_option() {
if [ "${bashopts_optprop_method[$op]}" == "add" ]; then
# array value
if ! eval "tval=$ival" 2>/dev/null; then
bashopts_error "'$ival' must be written in BASH array format: '( \"val 1\" \"val2\" \"val3...\" )'"
bashopts_log C "'$ival' must be written in BASH array format: '( \"val 1\" \"val2\" \"val3...\" )'"
unset tval
continue
fi
......@@ -636,7 +641,7 @@ bashopts_process_option() {
fi
# check format
if [ "${#tval[@]}" -eq 0 ] && [ "$val_req" == "true" ]; then
bashopts_error "At least one value required"
bashopts_log E "At least one value required"
unset tval
continue
fi
......@@ -682,9 +687,9 @@ bashopts_process_option() {
# force_write is true
echo "$(bashopts_get_def_full $op)" >> $bashopts_tool_settings_path
fi
) || bashopts_warning "Please check the settings file"
) || bashopts_log W "Please check the settings file"
else
bashopts_warning "No settings file specified"
bashopts_log W "No settings file specified"
fi
fi
if [ "$op" == "BASHOPTS_NON_INTERACTIVE" ] && ! [[ -v BASHOPTS_INTERACTIVE ]]; then
......
......@@ -159,7 +159,7 @@ _test_case_10() {
bashopts_declare -n simple_val -s -l value -d "Simple value" -t string -v 'val'
bashopts_parse_args --value=test
req_test_eq "$(bashopts_process_opts 2>&1)" "[WARNING] No settings file specified"
req_test_eq "$(bashopts_process_opts 2>&1)" "[WARN] No settings file specified"
}
_test_case_11() {
......@@ -168,7 +168,7 @@ _test_case_11() {
bashopts_declare -n simple_val -s -l value -d "Simple value" -t string -v 'val'
bashopts_parse_args --value=test
req_test_eq "$(bashopts_process_opts 2>&1 | grep 'WARNING')" "[WARNING] Please check the settings file"
req_test_eq "$(bashopts_process_opts 2>&1 | grep 'WARN')" "[WARN] Please check the settings file"
}
_test_case_12() {
......@@ -276,9 +276,12 @@ _test_case_18() {
bashopts_declare -n a_boolean_value -l boolean -m set -d "A boolean value" -t boolean -i
bashopts_parse_args
req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "tru")" "[ERROR] 'tru' is not a valid boolean value"
req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "fa")" "[ERROR] 'fa' is not a valid boolean value"
req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "5")" "[ERROR] '5' is not a valid boolean value"
req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "tru" \
| grep '^\[ERR')" "[ERRO] 'tru' is not a valid boolean value"
req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "fa" \
| grep '^\[ERR')" "[ERRO] 'fa' is not a valid boolean value"
req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "5" \
| grep '^\[ERR')" "[ERRO] '5' is not a valid boolean value"
}
_test_case_19() {
......@@ -297,7 +300,7 @@ is_eq_to_null() {
echo "$1"
return 0
fi
bashopts_error "Not equal to NULL"
bashopts_log E "Not equal to NULL"
return 1
}
......@@ -309,11 +312,9 @@ _test_case_20() {
bashopts_parse_args
bashopts_process_opts
req_test_eq "$(echo "$(bashopts_process_option -n value -k is_eq_to_null -r 2>&1 > /dev/null <<< "na")" \
| grep '\[ERROR\] Not equal to NULL' \
| head -n 1 || true)" '[ERROR] Not equal to NULL'
| grep '^\[ERR' | tail -n 1 || true)" '[ERRO] Not equal to NULL'
req_test_eq "$(echo "$(bashopts_process_option -n value -k is_eq_to_null -r 2>&1 > /dev/null <<< "()")" \
| grep '\[ERROR\] At least one value required' \
| head -n 1 || true)" '[ERROR] At least one value required'
| grep '^\[ERR' | tail -n 1 || true)" '[ERRO] At least one value required'
bashopts_process_option -n value -k is_eq_to_null -r > /dev/null 2>&1 <<< "NULL"
}
......@@ -325,8 +326,7 @@ _test_case_21() {
bashopts_parse_args -n
bashopts_process_opts
req_test_eq "$(echo "$(bashopts_process_option -n value -k is_not_empty -r 2>&1 > /dev/null)" \
| grep '\[ERROR\] At least one value required' \
| head -n 1)" '[ERROR] At least one value required'
| grep '^\[ERR' | head -n 1)" '[ERRO] At least one value required'
}
_test_case_22() {
......@@ -388,7 +388,7 @@ _test_case_27() {
bashopts_setup -n "$0" -d "Test case $0"
req_test_eq "$(bashopts_declare -n value -l value -d "A str value" -t string -r -v "test" 2>&1)" \
"[ERROR] bashopts_declare: -r and -v options cannot be activated at the same time"
"[CRIT] bashopts_declare: -r and -v options cannot be activated at the same time"
}
_test() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment