Introduce elision by removing unnecessary quoting

This commit is contained in:
Rezart Qelibari
2022-02-15 16:50:01 +01:00
parent 8b995d0938
commit 3a3324e1c5
2 changed files with 41 additions and 32 deletions

View File

@@ -10,47 +10,47 @@ function ensureDocopts() {
} }
function autoloadZShLib() { function autoloadZShLib() {
export ASTZWEIG_ZSHLIB="${_DIR}/zshlib" export ASTZWEIG_ZSHLIB=${_DIR}/zshlib
FPATH="${ASTZWEIG_ZSHLIB}:${FPATH}" FPATH="${ASTZWEIG_ZSHLIB}:${FPATH}"
local funcNames=("${(@f)$(find "${ASTZWEIG_ZSHLIB}" -type f -perm +u=x -maxdepth 1 | awk -F/ '{ print $NF }')}") local funcNames=(${(f)"$(find "${ASTZWEIG_ZSHLIB}" -type f -perm +u=x -maxdepth 1 | awk -F/ '{ print $NF }')"})
autoload -Uz "${funcNames[@]}" autoload -Uz ${funcNames}
} }
function configureLogging() { function configureLogging() {
local output=tostdout level=info local output=tostdout level=info
[ -n "${logfile}" ] && output="${logfile}" [ -n "${logfile}" ] && output=${logfile}
[ "${verbose}" = true ] && level=debug [ "${verbose}" = true ] && level=debug
lop setoutput -l "${level}" "${output}" lop setoutput -l ${level} ${output}
} }
function filterModules() { function filterModules() {
if [ "${#module}" -eq 0 ]; then if [ ${#module} -eq 0 ]; then
lop debug 'No modules given as arguments. Taking all modules.' lop debug 'No modules given as arguments. Taking all modules.'
modulesToInstall=("${allModules[@]}") modulesToInstall=(${allModules})
else else
lop debug "Given ${#module} modules as arguments: ${module}" lop debug "Given ${#module} modules as arguments: ${module}"
[ "${inverse}" = true ] && lop debug 'Taking complement set.' [ "${inverse}" = true ] && lop debug 'Taking complement set.'
local mod pattern="^.*(${(j.|.)module})\$" local mod pattern="^.*(${(j.|.)module})\$"
modulesToInstall=() modulesToInstall=()
for mod in "${allModules[@]}"; do for mod in ${allModules}; do
local found=false local found=false
[[ "${mod}" =~ ${pattern} ]] && found=true [[ ${mod} =~ ${pattern} ]] && found=true
lop debug "Was ${mod} found in ${pattern}: ${found}" lop debug "Was ${mod} found in ${pattern}: ${found}"
if [ "${inverse}" != 'true' -a "${found}" = true ]; then if [ "${inverse}" != 'true' -a ${found} = true ]; then
lop debug "Adding module ${mod}" lop debug "Adding module ${mod}"
modulesToInstall+=("${mod}") modulesToInstall+=(${mod})
elif [ "${inverse}" = 'true' -a "${found}" = false ]; then elif [ "${inverse}" = 'true' -a ${found} = false ]; then
lop debug "Adding module ${mod}" lop debug "Adding module ${mod}"
modulesToInstall+=("${mod}") modulesToInstall+=(${mod})
fi fi
done done
fi fi
} }
function runModule() { function runModule() {
local mod="$1" local mod=$1
shift shift
"${mod}" "$@" ${mod} "$@"
} }
function parseQuestionLine() { function parseQuestionLine() {
@@ -59,30 +59,30 @@ function parseQuestionLine() {
[ -z "${line}" ] && return [ -z "${line}" ] && return
[ "${line[2]}" != ':' ] && return 10 [ "${line[2]}" != ':' ] && return 10
questionType="${typeMap[${line[1]}]}" questionType=$typeMap[${line[1]}]
[ -z "${questionType}" ] && return 11 [ -z "${questionType}" ] && return 11
# remove question type # remove question type
[ "${line[3]}" = ' ' ] && line="${line:3}" || line="${line:2}" [ "${line[3]}" = ' ' ] && line=${line:3} || line=${line:2}
line=("${(s.=.)line[@]}") line=(${(s.=.)line[@]})
parameterName="${line[1]}" parameterName=${line[1]}
[ -z "${parameterName}" ] && return 12 [ -z "${parameterName}" ] && return 12
[ "${parameterName[1]}" = '-' ] && return 13 [ "${parameterName[1]}" = - ] && return 13
# remove parameter name # remove parameter name
line="${(j.=.)${(@)line:1}}" line="${(j.=.)${(@)line:1}}"
line=("${(s. #.)line}") line=("${(s. #.)line}")
question="${line[1]}" question=${line[1]}
[ -z "${question}" ] && return 14 [ -z "${question}" ] && return 14
# remove question part # remove question part
line="${(j. #.)${(@)line:1}}" line="${(j. #.)${(@)line:1}}"
if [ -n "${line}" ]; then if [ -n "${line}" ]; then
arguments=("${(s.;.)line}") arguments=(${(s.;.)line})
for arg in ${arguments[@]}; do for arg in ${arguments}; do
arg=("${(s.:.)arg}") arg=("${(s.:.)arg}")
[ -z "${arg[1]}" ] && return 15 [ -z "${arg[1]}" ] && return 15
arg[1]="`trim "${arg[1]}"`" arg[1]="`trim "${arg[1]}"`"
@@ -98,7 +98,7 @@ function parseQuestionLine() {
function populateQuestionsWithModuleRequiredInformation() { function populateQuestionsWithModuleRequiredInformation() {
lop debug "Asking ${mod} for required information" lop debug "Asking ${mod} for required information"
for line in "${(f)$(runModule "${mod}" show-questions)}"; do for line in ${(f)"$(runModule "${mod}" show-questions)"}; do
lop debug "Says line: ${line}" lop debug "Says line: ${line}"
parseQuestionLine parseQuestionLine
lop debug "Parsing question returned status: $?" lop debug "Parsing question returned status: $?"
@@ -109,7 +109,7 @@ function populateQuestionsWithModuleRequiredInformation() {
function findQuestionArgInInstruction() { function findQuestionArgInInstruction() {
local argNameToLookup="$1" arg name value local argNameToLookup="$1" arg name value
[ -z "${argNameToLookup}" ] && return [ -z "${argNameToLookup}" ] && return
for arg in ${instructions[@]}; do for arg in ${instructions}; do
arg=("${(s.:.)arg}") arg=("${(s.:.)arg}")
[ "${#arg}" -lt 2 ] && continue [ "${#arg}" -lt 2 ] && continue
name="${arg[1]}" name="${arg[1]}"
@@ -166,7 +166,7 @@ function generateConfigKeysFromQuestionID() {
function answerQuestionsFromConfigOrAskUser() { function answerQuestionsFromConfigOrAskUser() {
local questionID local questionID
for questionID in "${(k)questions[@]}"; do for questionID in ${(k)questions}; do
local value configkeys=() local value configkeys=()
lop debug "Answering question with ID: ${questionID}" lop debug "Answering question with ID: ${questionID}"
generateConfigKeysFromQuestionID "${mod}" "${questionID}" generateConfigKeysFromQuestionID "${mod}" "${questionID}"
@@ -191,7 +191,7 @@ function askNecessaryQuestions() {
lop debug "Config only option given with value: ${config_only}" lop debug "Config only option given with value: ${config_only}"
config setconfigfile "${config_only}" config setconfigfile "${config_only}"
fi fi
for mod in ${modulesToInstall[@]}; do for mod in ${modulesToInstall}; do
local -A questions=() local -A questions=()
populateQuestionsWithModuleRequiredInformation populateQuestionsWithModuleRequiredInformation
answerQuestionsFromConfigOrAskUser answerQuestionsFromConfigOrAskUser
@@ -200,7 +200,7 @@ function askNecessaryQuestions() {
function printModulesToInstall() { function printModulesToInstall() {
lop section 'Modules that will install are:' lop section 'Modules that will install are:'
for mod in "${modulesToInstall[@]}"; do for mod in "${modulesToInstall}"; do
hio info "${mod}" hio info "${mod}"
done | abbreviatePaths done | abbreviatePaths
exit 0 exit 0
@@ -210,8 +210,8 @@ function loadModules() {
local mod local mod
modpath=("${_DIR}/modules" "${modpath[@]}") modpath=("${_DIR}/modules" "${modpath[@]}")
lop debug "Module paths are: ${modpath[@]}" lop debug "Module paths are: ${modpath[@]}"
allModules=("${(f)$(find "${modpath[@]}" -type f -perm +u=x -maxdepth 1 2> /dev/null | sort -n)}") allModules=(${(f)"$(find "${modpath[@]}" -type f -perm +u=x -maxdepth 1 2> /dev/null | sort -n)"})
for mod in "${allModules[@]}"; do for mod in ${allModules}; do
lop debug "Found module ${mod}" lop debug "Found module ${mod}"
done done
filterModules filterModules
@@ -236,7 +236,7 @@ function generateModuleOptions() {
function installModules() { function installModules() {
local mod moduleOptions local mod moduleOptions
for mod in "${modulesToInstall[@]}"; do for mod in ${modulesToInstall}; do
generateModuleOptions generateModuleOptions
runModule "${mod}" ${moduleOptions} runModule "${mod}" ${moduleOptions}
done done

View File

@@ -53,9 +53,18 @@ Describe 'parseQuestionLine'
The status should be success The status should be success
End End
It 'ignores empty arguments'
declare -A questions
line='i: parameter-name=What parameter do you like? # some arg: some value;;'
When call parseQuestionLine
The output should eq ''
The line 1 of variable 'questions[parameter-name]' should eq 'What parameter do you like?'
The status should eq 0
End
It 'does nothing if an argument does not contain a name' It 'does nothing if an argument does not contain a name'
declare -A questions declare -A questions
line='i: parameter-name=What parameter do you like? # some arg = some value;;' line='i: parameter-name=What parameter do you like? # some arg: some value;:some value;'
When call parseQuestionLine When call parseQuestionLine
The output should eq '' The output should eq ''
The variable 'questions' should eq '' The variable 'questions' should eq ''