-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtpl-resolve.sh
More file actions
90 lines (82 loc) · 2.6 KB
/
Copy pathtpl-resolve.sh
File metadata and controls
90 lines (82 loc) · 2.6 KB
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
#!/bin/sh
function usage() {
echo "tpl-resolve.sh [-f input-filename] [-o output-filename] [-h]"
echo " input-filename: Input filename, if not specified, process all .yaml, .yml, .conf files in the current directory, including subdirectories"
echo " output-filename: Output filename, if not specified, directly modify the input file"
echo "tpl-resolve.sh reads configuration items from configure.sh"
echo " Variables defined in {{varname}} will be replaced with the value of the variable varname"
}
while getopts "f:o:h" opt
do
case $opt in
f)
input=$OPTARG;;
o)
output=$OPTARG;;
h)
usage
exit 0;;
?)
usage
exit 1;;
esac
done
. ./configure.sh
. ./scripts/newest-images.list
#
# Process variable markers in a .tpl file, resolve variables to actual values
# @param input_file Input file
# @param output_file Output file
#
function resolve_file() {
input_file=$1
output_file=$2
cp -f "$input_file" "$output_file"
echo generate $input_file to $output_file ...
# Find all {{variables}} and replace with values
for i in `grep -o -w -E "\{\{([[:alnum:]]|\.|_)*\}\}" $output_file|sort|uniq|tr -d '\r'`; do
# Extract key name
key=${i:2:(${#i}-4)}
# Get key value: search file content
value=$(eval echo \$$key)
echo "$key=>$value"
# Replace file content
if echo "$value" | grep -vq '#'; then
sed -i "s#$i#$value#g" "$output_file";
elif echo "$value" | grep -vq '/'; then
sed -i "s/$i/$value/g" "$output_file";
elif echo "$value" | grep -vq ','; then
sed -i "s,$i,$value,g" "$output_file";
else
echo "The value $value contains special characters \"#/,\" simultaneously, unable to perform replacement"
fi
done
}
#
# Process all .tpl files in a directory
# @param dir Directory path
#
function resolve_dir() {
dir="$1"
echo generate $dir ...
for entry in "$dir"/*; do
if [ -d "$entry" ]; then
# If it's a directory, call recursively
resolve_dir "$entry"
elif [ -f "$entry" ] && [[ "$entry" == *.tpl ]]; then
# If it's a .tpl file, remove the extension and output the filename
filename="${entry%.tpl}" # Remove .tpl extension
# Output the filename without the extension
resolve_file "$entry" "$filename"
fi
done
}
if [ ""X == "$output"X ]; then
output="$input"
fi
if [ X"" == X"$input" ]; then
resolve_dir .
else
resolve_file $input $output
cat $output
fi