Skip to content

Commit

Permalink
Better configuration loading management.
Browse files Browse the repository at this point in the history
Short 3 lines summary

tag: 2.2.0

# Breaking changes

# Bug fixes

- fixed regression - global options passed to the command was not
  taken into account(--verbose, -no-color, --log-level, ...)

# Compiler changes

# Binaries changes

- all binaries are using
  - shopt -s lastpipe headers change

# Updated Bash framework functions

# New Bash framework functions

# Documentation

# Validation/Tooling
  • Loading branch information
fchastanet committed Jan 6, 2024
1 parent 7b1d732 commit abfdcaa
Show file tree
Hide file tree
Showing 17 changed files with 636 additions and 416 deletions.
75 changes: 47 additions & 28 deletions bin/awkLint
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ set -o errexit
# Command Substitution can inherit errexit option since bash v4.4
shopt -s inherit_errexit || true

# if set, and job control is not active, the shell runs the last command
# of a pipeline not executed in the background in the current shell
# environment.
shopt -s lastpipe

# a log is generated when a command fails
set -o errtrace

# use nullglob so that (file*.php) will return an empty array if no file matches the wildcard
# use nullglob so that (file*.php) will return an empty array if no file
# matches the wildcard
shopt -s nullglob

# ensure regexp are interpreted without accentuated characters
Expand Down Expand Up @@ -253,10 +259,6 @@ Env::requireLoad() {
if [[ -f "${FRAMEWORK_ROOT_DIR}/.framework-config" ]]; then
configFiles+=("${FRAMEWORK_ROOT_DIR}/.framework-config")
fi
if [[ -n "${optionBashFrameworkConfig}" && -f "${optionBashFrameworkConfig}" ]]; then
# shellcheck disable=SC2034
configFiles+=("${optionBashFrameworkConfig}")
fi
configFiles+=("${optionEnvFiles[@]}")
configFiles+=("${defaultFiles[@]}")

Expand All @@ -269,6 +271,14 @@ Env::requireLoad() {
done
}

# @description create a temp file using default TMPDIR variable
# initialized in _includes/_commonHeader.sh
# @env TMPDIR String (default value /tmp)
# @arg $1 templateName:String template name to use(optional)
Framework::createTempFile() {
mktemp -p "${TMPDIR:-/tmp}" -t "${1:-}.XXXXXXXXXXXX"
}

# @description Log namespace provides 2 kind of functions
# - Log::display* allows to display given message with
# given display level
Expand Down Expand Up @@ -716,21 +726,21 @@ optionEnvFileCallback() {
optionInfoVerboseCallback() {
BASH_FRAMEWORK_ARGS_VERBOSE_OPTION='--verbose'
BASH_FRAMEWORK_ARGS_VERBOSE=${__VERBOSE_LEVEL_INFO}
BASH_FRAMEWORK_DISPLAY_LEVEL=${__LEVEL_INFO}
echo "BASH_FRAMEWORK_DISPLAY_LEVEL=${__LEVEL_INFO}" >> "${overrideEnvFile}"
}

# shellcheck disable=SC2317 # if function is overridden
optionDebugVerboseCallback() {
BASH_FRAMEWORK_ARGS_VERBOSE_OPTION='-vv'
BASH_FRAMEWORK_ARGS_VERBOSE=${__VERBOSE_LEVEL_DEBUG}
BASH_FRAMEWORK_DISPLAY_LEVEL=${__LEVEL_DEBUG}
echo "BASH_FRAMEWORK_DISPLAY_LEVEL=${__LEVEL_DEBUG}" >> "${overrideEnvFile}"
}

# shellcheck disable=SC2317 # if function is overridden
optionTraceVerboseCallback() {
BASH_FRAMEWORK_ARGS_VERBOSE_OPTION='-vvv'
BASH_FRAMEWORK_ARGS_VERBOSE=${__VERBOSE_LEVEL_TRACE}
BASH_FRAMEWORK_DISPLAY_LEVEL=${__LEVEL_DEBUG}
echo "BASH_FRAMEWORK_DISPLAY_LEVEL=${__LEVEL_DEBUG}" >> "${overrideEnvFile}"
}

getLevel() {
Expand All @@ -739,10 +749,10 @@ getLevel() {
OFF)
echo "${__LEVEL_OFF}"
;;
ERROR)
ERR | ERROR)
echo "${__LEVEL_ERROR}"
;;
WARNING)
WARN | WARNING)
echo "${__LEVEL_WARNING}"
;;
INFO)
Expand All @@ -763,7 +773,7 @@ getVerboseLevel() {
OFF)
echo "${__VERBOSE_LEVEL_OFF}"
;;
ERROR | WARNING | INFO)
ERR | ERROR | WARN | WARNING | INFO)
echo "${__VERBOSE_LEVEL_INFO}"
;;
DEBUG)
Expand All @@ -785,7 +795,7 @@ optionDisplayLevelCallback() {
logLevel="$(getLevel "${level}")"
verboseLevel="$(getVerboseLevel "${level}")"
BASH_FRAMEWORK_ARGS_VERBOSE=${verboseLevel}
BASH_FRAMEWORK_DISPLAY_LEVEL=${logLevel}
echo "BASH_FRAMEWORK_DISPLAY_LEVEL=${logLevel}" >> "${overrideEnvFile}"
}

# shellcheck disable=SC2317 # if function is overridden
Expand All @@ -795,18 +805,18 @@ optionLogLevelCallback() {
logLevel="$(getLevel "${level}")"
verboseLevel="$(getVerboseLevel "${level}")"
BASH_FRAMEWORK_ARGS_VERBOSE=${verboseLevel}
BASH_FRAMEWORK_LOG_LEVEL=${logLevel}
echo "BASH_FRAMEWORK_LOG_LEVEL=${logLevel}" >> "${overrideEnvFile}"
}

# shellcheck disable=SC2317 # if function is overridden
optionLogFileCallback() {
local logFile="$2"
BASH_FRAMEWORK_LOG_FILE="${logFile}"
echo "BASH_FRAMEWORK_LOG_FILE='${logFile}'" >> "${overrideEnvFile}"
}

# shellcheck disable=SC2317 # if function is overridden
optionQuietCallback() {
BASH_FRAMEWORK_QUIET_MODE=1
echo "BASH_FRAMEWORK_QUIET_MODE=1" >> "${overrideEnvFile}"
}

# shellcheck disable=SC2317 # if function is overridden
Expand Down Expand Up @@ -873,15 +883,24 @@ BASH_FRAMEWORK_LOG_FILE_MAX_ROTATION="${BASH_FRAMEWORK_LOG_FILE_MAX_ROTATION:-5}
EOF
)"

overrideEnvFile="$(Framework::createTempFile "overrideEnvFile")"

commandOptionParseFinished() {
# load default template framework config
# shellcheck disable=SC2034
defaultEnvFile="${PERSISTENT_TMPDIR}/.framework-config"
echo "${defaultFrameworkConfig}" > "${defaultEnvFile}"

Env::requireLoad "${defaultEnvFile}"
local -a files=("${defaultEnvFile}")
if [[ -f "${envFile}" ]]; then
files+=("${envFile}")
fi
# shellcheck disable=SC2154
if [[ -f "${optionBashFrameworkConfig}" ]]; then
files+=("${optionBashFrameworkConfig}")
fi
files+=("${overrideEnvFile}")
Env::requireLoad "${files[@]}"
Log::requireLoad

# shellcheck disable=SC2154
if [[ "${optionConfig}" = "1" ]]; then
displayConfig
fi
Expand Down Expand Up @@ -1086,15 +1105,15 @@ awkLintCommand() {
updateArgListQuietCallback "${options_parse_arg}"
;;
# Option 12/14
# Option optionLogLevel --log-level variableType String min 0 max 1 authorizedValues 'OFF|ERROR|WARNING|INFO|DEBUG|TRACE' regexp ''
# Option optionLogLevel --log-level variableType String min 0 max 1 authorizedValues 'OFF|ERR|ERROR|WARN|WARNING|INFO|DEBUG|TRACE' regexp ''
--log-level)
shift
if (($# == 0)); then
Log::displayError "Command ${SCRIPT_NAME} - Option ${options_parse_arg} - a value needs to be specified"
return 1
fi
if [[ ! "$1" =~ OFF|ERROR|WARNING|INFO|DEBUG|TRACE ]]; then
Log::displayError "Command ${SCRIPT_NAME} - Option ${options_parse_arg} - value '$1' is not part of authorized values(OFF|ERROR|WARNING|INFO|DEBUG|TRACE)"
if [[ ! "$1" =~ OFF|ERR|ERROR|WARN|WARNING|INFO|DEBUG|TRACE ]]; then
Log::displayError "Command ${SCRIPT_NAME} - Option ${options_parse_arg} - value '$1' is not part of authorized values(OFF|ERR|ERROR|WARN|WARNING|INFO|DEBUG|TRACE)"
return 1
fi
if ((options_parse_optionParsedCountOptionLogLevel >= 1)); then
Expand Down Expand Up @@ -1126,15 +1145,15 @@ awkLintCommand() {
updateArgListLogFileCallback "${options_parse_arg}" "${optionLogFile}"
;;
# Option 14/14
# Option optionDisplayLevel --display-level variableType String min 0 max 1 authorizedValues 'OFF|ERROR|WARNING|INFO|DEBUG|TRACE' regexp ''
# Option optionDisplayLevel --display-level variableType String min 0 max 1 authorizedValues 'OFF|ERR|ERROR|WARN|WARNING|INFO|DEBUG|TRACE' regexp ''
--display-level)
shift
if (($# == 0)); then
Log::displayError "Command ${SCRIPT_NAME} - Option ${options_parse_arg} - a value needs to be specified"
return 1
fi
if [[ ! "$1" =~ OFF|ERROR|WARNING|INFO|DEBUG|TRACE ]]; then
Log::displayError "Command ${SCRIPT_NAME} - Option ${options_parse_arg} - value '$1' is not part of authorized values(OFF|ERROR|WARNING|INFO|DEBUG|TRACE)"
if [[ ! "$1" =~ OFF|ERR|ERROR|WARN|WARNING|INFO|DEBUG|TRACE ]]; then
Log::displayError "Command ${SCRIPT_NAME} - Option ${options_parse_arg} - value '$1' is not part of authorized values(OFF|ERR|ERROR|WARN|WARNING|INFO|DEBUG|TRACE)"
return 1
fi
if ((options_parse_optionParsedCountOptionDisplayLevel >= 1)); then
Expand Down Expand Up @@ -1218,7 +1237,7 @@ Result in checkstyle format.")"
# shellcheck disable=SC2054
helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped)
echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")"
echo ' Default value: default'
echo " Default value: default"
echo ' Possible values: default|default-force|noColor'
echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}"
local -a helpArray
Expand All @@ -1240,7 +1259,7 @@ Result in checkstyle format.")"
# shellcheck disable=SC2054
helpArray=(Set\ log\ level)
echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")"
echo ' Possible values: OFF|ERROR|WARNING|INFO|DEBUG|TRACE'
echo ' Possible values: OFF|ERR|ERROR|WARN|WARNING|INFO|DEBUG|TRACE'
echo -e " ${__HELP_OPTION_COLOR}--log-file <String>${__HELP_NORMAL} {single}"
local -a helpArray
# shellcheck disable=SC2054
Expand All @@ -1251,7 +1270,7 @@ Result in checkstyle format.")"
# shellcheck disable=SC2054
helpArray=(set\ display\ level)
echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")"
echo ' Possible values: OFF|ERROR|WARNING|INFO|DEBUG|TRACE'
echo ' Possible values: OFF|ERR|ERROR|WARN|WARNING|INFO|DEBUG|TRACE'
echo
echo -n -e "${__HELP_TITLE_COLOR}VERSION: ${__RESET_COLOR}"
echo '1.0'
Expand Down
Loading

0 comments on commit abfdcaa

Please sign in to comment.