/
envsubst-on-templates-and-move.sh
64 lines (56 loc) · 2.27 KB
/
envsubst-on-templates-and-move.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
#!/bin/sh
set -e
ME=$(basename $0)
# cross platform realpath
# busy box realpath does not support the '--relative-to' switch
relpath () {
local ref absolute
ref=$(realpath $1)
absolute=$(realpath $2)
echo ${absolute#"$ref"}
}
auto_envsubst_and_move_all() {
local root_directory suffix defined_envs
root_directory=$(realpath $1)
suffix=$2
defined_envs=$(printf '${%s} ' $(env | cut -d= -f1))
find "$root_directory" -follow -type f -print | while read -r found_file; do
# relative path to root directory
local source_filepath source_relative_filepath target_dirpath target_filepath source_filepath_no_suffix
source_filepath=$(realpath $found_file)
source_relative_filepath=$(relpath $root_directory $found_file)
target_filepath=${source_relative_filepath%%"$suffix"}
target_dirpath=$(dirname $target_filepath)
source_filepath_no_suffix=${source_filepath%%"$suffix"}
if [ $(basename $source_filepath) != $(basename $target_filepath) ] && [ -f $source_filepath_no_suffix ]; then
echo >&2 "$ME: ERROR: after env interpolation, file '$source_filepath' will have the same name as file '$source_filepath_no_suffix'"
exit 22 # invalid argument
fi
# create target folder if not exists
if [ ! -d $target_dirpath ]; then
echo "$ME: $target_dirpath does not exists, creating hierachy..."
mkdir -p $target_dirpath
fi
# check if target folder is writable
if [ ! -w $target_dirpath ]; then
echo >&2 "$ME: ERROR: '[$root_directory/]$source_relative_filepath' found, but its target directory '$target_dirpath' is not writable"
exit 30 # read-only filesystem
fi
# file interpolation
if [ $(basename $source_filepath) != $(basename $target_filepath) ]; then
echo "$ME: Running envsubst on $source_filepath to $target_filepath"
envsubst "$defined_envs" < "$source_filepath" > "$target_filepath"
else
echo "$ME: Copying file '$source_relative_filepath' to target '$target_filepath'"
cp $source_filepath $target_filepath
fi
done
}
CONFIG_FOLDER=${1:-"/cm_configs"}
TEMPLATE_SUFFIX=${2:-".sample"}
if [ -d $CONFIG_FOLDER ]; then
auto_envsubst_and_move_all $CONFIG_FOLDER $TEMPLATE_SUFFIX
else
echo >&2 "$ME: WARNING: configuration directory '$CONFIG_FOLDER' not found"
fi
exit 0