-
Notifications
You must be signed in to change notification settings - Fork 1
/
common-functions.sh
executable file
·91 lines (75 loc) · 3.34 KB
/
common-functions.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash -e
## Util functions
## Author : Enrique Garcia <[email protected]>
log() {
LOG_LEVEL=${LOG_LEVEL:-INFO}
echo "[${LOG_LEVEL}] [$(date +%F_%H:%M:%S)] - " $@
}
version_gt() {
test "$(printf '%s\n' "$@" | sort -un | head -n 1)" != "$1";
}
# Write environment variables into a file
env_vars_in_file () {
PREFIX=${1:-$PREFIX} # Example: RABBIT_ .
DEST_FILE=${2:-$DEST_FILE} # Example: $HOME/conf/my.properties.
EXCLUSIONS=${3:-$EXCLUSIONS} # A match pattern for excluding env variables. Example: "^RABBIT_HOME_".
CREATE_FILE=${4:-${CREATE_FILE:-true}} # Create this file if doesn't exist. Example: $HOME/newFile.conf
OVERRIDE=${5:-${OVERRIDE:-true}} # Example: Override the variable in target file when it isn't in comment lines.
FROM_SEPARATOR=${6:-${FROM_SEPARATOR:-'_'}} # Example: Locate this character for splitting, default to '_' .
TO_SEPARATOR=${7:-${TO_SEPARATOR:-'.'}} # Example: Convert FROM_SEPARATOR character/s into this, default to '.' .
LOWER=${8:-${LOWER:-true}} # Convert to low characters RABBIT_PEPE-->pepe. If false preserve original characters RABBIT_size_isEnabled-->size.isEnabled
DEBUG=${9:-${DEBUG:-false}} # Activate debug mode
if $DEBUG ; then
echo -e "Writing environment variables to file :\n"
echo "PREFIX : ${PREFIX}"
echo "DEST_FILE : ${DEST_FILE}"
echo "EXCLUSIONS : ${EXCLUSIONS}"
echo "CREATE_FILE : ${CREATE_FILE}"
echo "OVERRIDE : ${OVERRIDE}"
echo "FROM_SEPARATOR : ${FROM_SEPARATOR}"
echo "TO_SEPARATOR : ${TO_SEPARATOR}"
echo "LOWER : ${LOWER}"
echo -e ".......................................\n"
set -u
set -o pipefail xtrace
LOG_LEVEL="DEBUG"
fi
if [[ -z "${PREFIX}" || -z "${DEST_FILE}" ]]; then
echo 'PREFIX and DEST_FILE are required values :-(' && exit 1;
fi
if [ ! -f $DEST_FILE ]; then
$CREATE_FILE && >>$DEST_FILE
[ ! $CREATE_FILE ] && { echo "Not found file ${DEST_FILE} :-("; exit 1; }
fi
IFS='
'
set -f
for ENV_VAR in `env | grep "^${PREFIX}"`; do
ENV_VAR_NAME=`echo "${ENV_VAR}" | sed -r "s/=(.*)//"`
if [ ! -z "${EXCLUSIONS}" ] && `echo "${ENV_VAR_NAME}" | egrep -q "${EXCLUSIONS}"`; then
$DEBUG && log "[EXCLUDED] : ${ENV_VAR_NAME}"
continue
fi
VAR_NAME=`echo "${ENV_VAR_NAME}" | sed -r "s/${PREFIX}//" | tr ${FROM_SEPARATOR} ${TO_SEPARATOR}`
if $LOWER; then
VAR_NAME=`echo "${VAR_NAME}" | tr '[:upper:]' '[:lower:]'`
fi
VAR_VALUE=`echo "${ENV_VAR}" | sed -r "s/.*=//"`
if `egrep -q "(^|^#)${VAR_NAME}=.*" ${DEST_FILE}`; then
# Not use '+' symbol neither in VAR_NAME nor in VAR_VALUE
if $OVERRIDE; then
sed -r -i "s+(^|^#)${VAR_NAME}=.*+${VAR_NAME}=${VAR_VALUE}+g" ${DEST_FILE} \
&& $DEBUG && log "[OVERRIDE] : ${ENV_VAR_NAME} --> ${VAR_NAME}=${VAR_VALUE}"
else
sed -r -i "s+^#${VAR_NAME}=.*+${VAR_NAME}=${VAR_VALUE}+g" ${DEST_FILE} \
&& $DEBUG && log "[ UPDATE ] : ${ENV_VAR_NAME} --> ${VAR_NAME}=${VAR_VALUE}"
fi
else
# If VAR name not found in file, insert it at end of file
echo -ne "\n${VAR_NAME}=${VAR_VALUE}" >> ${DEST_FILE}
$DEBUG && log "[ ADD ] : ${ENV_VAR_NAME} --> ${VAR_NAME}=${VAR_VALUE}"
fi
done
set +f
unset IFS
}