From 4fd0f15d903472e2e56ee3604a16d48d982ab01b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Thu, 16 Feb 2023 15:21:18 +0100 Subject: [PATCH] Ignore commented lines when reding PRIVACYLEVEL from config file Create dedicated getVal function in utils.sh as it might be useful somewhere else Account for tailing comments and $key not being on the first line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- advanced/Scripts/utils.sh | 22 ++++++++++++++++++++-- automated install/basic-install.sh | 3 ++- test/test_any_utils.py | 16 ++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/advanced/Scripts/utils.sh b/advanced/Scripts/utils.sh index 37516472..a1178265 100755 --- a/advanced/Scripts/utils.sh +++ b/advanced/Scripts/utils.sh @@ -44,7 +44,7 @@ addOrEditKeyValPair() { } ####################### -# Takes two arguments: file, and key. +# Takes two arguments: file and key. # Adds a key to target file # # Example usage: @@ -64,7 +64,7 @@ addKey(){ } ####################### -# Takes two arguments: file, and key. +# Takes two arguments: file and key. # Deletes a key or key/value pair from target file # # Example usage: @@ -76,6 +76,24 @@ removeKey() { sed -i "/^${key}/d" "${file}" } +####################### +# Takes two arguments: file and key. +# Returns the value of a given key from target file +# - ignores all commented lines +# - only returns the first value if multiple identical keys exist +# +# +# Example usage: +# getVal "/etc/pihole/setupVars.conf" "PIHOLE_DNS_1" +####################### +getVal() { + local file="${1}" + local key="${2}" + local value + value=$(sed -e '/^[[:blank:]]*#/d' "${file}" | grep "${key}" | awk -F "=" 'NR==1{printf$2}') + printf "%s" "$value" +} + ####################### # returns FTL's current telnet API port based on the setting in /etc/pihole-FTL.conf diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index 05bc0e4e..a9398d90 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -2612,7 +2612,8 @@ main() { # Get the privacy level if it exists (default is 0) if [[ -f "${FTL_CONFIG_FILE}" ]]; then - PRIVACY_LEVEL=$(sed -ne 's/PRIVACYLEVEL=\(.*\)/\1/p' "${FTL_CONFIG_FILE}") + # use getVal from utils.sh to get PRIVACYLEVEL + PRIVACY_LEVEL=$(getVal "${FTL_CONFIG_FILE}" "PRIVACYLEVEL") # If no setting was found, default to 0 PRIVACY_LEVEL="${PRIVACY_LEVEL:-0}" diff --git a/test/test_any_utils.py b/test/test_any_utils.py index 5b4075d9..6c920161 100644 --- a/test/test_any_utils.py +++ b/test/test_any_utils.py @@ -62,6 +62,22 @@ def test_key_removal_works(host): assert expected_stdout == output.stdout +def test_get_value_works(host): + """Confirms getVal returns the correct value for a given key""" + output = host.run( + """ + source /opt/pihole/utils.sh + echo "Somekey=xxx" >> /tmp/testfile + echo "#Testkey=1234" >> /tmp/testfile + echo "Testkey=5678" >> /tmp/testfile + echo "Testkey=abcd" >> /tmp/testfile + getVal "/tmp/testfile" "Testkey" + """ + ) + expected_stdout = "5678" + assert expected_stdout == output.stdout + + def test_getFTLAPIPort_default(host): """Confirms getFTLAPIPort returns the default API port""" output = host.run(