Compare commits

..

2 Commits

Author SHA1 Message Date
Adam Warner
8775720668 flip logic that was accidentally reversed in #4653
Signed-off-by: Adam Warner <me@adamwarner.co.uk>
2022-04-14 17:37:14 +01:00
Adam Warner
48f56d0385 I tried to do too many things in one function, vastly overcomplicating what should have been _this_ all along
Signed-off-by: Adam Warner <me@adamwarner.co.uk>
2022-04-13 22:48:39 +01:00
5 changed files with 37 additions and 30 deletions

View File

@@ -17,43 +17,49 @@
# - 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 either # Takes Three arguments: file, key, and value.
# - Three arguments: file, key, and value.
# - Two arguments: file, and key.
# #
# 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
# #
# Example usage: # Example usage:
# addOrEditKeyValuePair "/etc/pihole/setupVars.conf" "BLOCKING_ENABLED" "true" # addOrEditKeyValPair "/etc/pihole/setupVars.conf" "BLOCKING_ENABLED" "true"
####################### #######################
addOrEditKeyValPair() { addOrEditKeyValPair() {
local file="${1}" local file="${1}"
local key="${2}" local key="${2}"
local value="${3}" local value="${3}"
if [ "${value}" != "" ]; then if grep -q "^${key}=" "${file}"; then
# value has a value, so it is a key-value pair
if grep -q "^${key}=" "${file}"; then
# Key already exists in file, modify the value # Key already exists in file, modify the value
sed -i "/^${key}=/c\\${key}=${value}" "${file}" 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
# value has no value, so it is just a key. Add it if it does not already exist # Key does not already exist, add it and it's value
if ! grep -q "^${key}" "${file}"; then echo "${key}=${value}" >> "${file}"
# Key does not exist, add it.
echo "${key}" >> "${file}"
fi
fi fi
} }
####################### #######################
# Takes two arguments file, and key. # Takes two arguments: file, and key.
# Deletes a key from target file # Adds a key to target file
#
# Example usage:
# addKey "/etc/dnsmasq.d/01-pihole.conf" "log-queries"
#######################
addKey(){
local file="${1}"
local key="${2}"
if ! grep -q "^${key}" "${file}"; then
# Key does not exist, add it.
echo "${key}" >> "${file}"
fi
}
#######################
# Takes two arguments: file, and key.
# Deletes a key or key/value pair from target file
# #
# Example usage: # Example usage:
# removeKey "/etc/pihole/setupVars.conf" "PIHOLE_DNS_1" # removeKey "/etc/pihole/setupVars.conf" "PIHOLE_DNS_1"

View File

@@ -28,7 +28,7 @@ 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"
utilsfile="/opt/pihole/utils.sh" readonly utilsfile="/opt/pihole/utils.sh"
source "${utilsfile}" source "${utilsfile}"
coltable="/opt/pihole/COL_TABLE" coltable="/opt/pihole/COL_TABLE"

View File

@@ -11,9 +11,10 @@
source "/opt/pihole/COL_TABLE" source "/opt/pihole/COL_TABLE"
while true; do while true; do
read -rp " ${QST} Are you sure you would like to remove ${COL_WHITE}Pi-hole${COL_NC}? [y/N] " answer read -rp " ${QST} Are you sure you would like to remove ${COL_WHITE}Pi-hole${COL_NC}? [y/N] " yn
case ${answer} in case ${yn} in
[Yy]* ) break;; [Yy]* ) break;;
[Nn]* ) echo -e "${OVER} ${COL_LIGHT_GREEN}Uninstall has been canceled${COL_NC}"; exit 0;;
* ) echo -e "${OVER} ${COL_LIGHT_GREEN}Uninstall has been canceled${COL_NC}"; exit 0;; * ) echo -e "${OVER} ${COL_LIGHT_GREEN}Uninstall has been canceled${COL_NC}"; exit 0;;
esac esac
done done
@@ -75,8 +76,8 @@ removeAndPurge() {
for i in "${DEPS[@]}"; do for i in "${DEPS[@]}"; do
if package_check "${i}" > /dev/null; then if package_check "${i}" > /dev/null; then
while true; do while true; do
read -rp " ${QST} Do you wish to remove ${COL_WHITE}${i}${COL_NC} from your system? [Y/N] " answer read -rp " ${QST} Do you wish to remove ${COL_WHITE}${i}${COL_NC} from your system? [Y/N] " yn
case ${answer} in case ${yn} in
[Yy]* ) [Yy]* )
echo -ne " ${INFO} Removing ${i}..."; echo -ne " ${INFO} Removing ${i}...";
${SUDO} "${PKG_REMOVE[@]}" "${i}" &> /dev/null; ${SUDO} "${PKG_REMOVE[@]}" "${i}" &> /dev/null;
@@ -214,8 +215,8 @@ while true; do
echo -n "${i} " echo -n "${i} "
done done
echo "${COL_NC}" echo "${COL_NC}"
read -rp " ${QST} Do you wish to go through each dependency for removal? (Choosing No will leave all dependencies installed) [Y/n] " answer read -rp " ${QST} Do you wish to go through each dependency for removal? (Choosing No will leave all dependencies installed) [Y/n] " yn
case ${answer} in case ${yn} in
[Yy]* ) removeAndPurge; break;; [Yy]* ) removeAndPurge; break;;
[Nn]* ) removeNoPurge; break;; [Nn]* ) removeNoPurge; break;;
* ) removeAndPurge; break;; * ) removeAndPurge; break;;

6
pihole
View File

@@ -21,7 +21,7 @@ readonly FTL_PID_FILE="/run/pihole-FTL.pid"
readonly colfile="${PI_HOLE_SCRIPT_DIR}/COL_TABLE" readonly colfile="${PI_HOLE_SCRIPT_DIR}/COL_TABLE"
source "${colfile}" source "${colfile}"
utilsfile="${PI_HOLE_SCRIPT_DIR}/utils.sh" readonly utilsfile="${PI_HOLE_SCRIPT_DIR}/utils.sh"
source "${utilsfile}" source "${utilsfile}"
webpageFunc() { webpageFunc() {
@@ -260,7 +260,7 @@ Options:
exit 0 exit 0
elif [[ "${1}" == "off" ]]; then elif [[ "${1}" == "off" ]]; then
# Disable logging # Disable logging
addOrEditKeyValPair /etc/dnsmasq.d/01-pihole.conf "log-queries" removeKey /etc/dnsmasq.d/01-pihole.conf "log-queries"
addOrEditKeyValPair "${setupVars}" "QUERY_LOGGING" "false" addOrEditKeyValPair "${setupVars}" "QUERY_LOGGING" "false"
if [[ "${2}" != "noflush" ]]; then if [[ "${2}" != "noflush" ]]; then
# Flush logs # Flush logs
@@ -270,7 +270,7 @@ Options:
local str="Logging has been disabled!" local str="Logging has been disabled!"
elif [[ "${1}" == "on" ]]; then elif [[ "${1}" == "on" ]]; then
# Enable logging # Enable logging
removeKey /etc/dnsmasq.d/01-pihole.conf "log-queries" addKey /etc/dnsmasq.d/01-pihole.conf "log-queries"
addOrEditKeyValPair "${setupVars}" "QUERY_LOGGING" "true" addOrEditKeyValPair "${setupVars}" "QUERY_LOGGING" "true"
echo -e " ${INFO} Enabling logging..." echo -e " ${INFO} Enabling logging..."
local str="Logging has been enabled!" local str="Logging has been enabled!"

View File

@@ -6,8 +6,8 @@ def test_key_val_replacement_works(host):
addOrEditKeyValPair "./testoutput" "KEY_TWO" "value2" addOrEditKeyValPair "./testoutput" "KEY_TWO" "value2"
addOrEditKeyValPair "./testoutput" "KEY_ONE" "value3" addOrEditKeyValPair "./testoutput" "KEY_ONE" "value3"
addOrEditKeyValPair "./testoutput" "KEY_FOUR" "value4" addOrEditKeyValPair "./testoutput" "KEY_FOUR" "value4"
addOrEditKeyValPair "./testoutput" "KEY_FIVE_NO_VALUE" addKey "./testoutput" "KEY_FIVE_NO_VALUE"
addOrEditKeyValPair "./testoutput" "KEY_FIVE_NO_VALUE" addKey "./testoutput" "KEY_FIVE_NO_VALUE"
''') ''')
output = host.run(''' output = host.run('''
cat ./testoutput cat ./testoutput