File tree 3 files changed +38
-3
lines changed
3 files changed +38
-3
lines changed Original file line number Diff line number Diff line change @@ -44,7 +44,7 @@ addOrEditKeyValPair() {
44
44
}
45
45
46
46
# ######################
47
- # Takes two arguments: file, and key.
47
+ # Takes two arguments: file and key.
48
48
# Adds a key to target file
49
49
#
50
50
# Example usage:
@@ -64,7 +64,7 @@ addKey(){
64
64
}
65
65
66
66
# ######################
67
- # Takes two arguments: file, and key.
67
+ # Takes two arguments: file and key.
68
68
# Deletes a key or key/value pair from target file
69
69
#
70
70
# Example usage:
@@ -76,6 +76,24 @@ removeKey() {
76
76
sed -i " /^${key} /d" " ${file} "
77
77
}
78
78
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
+
79
97
80
98
# ######################
81
99
# returns FTL's current telnet API port based on the setting in /etc/pihole-FTL.conf
Original file line number Diff line number Diff line change @@ -2612,7 +2612,8 @@ main() {
2612
2612
2613
2613
# Get the privacy level if it exists (default is 0)
2614
2614
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" )
2616
2617
2617
2618
# If no setting was found, default to 0
2618
2619
PRIVACY_LEVEL=" ${PRIVACY_LEVEL:- 0} "
Original file line number Diff line number Diff line change @@ -62,6 +62,22 @@ def test_key_removal_works(host):
62
62
assert expected_stdout == output .stdout
63
63
64
64
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
+
65
81
def test_getFTLAPIPort_default (host ):
66
82
"""Confirms getFTLAPIPort returns the default API port"""
67
83
output = host .run (
You can’t perform that action at this time.
0 commit comments