mirror of https://github.com/pi-hole/pi-hole
Merge pull request #4585 from pi-hole/tweak/sed-add-if-not-exists
Replace value for BLOCKING_ENABLED, add if it does not already existpull/4595/head
commit
76d4e1209f
@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env bash
|
||||
# Pi-hole: A black hole for Internet advertisements
|
||||
# (c) 2017 Pi-hole, LLC (https://pi-hole.net)
|
||||
# Network-wide ad blocking via your own hardware.
|
||||
#
|
||||
# Script to hold utility functions for use in other scripts
|
||||
#
|
||||
# This file is copyright under the latest version of the EUPL.
|
||||
# Please see LICENSE file for your rights under this license.
|
||||
|
||||
# Basic Housekeeping rules
|
||||
# - Functions must be self contained
|
||||
# - Functions must be added in alphabetical order
|
||||
# - Functions must be documented
|
||||
# - New functions must have a test added for them in test/test_any_utils.py
|
||||
|
||||
#######################
|
||||
# Takes three arguments key, value, and file.
|
||||
# Checks the target file for the existence of the key
|
||||
# - If it exists, it changes the value
|
||||
# - If it does not exist, it adds the value
|
||||
#
|
||||
# Example usage:
|
||||
# addOrEditKeyValuePair "BLOCKING_ENABLED" "true" "/etc/pihole/setupVars.conf"
|
||||
#######################
|
||||
addOrEditKeyValPair() {
|
||||
local key="${1}"
|
||||
local value="${2}"
|
||||
local file="${3}"
|
||||
if grep -q "^${key}=" "${file}"; then
|
||||
sed -i "/^${key}=/c\\${key}=${value}" "${file}"
|
||||
else
|
||||
echo "${key}=${value}" >> "${file}"
|
||||
fi
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
def test_key_val_replacement_works(host):
|
||||
''' Confirms addOrEditKeyValPair provides the expected output '''
|
||||
host.run('''
|
||||
setupvars=./testoutput
|
||||
source /opt/pihole/utils.sh
|
||||
addOrEditKeyValPair "KEY_ONE" "value1" "./testoutput"
|
||||
addOrEditKeyValPair "KEY_TWO" "value2" "./testoutput"
|
||||
addOrEditKeyValPair "KEY_ONE" "value3" "./testoutput"
|
||||
addOrEditKeyValPair "KEY_FOUR" "value4" "./testoutput"
|
||||
cat ./testoutput
|
||||
''')
|
||||
output = host.run('''
|
||||
cat ./testoutput
|
||||
''')
|
||||
expected_stdout = 'KEY_ONE=value3\nKEY_TWO=value2\nKEY_FOUR=value4\n'
|
||||
assert expected_stdout == output.stdout
|
Loading…
Reference in new issue