refactor: shell optimizations

This commit is contained in:
Zac
2023-07-23 11:35:27 -05:00
committed by GitHub
parent 1c0c30e6d6
commit 79baded01d
11 changed files with 112 additions and 167 deletions

View File

@@ -12,23 +12,25 @@ NLS=workbench.settings.enableNaturalLanguageSearch
. ../utils.sh
update_setting () {
local FILENAME="${2}"
local FILENAME SETTING LINE_NUM IN_SETTING FOUND DEFAULT_TRUE_TO_FALSE
FILENAME="${2}"
# check that the file exists
if [ ! -f "${FILENAME}" ]; then
if [[ ! -f "${FILENAME}" ]]; then
echo "File to update setting in does not exist ${FILENAME}"
return
fi
# go through lines of file, looking for block that contains setting
local SETTING="${1}"
local LINE_NUM=0
SETTING="${1}"
LINE_NUM=0
while read -r line; do
local LINE_NUM=$(( LINE_NUM + 1 ))
LINE_NUM=$(( LINE_NUM + 1 ))
if [[ "${line}" == *"${SETTING}"* ]]; then
local IN_SETTING=1
IN_SETTING=1
fi
if [[ ("${line}" == *"${DEFAULT_TRUE}"* || "${line}" == *"${DEFAULT_ON}"*) && "${IN_SETTING}" == "1" ]]; then
local FOUND=1
FOUND=1
break
fi
done < "${FILENAME}"
@@ -40,9 +42,9 @@ update_setting () {
# construct line-aware replacement string
if [[ "${line}" == *"${DEFAULT_TRUE}"* ]]; then
local DEFAULT_TRUE_TO_FALSE="${LINE_NUM}s/${DEFAULT_TRUE}/${DEFAULT_FALSE}/"
DEFAULT_TRUE_TO_FALSE="${LINE_NUM}s/${DEFAULT_TRUE}/${DEFAULT_FALSE}/"
else
local DEFAULT_TRUE_TO_FALSE="${LINE_NUM}s/${DEFAULT_ON}/${DEFAULT_OFF}/"
DEFAULT_TRUE_TO_FALSE="${LINE_NUM}s/${DEFAULT_ON}/${DEFAULT_OFF}/"
fi
replace "${DEFAULT_TRUE_TO_FALSE}" "${FILENAME}"