Skip to content

Commit

Permalink
Merge pull request #6 from alinmear/#5
Browse files Browse the repository at this point in the history
  • Loading branch information
alinmear committed Mar 12, 2018
2 parents 4a12b8d + e78eb3a commit b42a6eb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
25 changes: 17 additions & 8 deletions configomat.sh
Expand Up @@ -9,25 +9,34 @@ configomat_do_work() {

_env_variable_prefix=$1
[ -z ${_env_variable_prefix} ] && \
echo "I could not find the env prefix. Exiting ..." && return 1
echo "I could not find the env prefix. Exiting ..." && \
return 1

IFS=" " read -r -a _config_files <<< $2

_tmpf=$(mktemp /tmp/configomat.XXXXXX) || exit 1

printenv | grep $_env_variable_prefix > $_tmpf

# dispatch env variables
for env_variable in $(printenv | grep $_env_variable_prefix);do
# for env_variable in $(printenv | grep $_env_variable_prefix);do
while read env_variable
do
# get key
# IFS not working because values like ldap_query_filter or search base consists of several '='
# IFS="=" read -r -a __values <<< $env_variable
# key="${__values[0]}"
# value="${__values[1]}"
key=$(echo $env_variable | cut -d "=" -f1)
key="$(echo $env_variable | cut -d '=' -f1)"
key=${key#"${_env_variable_prefix}"}
# make key lowercase
key=${key,,}
# get value
value=$(echo $env_variable | cut -d "=" -f2-)
config_overrides[$key]=$value
done
value="$(echo $env_variable | cut -d '=' -f2-)"
config_overrides[$key]="$value"
done <$_tmpf

rm -f $_tmpf

for f in "${_config_files[@]}"
do
Expand All @@ -38,10 +47,10 @@ configomat_do_work() {
do
[ -z $key ] && echo -e "\t no key provided" && return 1

echo " >> $f: $key = ${config_overrides[$key]}"
echo " >> $f: $key = ${config_overrides[$key]}"

# Escape special characters
config_overrides[$key]=$((echo ${config_overrides[$key]}|sed -r 's/([\&\|\$\.\*\/\[\\^])/\\\1/g'|sed 's/[]]/\[]]/g')>&1)
config_overrides[$key]="$((echo ${config_overrides[$key]}|sed -r 's/([\=\&\|\$\.\*\/\[\\^])/\\\1/g'|sed 's/[]]/\[]]/g')>&1)"
sed -i -e "s|^${key}[[:space:]]\+.*|${key} = ${config_overrides[$key]}|g" \
${f}
Expand Down
4 changes: 4 additions & 0 deletions test/example.conf
Expand Up @@ -4,3 +4,7 @@ key_3 =
key_4 =value_4
key_5= value_5
key_6=value_6
key_7 = value_7
key_8 = value_8
key_9 = value_9
key_10 = value_10
34 changes: 34 additions & 0 deletions test/tests.bats
Expand Up @@ -56,3 +56,37 @@ load 'test_helper/bats-assert/all'
# grep 'key_6 = new_value_6' /tmp/example.conf 2>&1 > /dev/null
# assert_success
# }

# #5 Check Problem from docker-mailserver: config_overrides[$key]: bad array subscript

@test "#5 Key Value 1" {
export TEST_key_7="uid=user,userPassword=password"
./configomat.sh TEST_ /tmp/example.conf
assert_success
grep "key_7 = uid=user,userPassword=password" /tmp/example.conf 2>&1 > /dev/null
assert_success
}

@test "#5 Key Value 2" {
export "TEST_key_8=(&(objectClass=mailAccount)(uid=%n))"
./configomat.sh TEST_ /tmp/example.conf
assert_success
grep "key_8 = (&(objectClass=mailAccount)(uid=%n))" /tmp/example.conf 2>&1 > /dev/null
assert_success
}

@test "#5 Key Value 3" {
export TEST_key_9="=home=/var/mail/%{ldap:mail}, =mail=maildir:/var/mail/%{ldap:mail}/Maildir"
./configomat.sh TEST_ /tmp/example.conf
assert_success
grep 'key_9 = =home=/var/mail/%{ldap:mail}, =mail=maildir:/var/mail/%{ldap:mail}/Maildir' /tmp/example.conf 2>&1 > /dev/null
assert_success
}

@test "#5 Key Value 4" {
export TEST_key_10="(&(objectClass=mailAccount)(uid=%n))"
./configomat.sh TEST_ /tmp/example.conf
assert_success
grep 'key_10 = (&(objectClass=mailAccount)(uid=%n))' /tmp/example.conf 2>&1 > /dev/null
assert_success
}

0 comments on commit b42a6eb

Please sign in to comment.