-
Notifications
You must be signed in to change notification settings - Fork 9
/
_functions_files.sh
111 lines (101 loc) · 2.91 KB
/
_functions_files.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash -eu
# Don't load it several times
set +u
${_FUNCTIONS_FILES_LOADED:-false} && return
set -u
# if the script was started from the base directory, then the
# expansion returns a period
if test "${SCRIPT_DIR}" == "."; then
SCRIPT_DIR="$PWD"
# if the script was not called with an absolute path, then we need to add the
# current working directory to the relative path of the script
elif test "${SCRIPT_DIR:0:1}" != "/"; then
SCRIPT_DIR="$PWD/${SCRIPT_DIR}"
fi
# #############################################################################
# Load shared functions
# #############################################################################
source "${SCRIPT_DIR}/_functions_core.sh"
# #############################################################################
# Files related functions
# #############################################################################
#
# Replace in file $1 the value $2 by $3
#
replace_in_file() {
mv $1 $1.orig
${CMD_SED} "s|$2|$3|g" $1.orig > $1
rm $1.orig
}
#
# find_file <VAR> <PATH1> .. <PATHx>
# test all paths and the path of the latest one existing in parameters is set as VAR
#
find_file() {
set +u
local _varName=$1
shift;
# default value set to UNSET
env_var ${_varName} "UNSET"
for i in $*
do
[ -e "$i" ] && env_var ${_varName} "$i"
done
set -u
}
#
# Apply Jinja2 template from $1 file
#
j2() {
if ! which j2 &>/dev/null; then
local tmpfile=$(mktemp)
env > $tmpfile
${DOCKER_CMD} run --env-file $tmpfile --rm -v "$2":"$2" ${DEPLOYMENT_J2CLI_IMAGE}:${DEPLOYMENT_J2CLI_VERSION} $1 $2
rm $tmpfile
else
j2 $1 $2
fi
}
#
# Replace in file $1 all environment variables (${XXX}) and push the result in $2
#
evaluate_file_content() {
local _file_in=$1
local _file_out=$2
if [ ${_file_in##*.} = "j2" ]; then
j2 --undefined ${_file_in} > ${_file_out}
else
if which perl &>/dev/null; then
perl -pe 's/\$\{([^}]+)\}/$ENV{$1} || ""/ge' < ${_file_in} > ${_file_out}
else
awk '{while(match($0,"[$]{[^}]*}")) {var=substr($0,RSTART+2,RLENGTH -3);gsub("[$]{"var"}",ENVIRON[var])}}1' < ${_file_in} > ${_file_out}
fi
# escape any single quote
if ${LINUX}; then
replace_in_file ${_file_out} "'" "\\\'"
else
replace_in_file ${_file_out} "\'" "\\\'"
fi
fi
}
# Backup the file passed as parameter
backup_file() {
if [ -d $1 ]; then
# We need to backup existing file if they already exist
cd $1
local _start_date=`date -u "+%Y%m%d-%H%M%S-UTC"`
for file in $2
do
if [ -e ${file} ]; then
echo_info "Archiving existing file $file as archived-on-${_start_date}-$file ..."
mv ${file} archived-on-${_start_date}-${file}
echo_info "Done."
fi
done
cd -
fi
}
# #############################################################################
# Env var to not load it several times
_FUNCTIONS_FILES_LOADED=true
echo_debug "_functions_files.sh Loaded"