-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalues.sh
83 lines (71 loc) · 1.99 KB
/
values.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
#!/usr/bin/env bash
DIRECTORY="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
ROOT_DIRECTORY=$(dirname $DIRECTORY)
BOLD='\e[1m'
BLUE='\e[34m'
RED='\e[31m'
YELLOW='\e[33m'
GREEN='\e[92m'
NC='\e[0m'
init() {
CREATED_FILES=()
VARIABLES=()
}
info() {
printf "\n${BOLD}${BLUE}====> $(echo $@ ) ${NC}\n"
}
warning () {
printf "\n${BOLD}${YELLOW}====> $(echo $@ ) ${NC}\n"
}
error() {
printf "\n${BOLD}${RED}====> $(echo $@ ) ${NC}\n"
bash -c "exit 1"
}
success () {
printf "\n${BOLD}${GREEN}====> $(echo $@ ) ${NC}\n"
}
# find template files
findTemplateFiles() {
# determine the directory
local new_dir=$([ "$2" != "" ] && echo $new_dir || echo $DIRECTORY )
info "current directory is $new_dir"
local _yamlFilesVariable=$1
local _templates=$(find $new_dir -name "*.templ" -type f)
if [ "$_yamlFilesVariable" ]; then
# let variable parsed carry the array of templates
eval $_yamlFilesVariable="'$_templates'"
else
echo $_templates;
fi
}
# find variable and replace it within the file
findAndReplaceVariables() {
for file in ${TEMPLATES[@]}; do
local output=${file%.templ}
local temp=""
cp $file $output
info "Building $(basename $file) template to $(basename $output)"
for variable in ${VARIABLES[@]}; do
local value=${!variable}
echo "$variable=====$value"
sed -i -e "s|\$($variable)|$value|g" $output;
sed -i -e "s|<$variable>|$value|g" $output;
done
if [[ $? == 0 ]]; then
success "Template file $(basename $file) has been successfuly built to $(basename $output)"
else
error "Failed to build template $(basename $file), variable $temp not found"
fi
done
# info "Cleaning backup files after substitution" for mac
}
main(){
local new_dir="$DIRECTORY"
new_dir="$new_dir/$1"
info "building $1 scripts"
findTemplateFiles 'TEMPLATES' $new_dir
findAndReplaceVariables
}
# set array of template variables
source variables.sh
main $@