From b772e6ace73ea6c9ede74373683577ea2726d9d6 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 9 Jan 2022 00:49:44 -0800 Subject: [PATCH 1/3] lib/log: `shellcheck` && `shfmt` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Alsö, fix tests to load `lib/colors` instead of `lib/appearance`...wut Alsö, `short-circuit _has_colors()`: If we already looked up colors, and we already have them, then don't run `tput` again. My apologies to future `git blame` hunters ♥ --- clean_files.txt | 1 + lib/log.bash | 77 ++++++++++++++++++++++--------------------------- 2 files changed, 36 insertions(+), 42 deletions(-) diff --git a/clean_files.txt b/clean_files.txt index a5959798..857ef910 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -78,6 +78,7 @@ completion/available/vuejs.completion.bash completion/available/wpscan.completion.bash # libraries +lib/log.bash lib/utilities.bash # plugins diff --git a/lib/log.bash b/lib/log.bash index 6bc53a12..f1bce9f7 100644 --- a/lib/log.bash +++ b/lib/log.bash @@ -1,60 +1,53 @@ -#!/usr/bin/env bash +# shellcheck shell=bash +# +# A collection of logging functions. export BASH_IT_LOG_LEVEL_ERROR=1 export BASH_IT_LOG_LEVEL_WARNING=2 export BASH_IT_LOG_LEVEL_ALL=3 -function _has_colors() -{ - # Check that stdout is a terminal - test -t 1 || return 1 - - ncolors=$(tput colors) - test -n "$ncolors" && test "$ncolors" -ge 8 || return 1 - return 0 +function _has_colors() { + # Check that stdout is a terminal, and that it has at least 8 colors. + [[ -t 1 && "${_bash_it_available_colors:=$(tput colors 2> /dev/null)}" -ge 8 ]] } -function _log_general() -{ - about 'Internal function used for logging, uses BASH_IT_LOG_PREFIX as a prefix' - param '1: color of the message' - param '2: log level to print before the prefix' - param '3: message to log' - group 'log' +function _log_general() { + about 'Internal function used for logging, uses BASH_IT_LOG_PREFIX as a prefix' + param '1: color of the message' + param '2: log level to print before the prefix' + param '3: message to log' + group 'log' - message=$2${BASH_IT_LOG_PREFIX:-default: }$3 - _has_colors && echo -e "$1${message}${echo_normal:-}" || echo -e "${message}" + message=$2${BASH_IT_LOG_PREFIX:-default: }$3 + _has_colors && echo -e "$1${message}${echo_normal:-}" || echo -e "${message}" } -function _log_debug() -{ - about 'log a debug message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_ALL' - param '1: message to log' - example '$ _log_debug "Loading plugin git..."' - group 'log' +function _log_debug() { + about 'log a debug message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_ALL' + param '1: message to log' + example '$ _log_debug "Loading plugin git..."' + group 'log' - [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ALL ]] || return 0 - _log_general "${echo_green:-}" "DEBUG: " "$1" + [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ALL ]] || return 0 + _log_general "${echo_green:-}" "DEBUG: " "$1" } -function _log_warning() -{ - about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_WARNING' - param '1: message to log' - example '$ _log_warning "git binary not found, disabling git plugin..."' - group 'log' +function _log_warning() { + about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_WARNING' + param '1: message to log' + example '$ _log_warning "git binary not found, disabling git plugin..."' + group 'log' - [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return 0 - _log_general "${echo_yellow:-}" " WARN: " "$1" + [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return 0 + _log_general "${echo_yellow:-}" " WARN: " "$1" } -function _log_error() -{ - about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_ERROR' - param '1: message to log' - example '$ _log_error "Failed to load git plugin..."' - group 'log' +function _log_error() { + about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_ERROR' + param '1: message to log' + example '$ _log_error "Failed to load git plugin..."' + group 'log' - [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return 0 - _log_general "${echo_red:-}" "ERROR: " "$1" + [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return 0 + _log_general "${echo_red:-}" "ERROR: " "$1" } From 6dec28b5dfb25acabde7778f19ec28920102570c Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sat, 16 Oct 2021 14:40:54 -0700 Subject: [PATCH 2/3] lib/log: rename `_log_general()` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ...to `_bash-it-log-message()`. alsö, add common log levels with common names. --- lib/log.bash | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/log.bash b/lib/log.bash index f1bce9f7..673fdd08 100644 --- a/lib/log.bash +++ b/lib/log.bash @@ -2,34 +2,39 @@ # # A collection of logging functions. -export BASH_IT_LOG_LEVEL_ERROR=1 -export BASH_IT_LOG_LEVEL_WARNING=2 -export BASH_IT_LOG_LEVEL_ALL=3 +# Declare log severity levels, matching syslog numbering +: "${BASH_IT_LOG_LEVEL_FATAL:=1}" +: "${BASH_IT_LOG_LEVEL_ERROR:=3}" +: "${BASH_IT_LOG_LEVEL_WARNING:=4}" +: "${BASH_IT_LOG_LEVEL_ALL:=6}" +: "${BASH_IT_LOG_LEVEL_INFO:=6}" +: "${BASH_IT_LOG_LEVEL_TRACE:=7}" +readonly "${!BASH_IT_LOG_LEVEL_@}" function _has_colors() { # Check that stdout is a terminal, and that it has at least 8 colors. [[ -t 1 && "${_bash_it_available_colors:=$(tput colors 2> /dev/null)}" -ge 8 ]] } -function _log_general() { +function _bash-it-log-message() { about 'Internal function used for logging, uses BASH_IT_LOG_PREFIX as a prefix' param '1: color of the message' param '2: log level to print before the prefix' param '3: message to log' group 'log' - message=$2${BASH_IT_LOG_PREFIX:-default: }$3 + message="$2${BASH_IT_LOG_PREFIX:-default: }$3" _has_colors && echo -e "$1${message}${echo_normal:-}" || echo -e "${message}" } function _log_debug() { - about 'log a debug message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_ALL' + about 'log a debug message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_INFO' param '1: message to log' example '$ _log_debug "Loading plugin git..."' group 'log' - [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ALL ]] || return 0 - _log_general "${echo_green:-}" "DEBUG: " "$1" + [[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_INFO?}" ]] || return 0 + _bash-it-log-message "${echo_green:-}" "DEBUG: " "$1" } function _log_warning() { @@ -38,8 +43,8 @@ function _log_warning() { example '$ _log_warning "git binary not found, disabling git plugin..."' group 'log' - [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return 0 - _log_general "${echo_yellow:-}" " WARN: " "$1" + [[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_WARNING?}" ]] || return 0 + _bash-it-log-message "${echo_yellow:-}" " WARN: " "$1" } function _log_error() { @@ -48,6 +53,6 @@ function _log_error() { example '$ _log_error "Failed to load git plugin..."' group 'log' - [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return 0 - _log_general "${echo_red:-}" "ERROR: " "$1" + [[ "${BASH_IT_LOG_LEVEL:-0}" -ge "${BASH_IT_LOG_LEVEL_ERROR?}" ]] || return 0 + _bash-it-log-message "${echo_red:-}" "ERROR: " "$1" } From e71ea4ad02fc82674dec55b57d444eaff1f2813f Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sat, 16 Oct 2021 14:54:08 -0700 Subject: [PATCH 3/3] lib/log: function `_bash-it-log-prefix-by-path()` ...to replace `_set-prefix-based-on-path()` in `scripts/reloader`. Deliberately does not use `_bash-it-get-component-name-from-path()`/`_bash-it-get-component-type-from-path()` as we need some of the intermediate state and would have to reimplement anyway. --- lib/log.bash | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/log.bash b/lib/log.bash index 673fdd08..444a6854 100644 --- a/lib/log.bash +++ b/lib/log.bash @@ -11,6 +11,38 @@ : "${BASH_IT_LOG_LEVEL_TRACE:=7}" readonly "${!BASH_IT_LOG_LEVEL_@}" +function _bash-it-log-prefix-by-path() { + local component_path="${1?${FUNCNAME[0]}: path specification required}" + local without_extension component_directory + local component_filename component_type component_name + + # get the directory, if any + component_directory="${component_path%/*}" + # drop the directory, if any + component_filename="${component_path##*/}" + # strip the file extension + without_extension="${component_filename%.bash}" + # strip before the last dot + component_type="${without_extension##*.}" + # strip component type, but try not to strip other words + # - aliases, completions, plugins, themes + component_name="${without_extension%.[acpt][hlo][eimu]*[ens]}" + # Finally, strip load priority prefix + component_name="${component_name##[[:digit:]][[:digit:]][[:digit:]]"${BASH_IT_LOAD_PRIORITY_SEPARATOR:----}"}" + + # best-guess for files without a type + if [[ "${component_type:-${component_name}}" == "${component_name}" ]]; then + if [[ "${component_directory}" == *'vendor'* ]]; then + component_type='vendor' + else + component_type="${component_directory##*/}" + fi + fi + + # shellcheck disable=SC2034 + BASH_IT_LOG_PREFIX="${component_type:-lib}: $component_name" +} + function _has_colors() { # Check that stdout is a terminal, and that it has at least 8 colors. [[ -t 1 && "${_bash_it_available_colors:=$(tput colors 2> /dev/null)}" -ge 8 ]]