Adjust addOrEditKeyValPair to optionally take two or three arguments (adjust test to suit)

Add a removeKey function with test

update webpage.sh to reference functions in utils.sh (this can likely be abstracted/refactored further)

Signed-off-by: Adam Warner <me@adamwarner.co.uk>
pull/4653/head
Adam Warner 2 years ago
parent ff5e788889
commit 48138d32b6
No known key found for this signature in database
GPG Key ID: 872950F3ECF2B173

@ -15,7 +15,10 @@
# - New functions must have a test added for them in test/test_any_utils.py # - New functions must have a test added for them in test/test_any_utils.py
####################### #######################
# Takes three arguments key, value, and file. # Takes either
# - Three arguments: key, value, and file.
# - Two arguments: key, and file
#
# Checks the target file for the existence of the key # Checks the target file for the existence of the key
# - If it exists, it changes the value # - If it exists, it changes the value
# - If it does not exist, it adds the value # - If it does not exist, it adds the value
@ -25,15 +28,48 @@
####################### #######################
addOrEditKeyValPair() { addOrEditKeyValPair() {
local key="${1}" local key="${1}"
local value="${2}" local value
local file="${3}" local file
if grep -q "^${key}=" "${file}"; then
sed -i "/^${key}=/c\\${key}=${value}" "${file}" # If two arguments have been passed, then the second one is the file - there is no value
if [ $# -lt 3 ]; then
file="${2}"
else
value="${2}"
file="${3}"
fi
if [[ "${value}" != "" ]]; then
# value has a value, so it is a key pair
if grep -q "^${key}=" "${file}"; then
# Key already exists in file, modify the value
sed -i "/^${key}=/c\\${key}=${value}" "${file}"
else
# Key does not already exist, add it and it's value
echo "${key}=${value}" >> "${file}"
fi
else else
echo "${key}=${value}" >> "${file}" # value has no value, so it is just a key. Add it if it does not already exist
if ! grep -q "^${key}" "${file}"; then
# Key does not exist, add it.
echo "${key}" >> "${file}"
fi
fi fi
} }
#######################
# Takes two arguments key, and file.
# Deletes a key from target file
#
# Example usage:
# removeKey "PIHOLE_DNS_1" "/etc/pihole/setupVars.conf"
#######################
removeKey() {
local key="${1}"
local file="${2}"
sed -i "/^${key}/d" "${file}"
}
####################### #######################
# returns FTL's current telnet API port # returns FTL's current telnet API port
####################### #######################

@ -26,6 +26,9 @@ readonly PI_HOLE_FILES_DIR="/etc/.pihole"
PH_TEST="true" PH_TEST="true"
source "${PI_HOLE_FILES_DIR}/automated install/basic-install.sh" source "${PI_HOLE_FILES_DIR}/automated install/basic-install.sh"
readonly utilsfile="/opt/pihole/utils.sh"
source "${utilsfile}"
coltable="/opt/pihole/COL_TABLE" coltable="/opt/pihole/COL_TABLE"
if [[ -f ${coltable} ]]; then if [[ -f ${coltable} ]]; then
source ${coltable} source ${coltable}
@ -51,41 +54,35 @@ Options:
} }
add_setting() { add_setting() {
echo "${1}=${2}" >> "${setupVars}" addOrEditKeyValPair "${1}" "${2}" "${setupVars}"
} }
delete_setting() { delete_setting() {
sed -i "/^${1}/d" "${setupVars}" removeKey "${1}" "${setupVars}"
} }
change_setting() { change_setting() {
delete_setting "${1}" addOrEditKeyValPair "${1}" "${2}" "${setupVars}"
add_setting "${1}" "${2}"
} }
addFTLsetting() { addFTLsetting() {
echo "${1}=${2}" >> "${FTLconf}" addOrEditKeyValPair "${1}" "${2}" "${FTLconf}"
} }
deleteFTLsetting() { deleteFTLsetting() {
sed -i "/^${1}/d" "${FTLconf}" removeKey "${1}" "${FTLconf}"
} }
changeFTLsetting() { changeFTLsetting() {
deleteFTLsetting "${1}" addOrEditKeyValPair "${1}" "${2}" "${FTLconf}"
addFTLsetting "${1}" "${2}"
} }
add_dnsmasq_setting() { add_dnsmasq_setting() {
if [[ "${2}" != "" ]]; then addOrEditKeyValPair "${1}" "${2}" "${dnsmasqconfig}"
echo "${1}=${2}" >> "${dnsmasqconfig}"
else
echo "${1}" >> "${dnsmasqconfig}"
fi
} }
delete_dnsmasq_setting() { delete_dnsmasq_setting() {
sed -i "/^${1}/d" "${dnsmasqconfig}" removeKey "${1}" "${dnsmasqconfig}"
} }
SetTemperatureUnit() { SetTemperatureUnit() {
@ -183,7 +180,7 @@ ProcessDNSSettings() {
fi fi
delete_dnsmasq_setting "dnssec" delete_dnsmasq_setting "dnssec"
delete_dnsmasq_setting "trust-anchor=" delete_dnsmasq_setting "trust-anchor"
if [[ "${DNSSEC}" == true ]]; then if [[ "${DNSSEC}" == true ]]; then
echo "dnssec echo "dnssec

@ -6,11 +6,28 @@ def test_key_val_replacement_works(host):
addOrEditKeyValPair "KEY_TWO" "value2" "./testoutput" addOrEditKeyValPair "KEY_TWO" "value2" "./testoutput"
addOrEditKeyValPair "KEY_ONE" "value3" "./testoutput" addOrEditKeyValPair "KEY_ONE" "value3" "./testoutput"
addOrEditKeyValPair "KEY_FOUR" "value4" "./testoutput" addOrEditKeyValPair "KEY_FOUR" "value4" "./testoutput"
addOrEditKeyValPair "KEY_FIVE_NO_VALUE" "./testoutput"
addOrEditKeyValPair "KEY_FIVE_NO_VALUE" "./testoutput"
''') ''')
output = host.run(''' output = host.run('''
cat ./testoutput cat ./testoutput
''') ''')
expected_stdout = 'KEY_ONE=value3\nKEY_TWO=value2\nKEY_FOUR=value4\n' expected_stdout = 'KEY_ONE=value3\nKEY_TWO=value2\nKEY_FOUR=value4\nKEY_FIVE_NO_VALUE\n'
assert expected_stdout == output.stdout
def test_key_val_removal_works(host):
''' Confirms addOrEditKeyValPair provides the expected output '''
host.run('''
source /opt/pihole/utils.sh
addOrEditKeyValPair "KEY_ONE" "value1" "./testoutput"
addOrEditKeyValPair "KEY_TWO" "value2" "./testoutput"
addOrEditKeyValPair "KEY_THREE" "value3" "./testoutput"
removeKey "KEY_TWO" "./testoutput"
''')
output = host.run('''
cat ./testoutput
''')
expected_stdout = 'KEY_ONE=value1\nKEY_THREE=value3\n'
assert expected_stdout == output.stdout assert expected_stdout == output.stdout

Loading…
Cancel
Save