#@IgnoreInspection BashAddShebang # Base app functionality (Koalephant Shell Script Library) # Version: 2.0.0 # Copyright: 2014-2017, Koalephant Co., Ltd # Author: Stephen Reay , Koalephant Packaging Team # Base Shell Library version setglobal KOALEPHANT_LIB_VERSION = '"2.0.0'" const KOALEPHANT_LIB_VERSION = '' # Base directory for the installed Library setglobal KOALEPHANT_LIB_PATH = $(KOALEPHANT_LIB_PATH:-PREFIX/LIBRARY_PATH) const KOALEPHANT_LIB_PATH = '' # Get the tool name # # Input: # $KOALEPHANT_TOOL_NAME - explicit name used as-is if set # $0 - the script name is stripped of directory components and any '.sh' suffix # # Output: # the tool name proc k_tool_name { if test -n $(KOALEPHANT_TOOL_NAME:-) { printf "%s" $(KOALEPHANT_TOOL_NAME) } else { k_fs_basename $(0) ".sh" } } # Get the tool version # # Input: # $KOALEPHANT_TOOL_VERSION - explicit version used as-is if set. Default is 1.0.0 if not set # # Output: # the tool version proc k_tool_version { printf "%s" $(KOALEPHANT_TOOL_VERSION:-1.0.0) } # Get the tool copyright year # # Input: # $KOALEPHANT_TOOL_YEAR - explicit copyright year used as-is if set. Default is current year if not set # # Output: # the tool copyright year proc k_tool_year { printf "%s" $(KOALEPHANT_TOOL_YEAR:-$(date +%Y)) } # Get the tool copyright owner # # Input: # $KOALEPHANT_TOOL_OWNER - explicit copyright owner used as-is if set. Default is 'Koalephant Co., Ltd' if not set # # Output: # the tool copyright owner proc k_tool_owner { printf "%s" $(KOALEPHANT_TOOL_OWNER:-Koalephant Co., Ltd) } # Get the tool description # # Input: # $KOALEPHANT_TOOL_DESCRIPTION - tool description used as-is # # Output: # the tool description proc k_tool_description { printf "%s" $(KOALEPHANT_TOOL_DESCRIPTION:-) } # Get the tool options descriptions # # Input: # $KOALEPHANT_TOOL_OPTIONS - tool options descriptions used as-is # # Output: # the tool options descriptions proc k_tool_options { printf "%s" $(KOALEPHANT_TOOL_OPTIONS:-) } # Get the tool option(s) argument(s) # Shows output if (#k_tool_options) returns non-empty content # # Input: # $KOALEPHANT_TOOL_OPTIONS_ARGUMENTS - explicit tool option(s) argument(s) used as-is. Default is '[options]' if not set # # Output: # the tool option(s) argument(s) proc k_tool_options_arguments { if test -n $[k_tool_options] { printf " %s" $(KOALEPHANT_TOOL_OPTIONS_ARGUMENTS:-[options]) } } # Get the tool arguments # # Input: # $KOALEPHANT_TOOL_ARGUMENTS - tool arguments used as-is # # Output: # the tool arguments proc k_tool_arguments { printf " %s" $(KOALEPHANT_TOOL_ARGUMENTS:-) } # Get the tool usage # uses (#k_tool_name), (#k_tool_options_arguments) and (#k_tool_arguments) # # Output: # the tool usage proc k_tool_usage { printf "Usage: %s%s%s\n" $[k_tool_name] $[k_tool_options_arguments] $[k_tool_arguments] } # Show the current tool version # uses (#k_tool_name), (#k_tool_version) (#k_tool_year) and (#k_tool_owner) # # Output: # the version information proc k_version { printf "%s version %s \nCopyright (c) %s, %s\n" $[k_tool_name] $[k_tool_version] $[k_tool_year] $[k_tool_owner] } # Show the Usage info for this script # uses (#k_tool_name), (#k_tool_description) and (#k_tool_usage) and (#k_tool_options) # # Output: # the usage information proc k_usage { printf "%s\n" $[k_tool_usage] printf "%s\n\n" $[k_tool_description] printf "%s\n" $[k_tool_options] } # Check if the running (effective) user is root, and error if not # # Input: # $1 - an extra error message to show if the current user is not root # # Output: # Any additional message provided in `$1` plus a standard error message, if run by a non-root user. # # Return: # 0 when effective user is root, 1 otherwise proc k_require_root { if test $[id -u] -ne 0 { if test -n $(1) { k_log_err $(1) } k_log_err "$[k_tool_name] must be run as root" return 1 } } # Check that an option has an argument specified. # # Input: # $1 - the option name # $2 - the next argument the tool was invoked with # # Output: # the argument value if valid # # Return: # 0 on success, 1 otherwise proc k_option_requires_arg { if test -n $(2:-) && ! k_string_starts_with "-" $(2) { printf '%s' $(2) } else { k_log_err "Error, $(1) requires an argument" k_usage return 1 } } # Check whether an option has an optional argument specified. # # Input: # $1 - the option name # $2 - the next argument the tool was invoked with # # Output: # the argument value if specified proc k_option_optional_arg { var tool = $1 if test -n $(2:-) && ! k_string_starts_with "-" $(2) { printf '%s' $(2) } } # Log level EMERG - system is unusable setglobal KOALEPHANT_LOG_LEVEL_EMERG = '1' const KOALEPHANT_LOG_LEVEL_EMERG = '' # Log level ALERT - action must be taken immediately setglobal KOALEPHANT_LOG_LEVEL_ALERT = '2' const KOALEPHANT_LOG_LEVEL_ALERT = '' # Log level CRIT - critical conditions setglobal KOALEPHANT_LOG_LEVEL_CRIT = '3' const KOALEPHANT_LOG_LEVEL_CRIT = '' # Log level ERR - error conditions setglobal KOALEPHANT_LOG_LEVEL_ERR = '4' const KOALEPHANT_LOG_LEVEL_ERR = '' # Log level WARNING - warning conditions setglobal KOALEPHANT_LOG_LEVEL_WARNING = '5' const KOALEPHANT_LOG_LEVEL_WARNING = '' # Log level NOTICE - normal, but significant, condition setglobal KOALEPHANT_LOG_LEVEL_NOTICE = '6' const KOALEPHANT_LOG_LEVEL_NOTICE = '' # Log level INFO - informational message setglobal KOALEPHANT_LOG_LEVEL_INFO = '7' const KOALEPHANT_LOG_LEVEL_INFO = '' # Log level DEBUG - debug-level message setglobal KOALEPHANT_LOG_LEVEL_DEBUG = '8' const KOALEPHANT_LOG_LEVEL_DEBUG = '' # Check if a value is a valid log level # # Input: # $1 - log level value to check proc k_log_level_valid { test $1 -ge $(KOALEPHANT_LOG_LEVEL_EMERG) && test $1 -le $(KOALEPHANT_LOG_LEVEL_DEBUG) } # Get/Set the current log level # # Input: # $1 - if provided, set the active log level to this value # $KOALEPHANT_LOG_LEVEL_ACTIVE - used as explicit log level if set. Default is ($#KOALEPHANT_LOG_LEVEL_ERR) if not set # # Output: # the active log level proc k_log_level { if test $Argc -eq 1 { if ! k_log_level_valid $1 { printf "%s" "Invalid log level specified" > !2 return 1 } else { setglobal KOALEPHANT_LOG_LEVEL_ACTIVE = $1 } } printf '%d' $(KOALEPHANT_LOG_LEVEL_ACTIVE:-$KOALEPHANT_LOG_LEVEL_ERR) } # Get the log level name from a log level value proc k_log_level_name { if ! k_log_level_valid $1 { printf "%s" "Invalid log level specified" > !2 return 1 } match $(1) { with ${KOALEPHANT_LOG_LEVEL_EMERG} printf "%s" "emerg" with ${KOALEPHANT_LOG_LEVEL_ALERT} printf "%s" "alert" with ${KOALEPHANT_LOG_LEVEL_CRIT} printf "%s" "crit" with ${KOALEPHANT_LOG_LEVEL_ERR} printf "%s" "err" with ${KOALEPHANT_LOG_LEVEL_WARNING} printf "%s" "warning" with ${KOALEPHANT_LOG_LEVEL_NOTICE} printf "%s" "notice" with ${KOALEPHANT_LOG_LEVEL_INFO} printf "%s" "info" with ${KOALEPHANT_LOG_LEVEL_DEBUG} printf "%s" "debug" } } # Check if logs should be set to syslog # # Input: # $KOALEPHANT_LOG_SYSLOG - used as flag to enable syslog # # Return: # 0 if syslog should be used, 1 otherwise proc k_log_syslog { k_bool_test $(KOALEPHANT_LOG_SYSLOG:-) } # Log a message according to a syslog level # # Input: # $1 - the syslog level constant to use. One of the `KOALEPHANT_LOG_LEVEL_*` constants proc k_log_message { var level = $1 shift var message = "$ifsjoin(Argv)" if k_log_level_valid $level && test $[k_log_level] -ge $(level) { printf "%s\n" @Argv > !2 if k_log_syslog { /usr/bin/logger --tag $[k_tool_name] --id $Pid --priority "user.$[k_log_level_name $(level)]" -- @Argv } } } # Log a message at syslog-level EMERG # # Input: # $1...n - the message string proc k_log_emerg { k_log_message $(KOALEPHANT_LOG_LEVEL_EMERG) @Argv } # Log a message at syslog-level ALERT # # Input: # $1...n - the message string proc k_log_alert { k_log_message $(KOALEPHANT_LOG_LEVEL_ALERT) @Argv } # Log a message at syslog-level CRIT # # Input: # $1...n - the message string proc k_log_crit { k_log_message $(KOALEPHANT_LOG_LEVEL_CRIT) @Argv } # Log a message at syslog-level ERR # # Input: # $1...n - the message string proc k_log_err { k_log_message $(KOALEPHANT_LOG_LEVEL_ERR) @Argv } # Log a message at syslog-level WARNING # # Input: # $1...n - the message string proc k_log_warning { k_log_message $(KOALEPHANT_LOG_LEVEL_WARNING) @Argv } # Log a message at syslog-level NOTICE # # Input: # $1...n - the message string proc k_log_notice { k_log_message $(KOALEPHANT_LOG_LEVEL_NOTICE) @Argv } # Log a message at syslog-level INFO # # Input: # $1...n - the message string proc k_log_info { k_log_message $(KOALEPHANT_LOG_LEVEL_INFO) @Argv } # Log a message at syslog-level DEBUG # # Input: # $1...n - the message string proc k_log_debug { k_log_message $(KOALEPHANT_LOG_LEVEL_DEBUG) @Argv }