-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.sh
353 lines (314 loc) · 9.92 KB
/
example.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/bin/bash
# Define color codes
RED="\033[0;31m"
GREEN="\033[0;32m"
BLUE="\033[0;34m"
YELLOW="\033[1;33m"
NC="\033[0m" # No Color
# Array to keep track of tree indicators for each depth
declare -a TREE_INDICATORS=()
# Define log file
LOG_FILE="example.log"
# Function to log messages with timestamps
log() {
local level=$1
local message=$2
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [$level] $message" | tee -a "$LOG_FILE"
}
# Logging function
# Arguments:
# $1 - Depth level (integer)
# $2 - Name of the file/folder
# $3 - Status ("Valid", "Generated", "Error", "Processing", "Built", "Deleted")
# $4 - Is last item in the current directory ("yes" or "no")
log_entry() {
local depth=$1
local name=$2
local status=$3
local is_last=$4
local prefix=""
# Build the prefix based on TREE_INDICATORS
for ((i=1; i<depth; i++)); do
if [ "${TREE_INDICATORS[i]}" == "yes" ]; then
prefix+=" "
else
prefix+="│ "
fi
done
# Determine the branch symbol
if [ "$is_last" == "yes" ]; then
prefix+="└── "
TREE_INDICATORS[depth]="yes"
else
prefix+="├── "
TREE_INDICATORS[depth]="no"
fi
# Determine the color based on status
local color="$NC"
case "$status" in
Valid)
color="$GREEN"
;;
Generated)
color="$BLUE"
;;
Error)
color="$RED"
;;
Processing)
color="$YELLOW"
;;
Built)
color="$GREEN"
;;
Deleted)
color="$RED"
;;
Merged)
color="$GREEN"
;;
*)
color="$NC"
;;
esac
# Print the log line
echo -e "${prefix}${name} ${color}[${status}]${NC}"
}
# Function to compile the Go application
compile_go_app() {
go build -o YAMLtecture >>"$LOG_FILE" 2>&1
if [ $? -ne 0 ]; then
log_entry 1 "YAMLtecture" "Error" "yes"
exit 1
else
# Log the built application as [Built]
log_entry 1 "YAMLtecture" "Built" "yes"
fi
}
# Function to process mermaid.yaml and generate mermaid.mmd
# Arguments:
# $1 - Directory path
# $2 - Depth level
process_mermaid() {
local dir="${1%/}"
local depth=$2
local mermaid_in="$dir/mermaid.yaml"
local mermaid_out="$dir/mermaid.mmd"
if [ -f "$mermaid_in" ]; then
echo "./YAMLtecture --validateMermaid --mermaidIn=$mermaid_in" >> "$LOG_FILE"
./YAMLtecture --validateMermaid --mermaidIn="$mermaid_in" >>"$LOG_FILE" 2>&1
echo "" >> "$LOG_FILE"
if [ $? -ne 0 ]; then
log_entry "$depth" "mermaid.yaml" "Error" "no"
else
log_entry "$depth" "mermaid.yaml" "Valid" "no"
# Generate mermaid.mmd
echo "./YAMLtecture --generateMermaid --configIn=$dir/config.yaml --mermaidIn=$mermaid_in --out=$mermaid_out" >> "$LOG_FILE"
./YAMLtecture --generateMermaid --configIn="$dir/config.yaml" --mermaidIn="$mermaid_in" --out="$mermaid_out" >>"$LOG_FILE" 2>&1
echo "" >> "$LOG_FILE"
if [ $? -ne 0 ]; then
log_entry "$depth" "mermaid.mmd" "Error" "no"
else
log_entry "$depth" "mermaid.mmd" "Generated" "no"
fi
fi
else
# No mermaid.yaml found, remove mermaid.mmd if exists
if [ -f "$mermaid_out" ]; then
rm -f "$mermaid_out"
log_entry "$depth" "mermaid.mmd" "Deleted" "no"
fi
fi
}
# Function to process queries within a directory
# Arguments:
# $1 - Configuration directory path
# $2 - Depth level
process_queries() {
local config_dir=$1
local depth=$2
local queries_dir="$config_dir/queries"
if [ -d "$queries_dir" ]; then
# Log the 'queries' directory
local is_last_queries="yes" # Assuming 'queries' is the last item in its parent directory
log_entry "$depth" "queries" "Processing" "$is_last_queries"
# Collect all query directories
local queries=()
for query in "$queries_dir"/*/; do
[ -d "$query" ] || continue
queries+=("$query")
done
local total=${#queries[@]}
local count=0
for query in "${queries[@]}"; do
count=$((count + 1))
local current_is_last="no"
if [ "$count" -eq "$total" ]; then
current_is_last="yes"
fi
# Remove the trailing slash and get the basename
query=${query%*/}
local queryName=$(basename "$query")
# Log the query directory
log_entry "$((depth + 1))" "$queryName" "Processing" "$current_is_last"
# Validate the query.yaml
local query_file="$query/query.yaml"
if [ -f "$query_file" ]; then
echo "./YAMLtecture --validateQuery --queryIn=$query_file" >> "$LOG_FILE"
./YAMLtecture --validateQuery --queryIn="$query_file" >>"$LOG_FILE" 2>&1
echo "" >> "$LOG_FILE"
if [ $? -ne 0 ]; then
log_entry "$((depth + 2))" "query.yaml" "Error" "no"
else
log_entry "$((depth + 2))" "query.yaml" "Valid" "no"
# Execute the query
echo "./YAMLtecture --executeQuery --configIn=$config_dir/config.yaml --queryIn=$query_file --out=$query/config.yaml" >> "$LOG_FILE"
./YAMLtecture --executeQuery --configIn="$config_dir/config.yaml" --queryIn="$query_file" --out="$query/config.yaml" >>"$LOG_FILE" 2>&1
echo "" >> "$LOG_FILE"
if [ $? -ne 0 ]; then
log_entry "$((depth + 2))" "config.yaml" "Error" "no"
else
log_entry "$((depth + 2))" "config.yaml" "Generated" "no"
# Validate the generated config.yaml
echo "./YAMLtecture --validateConfig --configIn=$query/config.yaml" >> "$LOG_FILE"
./YAMLtecture --validateConfig --configIn="$query/config.yaml" >>"$LOG_FILE" 2>&1
echo "" >> "$LOG_FILE"
if [ $? -ne 0 ]; then
log_entry "$((depth + 2))" "config.yaml" "Error" "no"
else
# Process mermaid for the query
process_mermaid "$query" "$((depth + 2))"
fi
fi
fi
else
log_entry "$((depth + 2))" "query.yaml" "Error" "no"
fi
done
else
log_entry "$depth" "queries" "Error" "no"
fi
}
# Function to process a single configuration directory
# Arguments:
# $1 - Configuration directory path
# $2 - Depth level
# $3 - Is last item in the parent directory ("yes" or "no")
process_config_dir() {
local dir="${1%/}"
local depth=$2
local is_last=$3
local name=$(basename "$dir")
log_entry "$depth" "$name" "Processing" "$is_last"
# Process the "configs" folder if it exists
if [ -d "$dir/configs" ]; then
echo "./YAMLtecture --in=$dir/configs --mergeConfig" --out=$dir/config.yaml >> "$LOG_FILE"
./YAMLtecture --in="$dir/configs" --mergeConfig --out=$dir/config.yaml >>"$LOG_FILE" 2>&1
echo "" >> "$LOG_FILE"
if [ $? -ne 0 ]; then
log_entry "$((depth + 1))" "configs" "Error" "no"
else
log_entry "$((depth + 1))" "configs" "Merged" "no"
fi
fi
# Collect all items in the configuration directory except 'queries'
local items=()
for item in "$dir"/*; do
if [ -e "$item" ] && [ "$(basename "$item")" != "queries" ]; then
items+=("$item")
fi
done
# Check if 'queries' exists to determine if it's the last item
local has_queries=0
if [ -d "$dir/queries" ]; then
has_queries=1
fi
local total=${#items[@]}
if [ "$has_queries" -eq 1 ]; then
total=$((total + 1))
fi
local count=0
# Process each item in the configuration directory
for item in "${items[@]}"; do
count=$((count + 1))
local current_is_last="no"
if [ "$count" -eq "$total" ]; then
current_is_last="yes"
fi
local item_name
item_name=$(basename "$item")
case "$item_name" in
config.yaml)
# Validate the config.yaml
echo "./YAMLtecture --validateConfig --configIn=$item" >> "$LOG_FILE"
./YAMLtecture --validateConfig --configIn="$item" >>"$LOG_FILE" 2>&1
echo "" >> "$LOG_FILE"
if [ $? -ne 0 ]; then
log_entry "$((depth + 1))" "config.yaml" "Error" "no"
else
log_entry "$((depth + 1))" "config.yaml" "Valid" "no"
fi
;;
mermaid.yaml)
# Validate and generate mermaid.mmd
echo "./YAMLtecture --validateMermaid --mermaidIn=$item" >> "$LOG_FILE"
./YAMLtecture --validateMermaid --mermaidIn="$item" >>"$LOG_FILE" 2>&1
echo "" >> "$LOG_FILE"
if [ $? -ne 0 ]; then
log_entry "$((depth + 1))" "mermaid.yaml" "Error" "no"
else
log_entry "$((depth + 1))" "mermaid.yaml" "Valid" "no"
echo "./YAMLtecture --generateMermaid --configIn=$dir/config.yaml --mermaidIn=$item --out=$dir/mermaid.mmd" >> "$LOG_FILE"
./YAMLtecture --generateMermaid --configIn="$dir/config.yaml" --mermaidIn="$item" --out="$dir/mermaid.mmd" >>"$LOG_FILE" 2>&1
echo "" >> "$LOG_FILE"
if [ $? -ne 0 ]; then
log_entry "$((depth + 1))" "mermaid.mmd" "Error" "no"
else
log_entry "$((depth + 1))" "mermaid.mmd" "Generated" "no"
fi
fi
;;
*)
# Handle other files if necessary
;;
esac
done
# If 'queries' directory exists, process it
if [ "$has_queries" -eq 1 ]; then
local is_last_queries="yes" # Assuming 'queries' is the last item
process_queries "$dir" "$((depth + 1))"
fi
}
# Function to process the example directory
process_example_dir() {
local example_dir="example"
if [ -d "$example_dir" ]; then
# Collect top-level directories
local dirs=()
for dir in "$example_dir"/*/; do
[ -d "$dir" ] || continue
dirs+=("$dir")
done
local total=${#dirs[@]}
local count=0
for dir in "${dirs[@]}"; do
count=$((count + 1))
local is_last="no"
if [ "$count" -eq "$total" ]; then
is_last="yes"
fi
process_config_dir "$dir" 2 "$is_last"
done
else
echo -e "${RED}Directory 'example' does not exist.${NC}"
exit 1
fi
}
# Main script execution starts here
rm -f "$LOG_FILE"
# Compile the Go application
compile_go_app
# Process the example directory
process_example_dir
# Optionally, clean up the binary after processing
# rm -f YAMLtecture