This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.sh
executable file
·67 lines (55 loc) · 1.84 KB
/
config.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env bash
# Run it like:
# `CONFIG_DIR=/usr/local/docker-config config/tools/config.sh /tmp/secrets.txt`
# First arg is the full path of the secrets.txt file
SECRETS_FILE=$1
if [ "${SECRETS_FILE}" == "" ];
then
printf "\nERROR: The first argument, name of the secrets file, is mandatory.\n\n"
exit 2
fi
if [ "${CONFIG_DIR}" == "" ];
then
printf "\nERROR: CONFIG_DIR environment variable is not set, or empty. Cannot proceed.\n\n"
exit 2
fi
SED_VERSION=$(sed --version 2>/dev/null | head -1 | cut -d ")" -f 2 | tr -d " ")
LOGFILE=logfile_`date +%Y%m%d_%H%M%S`.log
touch ${LOGFILE}
echo "Running ...."
echo "Replace template directory"
rm -fR ${CONFIG_DIR}/*
cp -r config/template/* ${CONFIG_DIR}
echo "Done"
echo "Start replacement"
for FN in `find ${CONFIG_DIR} -name "*.*"`
do
FILE_TO_ACT_ON="${FN}"
echo "**************** Processing file ${FILE_TO_ACT_ON} ****************" >> ${LOGFILE}
for KEY_VALUE_PAIR in $(cat ${SECRETS_FILE})
do
if [ "${KEY_VALUE_PAIR}" != "" ] && [ $(echo "${KEY_VALUE_PAIR}" | cut -c 1) != "#" ];
then
echo "Replacing kv pair '${KEY_VALUE_PAIR}'" >> ${LOGFILE}
KEY=$(echo "${KEY_VALUE_PAIR}" | cut -d "=" -f 1)
VALUE=$(echo "${KEY_VALUE_PAIR}" | cut -d "=" -f 2)
VALUE=${VALUE//'/'/'\/'}
if [ "${SED_VERSION}" == "" ];
then
# Stupid macOSX 'feature'
sed -i '' "s/__${KEY}__/${VALUE}/g" ${FILE_TO_ACT_ON} 2>> ${LOGFILE}
else
sed -i "s/__${KEY}__/${VALUE}/g" ${FILE_TO_ACT_ON} 2>> ${LOGFILE}
fi
if [ $? -ne 0 ];
then
echo "Failed :(" >> ${LOGFILE}
fi
else
echo "Skipping empty or comment line '${KEY_VALUE_PAIR}' in secrets.txt" >> ${LOGFILE}
fi
done
done
echo "Finished. Log file generated ${LOGFILE}"
echo ""
find ${CONFIG_DIR} -exec grep -H "__" {} \; 2>/dev/null