Skip to content

Commit

Permalink
Refactoring to remove duplicated code
Browse files Browse the repository at this point in the history
- move conf/.env to conf/defaultEnv/.env in order to
  be able to include the .env file using gtpl
  it avoids usage of @embed feature removing the
  need of tar command in BashTools::Conf::requireLoad
- added
  src/_binaries/commandDefinitions/optionsDefault.yaml
  to set default binary options for all the binaries
  beforeParseCallback calling
  defaultBeforeParseCallback
- avoid duplicated code by using the new
  fromDsnOptionLongDescription function
- reformatted long description for all Database's
  binaries
- better optionProfile default value management
  that can be default or empty depending the case
- added
  src/_binaries/commandDefinitions/optionsTables.sh
  src/_binaries/commandDefinitions/optionsRatio.yaml
  in order to correctly order the options in help
- move requirements to beforeParseCallback
- recompiled all binaries to remove deprecated
  option --env-file
  • Loading branch information
fchastanet committed Sep 8, 2024
1 parent a43bd94 commit babaf94
Show file tree
Hide file tree
Showing 94 changed files with 3,088 additions and 2,253 deletions.
214 changes: 99 additions & 115 deletions bin/cli
Original file line number Diff line number Diff line change
Expand Up @@ -352,18 +352,79 @@ Bash::handlePipelineFailure() {
}


read -r -d '\0' bashToolsDefaultConfigTemplate <<-EOM || true
#
# shellcheck disable=SC2034
# Default settings
# you can override these settings by creating ${HOME}/.bash-tools/.env file
###
### DISPLAY Level
### minimum level of the messages that will be displayed on screen
###
### 0: NO LOG
### 1: ERROR
### 2: WARNING
### 3: INFO
### 4: DEBUG
###
BASH_FRAMEWORK_DISPLAY_LEVEL=${BASH_FRAMEWORK_DISPLAY_LEVEL:-3}
###
### DISPLAY duration
### 0: no duration is displayed on the messages
### 1: duration between previous message and current is displayed
### with the message
###
DISPLAY_DURATION=${DISPLAY_DURATION:0}
###
### Log to file
###
### all log messages will be redirected to log file specified
### this same path will be used inside and outside of the container
###
BASH_FRAMEWORK_LOG_FILE=${BASH_FRAMEWORK_LOG_FILE:-${FRAMEWORK_ROOT_DIR}/logs/bash.log}
###
### LOG Level
### minimum level of the messages that will be logged into LOG_FILE
###
### 0: NO LOG
### 1: ERROR
### 2: WARNING
### 3: INFO
### 4: DEBUG
###
BASH_FRAMEWORK_LOG_LEVEL=${BASH_FRAMEWORK_LOG_LEVEL:-0}
# absolute directory containing db import sql dumps
DB_IMPORT_DUMP_DIR=${DB_IMPORT_DUMP_DIR:-${HOME}/.bash-tools/dbImportDumps}
# garbage collect all files for which modification is greater than eg: 30 days (+30)
# each time an existing file is used by dbImport/dbImportTable
# the file modification time is set to now
DB_IMPORT_GARBAGE_COLLECT_DAYS=${DB_IMPORT_GARBAGE_COLLECT_DAYS:-+30}
# absolute directory containing dbScripts used by dbScriptAllDatabases
SCRIPTS_FOLDER=${SCRIPTS_FOLDER:-${HOME}/.bash-tools/conf/dbScripts}
# -----------------------------------------------------
# AWS Parameters
# -----------------------------------------------------
S3_BASE_URL=${S3_BASE_URL:-}
# -----------------------------------------------------
# Postman Parameters
# -----------------------------------------------------
POSTMAN_API_KEY=
EOM

# @description loads ~/.bash-tools/.env if available
# if not creates it from a default template
# else check if new options need to be added
BashTools::Conf::requireLoad() {
Linux::requireTarCommand
Compiler::Embed::extractFileFromBase64 \
"${PERSISTENT_TMPDIR:-/tmp}/db87222729e0f1ed9c597a486a61a08c/bashToolsDefaultConfigTemplate" \
"IyEvdXNyL2Jpbi9lbnYgYmFzaAojIHNoZWxsY2hlY2sgZGlzYWJsZT1TQzIwMzQKCiMgRGVmYXVsdCBzZXR0aW5ncwojIHlvdSBjYW4gb3ZlcnJpZGUgdGhlc2Ugc2V0dGluZ3MgYnkgY3JlYXRpbmcgJHtIT01FfS8uYmFzaC10b29scy8uZW52IGZpbGUKCiMjIwojIyMgRElTUExBWSBMZXZlbAojIyMgbWluaW11bSBsZXZlbCBvZiB0aGUgbWVzc2FnZXMgdGhhdCB3aWxsIGJlIGRpc3BsYXllZCBvbiBzY3JlZW4KIyMjCiMjIyAwOiBOTyBMT0cKIyMjIDE6IEVSUk9SCiMjIyAyOiBXQVJOSU5HCiMjIyAzOiBJTkZPCiMjIyA0OiBERUJVRwojIyMKQkFTSF9GUkFNRVdPUktfR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emplate="${PERSISTENT_TMPDIR:-/tmp}/db87222729e0f1ed9c597a486a61a08c/bashToolsDefaultConfigTemplate"

BASH_TOOLS_ROOT_DIR="$(cd "${CURRENT_DIR}/${RELATIVE_FRAMEWORK_DIR_TO_CURRENT_DIR}" && pwd -P)"
if [[ -d "${BASH_TOOLS_ROOT_DIR}/vendor/bash-tools-framework/" ]]; then
FRAMEWORK_ROOT_DIR="$(cd "${BASH_TOOLS_ROOT_DIR}/vendor/bash-tools-framework" && pwd -P)"
Expand Down Expand Up @@ -391,7 +452,7 @@ declare -gx embed_file_bashToolsDefaultConfigTemplate="${PERSISTENT_TMPDIR:-/tmp
(
echo "#!/usr/bin/env bash"
# shellcheck disable=SC2154
echo "${embed_file_bashToolsDefaultConfigTemplate}"
echo "${bashToolsDefaultConfigTemplate}"
) >"${envFile}"
Log::displayInfo "Configuration file '${envFile}' created"
else
Expand All @@ -404,44 +465,14 @@ declare -gx embed_file_bashToolsDefaultConfigTemplate="${PERSISTENT_TMPDIR:-/tmp
) >>"${envFile}"
fi
fi
# shellcheck source=/conf/.env
# shellcheck source=/conf/defaultEnv/.env
source "${envFile}" || {
Log::displayError "impossible to load '${envFile}'"
exit 1
}
}


# @description convert base64 encoded back to target file
# if target file is executable prepend dir of target
# file to PATH to make binary available everywhere
# it is advised to include in the path of the target file
# the md5sum of the binFile
#
# @arg $1 targetFile:String the file to write
# @arg $2 binFileBase64:String the base64 encoded file
# @arg $3 fileMode:String the chmod to set on the file
# @set PATH String prepend target embedded file binary directory to PATH variable if binary executable
Compiler::Embed::extractFileFromBase64() {
local targetFile="$1"
local binFileBase64="$2"
local fileMode="${3:-+x}"
local targetDir="${targetFile%/*}"

if [[ ! -f "${targetFile}" ]]; then
if [[ ! -d "${targetDir}" ]]; then
mkdir -p "${targetDir}"
fi
base64 -d >"${targetFile}" <<<"${binFileBase64}"
chmod "${fileMode}" "${targetFile}"
fi

if [[ -x "${targetFile}" ]]; then
Env::pathPrepend "${targetDir}"
fi
}


# @description get absolute conf file from specified conf folder deduced using these rules
# * from absolute file (ignores <confFolder> and <extension>)
# * relative to where script is executed (ignores <confFolder> and <extension>)
Expand Down Expand Up @@ -680,19 +711,6 @@ Db::checkRequirements() {
}


# @description prepend directories to the PATH environment variable
# @arg $@ args:String[] list of directories to prepend
# @set PATH update PATH with the directories prepended
Env::pathPrepend() {
local arg
for arg in "$@"; do
if [[ -d "${arg}" && ":${PATH}:" != *":${arg}:"* ]]; then
PATH="$(realpath "${arg}"):${PATH}"
fi
done
}


# @description ensure env files are loaded
# @arg $@ list of default files to load at the end
# @exitcode 1 if one of env files fails to load
Expand Down Expand Up @@ -765,14 +783,6 @@ Linux::requireRealpathCommand() {
}


# @description ensure command tar is available
# @exitcode 1 if tar command not available
# @stderr diagnostics information is displayed
Linux::requireTarCommand() {
Assert::commandExists tar
}


declare -g FIRST_LOG_DATE LOG_LAST_LOG_DATE LOG_LAST_LOG_DATE_INIT LOG_LAST_DURATION_STR
FIRST_LOG_DATE="${EPOCHREALTIME/[^0-9]/}"
LOG_LAST_LOG_DATE="${FIRST_LOG_DATE}"
Expand Down Expand Up @@ -1295,16 +1305,6 @@ optionVersionCallback() {
exit 0
}

# shellcheck disable=SC2317 # if function is overridden
optionEnvFileCallback() {
local envFile="$2"
Log::displayWarning "Command ${SCRIPT_NAME} - Option --env-file is deprecated and will be removed in the future"
if [[ ! -f "${envFile}" || ! -r "${envFile}" ]]; then
Log::displayError "Command ${SCRIPT_NAME} - Option --env-file - File '${envFile}' doesn't exist"
exit 1
fi
}

# shellcheck disable=SC2317 # if function is overridden
optionInfoVerboseCallback() {
BASH_FRAMEWORK_ARGS_VERBOSE_OPTION='--verbose'
Expand Down Expand Up @@ -1497,6 +1497,19 @@ commandOptionParseFinished() {



defaultBeforeParseCallback() {
BashTools::Conf::requireLoad
Env::requireLoad
UI::requireTheme
Log::requireLoad
}

beforeParseCallback() {
defaultBeforeParseCallback
}



# option values
# shellcheck disable=SC2034
declare containerArg="default"
Expand All @@ -1510,10 +1523,7 @@ declare PROFILES_DIR
declare HOME_PROFILES_DIR

beforeParseCallback() {
BashTools::Conf::requireLoad
Env::requireLoad
UI::requireTheme
Log::requireLoad
defaultBeforeParseCallback
Linux::requireRealpathCommand
Assert::commandExists docker "check https://docs.docker.com/engine/install/ubuntu/"
}
Expand Down Expand Up @@ -1626,7 +1636,6 @@ declare optionBashFrameworkConfig=""
declare optionInfoVerbose="0"
declare optionDebugVerbose="0"
declare optionTraceVerbose="0"
declare -a optionEnvFiles=()
declare optionLogLevel=""
declare optionLogFile=""
declare optionDisplayLevel=""
Expand Down Expand Up @@ -1660,7 +1669,6 @@ cliCommandParse() {
optionTraceVerbose="0"
local -i options_parse_optionParsedCountOptionTraceVerbose
((options_parse_optionParsedCountOptionTraceVerbose = 0)) || true

optionLogLevel=""
local -i options_parse_optionParsedCountOptionLogLevel
((options_parse_optionParsedCountOptionLogLevel = 0)) || true
Expand Down Expand Up @@ -1702,7 +1710,7 @@ cliCommandParse() {
local options_parse_arg="$1"
local argOptDefaultBehavior=0
case "${options_parse_arg}" in
# Option 1/14
# Option 1/13
# optionHelp alts --help|-h
# type: Boolean min 0 max 1
--help | -h)
Expand All @@ -1718,7 +1726,7 @@ cliCommandParse() {

;;

# Option 2/14
# Option 2/13
# optionConfig alts --config
# type: Boolean min 0 max 1
--config)
Expand All @@ -1732,7 +1740,7 @@ cliCommandParse() {
((++options_parse_optionParsedCountOptionConfig))
;;

# Option 3/14
# Option 3/13
# optionBashFrameworkConfig alts --bash-framework-config
# type: String min 0 max 1
--bash-framework-config)
Expand All @@ -1753,7 +1761,7 @@ cliCommandParse() {

;;

# Option 4/14
# Option 4/13
# optionInfoVerbose alts --verbose|-v
# type: Boolean min 0 max 1
--verbose | -v)
Expand All @@ -1771,7 +1779,7 @@ cliCommandParse() {

;;

# Option 5/14
# Option 5/13
# optionDebugVerbose alts -vv
# type: Boolean min 0 max 1
-vv)
Expand All @@ -1789,7 +1797,7 @@ cliCommandParse() {

;;

# Option 6/14
# Option 6/13
# optionTraceVerbose alts -vvv
# type: Boolean min 0 max 1
-vvv)
Expand All @@ -1807,25 +1815,7 @@ cliCommandParse() {

;;

# Option 7/14
# optionEnvFiles alts --env-file
# type: StringArray min 0 max -1
--env-file)
shift
if (($# == 0)); then
Log::displayError "Command ${SCRIPT_NAME} - Option ${options_parse_arg} - a value needs to be specified"
return 1
fi

((++options_parse_optionParsedCountOptionEnvFiles))
optionEnvFiles+=("$1")
optionEnvFileCallback "${options_parse_arg}" "${optionEnvFiles[@]}"

updateArgListEnvFileCallback "${options_parse_arg}" "${optionEnvFiles[@]}"

;;

# Option 8/14
# Option 7/13
# optionLogLevel alts --log-level
# type: String min 0 max 1
# authorizedValues: OFF|ERR|ERROR|WARN|WARNING|INFO|DEBUG|TRACE
Expand Down Expand Up @@ -1853,7 +1843,7 @@ cliCommandParse() {

;;

# Option 9/14
# Option 8/13
# optionLogFile alts --log-file
# type: String min 0 max 1
--log-file)
Expand All @@ -1876,7 +1866,7 @@ cliCommandParse() {

;;

# Option 10/14
# Option 9/13
# optionDisplayLevel alts --display-level
# type: String min 0 max 1
# authorizedValues: OFF|ERR|ERROR|WARN|WARNING|INFO|DEBUG|TRACE
Expand Down Expand Up @@ -1904,7 +1894,7 @@ cliCommandParse() {

;;

# Option 11/14
# Option 10/13
# optionNoColor alts --no-color
# type: Boolean min 0 max 1
--no-color)
Expand All @@ -1922,7 +1912,7 @@ cliCommandParse() {

;;

# Option 12/14
# Option 11/13
# optionTheme alts --theme
# type: String min 0 max 1
# authorizedValues: default|default-force|noColor
Expand Down Expand Up @@ -1950,7 +1940,7 @@ cliCommandParse() {

;;

# Option 13/14
# Option 12/13
# optionVersion alts --version
# type: Boolean min 0 max 1
--version)
Expand All @@ -1966,7 +1956,7 @@ cliCommandParse() {

;;

# Option 14/14
# Option 13/13
# optionQuiet alts --quiet|-q
# type: Boolean min 0 max 1
--quiet | -q)
Expand Down Expand Up @@ -2081,7 +2071,7 @@ cliCommandHelp() {
# ------------------------------------------
# usage/options section
# ------------------------------------------
optionsAltList=("[--help|-h]" "[--config]" "[--bash-framework-config <bash-framework-config>]" "[--verbose|-v]" "[-vv]" "[-vvv]" "[--env-file <env-file>]" "[--log-level <log-level>]" "[--log-file <log-file>]" "[--display-level <display-level>]" "[--no-color]" "[--theme <theme>]" "[--version]" "[--quiet|-q]"
optionsAltList=("[--help|-h]" "[--config]" "[--bash-framework-config <bash-framework-config>]" "[--verbose|-v]" "[-vv]" "[-vvv]" "[--log-level <log-level>]" "[--log-file <log-file>]" "[--display-level <display-level>]" "[--no-color]" "[--theme <theme>]" "[--version]" "[--quiet|-q]"
)
Array::wrap2 " " 80 2 "${__HELP_TITLE_COLOR}USAGE:${__RESET_COLOR}" \
"cli" "${optionsAltList[@]}"
Expand Down Expand Up @@ -2146,12 +2136,6 @@ cliCommandHelp() {



echo -e " ${__HELP_OPTION_COLOR}--env-file <env-file>${__HELP_NORMAL} {list} (optional)"
Array::wrap2 ' ' 76 4 " " "Load the specified env file (deprecated, please use --bash-framework-config option instead)"
echo



echo -e " ${__HELP_OPTION_COLOR}--log-level <log-level>${__HELP_NORMAL} {single}"
Array::wrap2 ' ' 76 4 " " "Set log level"
echo
Expand Down
Loading

0 comments on commit babaf94

Please sign in to comment.