Skip to content

Commit 4fd0f15

Browse files
committed
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 Signed-off-by: Christian König <ckoenig@posteo.de>
1 parent 45cab12 commit 4fd0f15

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

advanced/Scripts/utils.sh

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ addOrEditKeyValPair() {
4444
}
4545

4646
#######################
47-
# Takes two arguments: file, and key.
47+
# Takes two arguments: file and key.
4848
# Adds a key to target file
4949
#
5050
# Example usage:
@@ -64,7 +64,7 @@ addKey(){
6464
}
6565

6666
#######################
67-
# Takes two arguments: file, and key.
67+
# Takes two arguments: file and key.
6868
# Deletes a key or key/value pair from target file
6969
#
7070
# Example usage:
@@ -76,6 +76,24 @@ removeKey() {
7676
sed -i "/^${key}/d" "${file}"
7777
}
7878

79+
#######################
80+
# Takes two arguments: file and key.
81+
# Returns the value of a given key from target file
82+
# - ignores all commented lines
83+
# - only returns the first value if multiple identical keys exist
84+
#
85+
#
86+
# Example usage:
87+
# getVal "/etc/pihole/setupVars.conf" "PIHOLE_DNS_1"
88+
#######################
89+
getVal() {
90+
local file="${1}"
91+
local key="${2}"
92+
local value
93+
value=$(sed -e '/^[[:blank:]]*#/d' "${file}" | grep "${key}" | awk -F "=" 'NR==1{printf$2}')
94+
printf "%s" "$value"
95+
}
96+
7997

8098
#######################
8199
# returns FTL's current telnet API port based on the setting in /etc/pihole-FTL.conf

automated install/basic-install.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2612,7 +2612,8 @@ main() {
26122612

26132613
# Get the privacy level if it exists (default is 0)
26142614
if [[ -f "${FTL_CONFIG_FILE}" ]]; then
2615-
PRIVACY_LEVEL=$(sed -ne 's/PRIVACYLEVEL=\(.*\)/\1/p' "${FTL_CONFIG_FILE}")
2615+
# use getVal from utils.sh to get PRIVACYLEVEL
2616+
PRIVACY_LEVEL=$(getVal "${FTL_CONFIG_FILE}" "PRIVACYLEVEL")
26162617

26172618
# If no setting was found, default to 0
26182619
PRIVACY_LEVEL="${PRIVACY_LEVEL:-0}"

test/test_any_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ def test_key_removal_works(host):
6262
assert expected_stdout == output.stdout
6363

6464

65+
def test_get_value_works(host):
66+
"""Confirms getVal returns the correct value for a given key"""
67+
output = host.run(
68+
"""
69+
source /opt/pihole/utils.sh
70+
echo "Somekey=xxx" >> /tmp/testfile
71+
echo "#Testkey=1234" >> /tmp/testfile
72+
echo "Testkey=5678" >> /tmp/testfile
73+
echo "Testkey=abcd" >> /tmp/testfile
74+
getVal "/tmp/testfile" "Testkey"
75+
"""
76+
)
77+
expected_stdout = "5678"
78+
assert expected_stdout == output.stdout
79+
80+
6581
def test_getFTLAPIPort_default(host):
6682
"""Confirms getFTLAPIPort returns the default API port"""
6783
output = host.run(

0 commit comments

Comments
 (0)