-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapply.sh
executable file
·137 lines (112 loc) · 1.97 KB
/
apply.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
color () {
local color_name
local message
color_name="$1"
message="$2"
case "$color_name" in
red)
color=1
;;
green)
color=2
;;
yellow)
color=3
;;
blue)
color=4
;;
magenta)
color=5
;;
cyan)
color=6
;;
silver)
color=7
;;
gray)
color=8
;;
brightred)
color=9
;;
brightgreen)
color=10
;;
brightyellow)
color=11
;;
brightblue)
color=12
;;
brightmagenta)
color=13
;;
brightcyan)
color=14
;;
white)
color=15
;;
*)
color=0
esac
echo -e "\e[38;5;${color}m${message}\e[0m"
}
error () {
color red "$1"
exit 1
}
info () {
color cyan "$1"
}
# a list of directorries
top_level_modules=$(
find . \
-maxdepth 1 \
-type d \
-not -path '*/\.*' \
-not -path '.' |
sed 's|./||g' |
tr '\n' ' '
)
# if not found, print usage and exit
print_help() {
info "Usage: comtrya apply <module>"
info "Available modules: ${top_level_modules}"
exit 1
}
validate_module() {
local module
local module_root
module="$1"
module_root=$(echo "$1" | cut -d'.' -f1)
module_as_path=$(echo "$module" | tr '.' '/')
# does "${module_as_path}.yml" exist?
[ ! -f "${module_as_path}.yml" ] && {
error "Invalid module: $module"
}
# if root is not valid module, print usage and exit
[ -z "$module_root" ] && {
error "Invalid module: $module"
}
# does top level modules contain the chosen root module?
[ -z "$(echo "$top_level_modules" | grep "$module_root")" ] && {
error "Invalid module: $module"
}
}
apply_module() {
local module
local module_root
module="$1"
validate_module "$module"
info "==> 🚀 Applying $module"
comtrya apply -m "$module"
}
# if no arguments, print usage and exit
[ -z "$1" ] && print_help
for arg in "$@"; do
apply_module "$arg"
done