-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.sh
More file actions
executable file
·373 lines (312 loc) · 7.84 KB
/
format.sh
File metadata and controls
executable file
·373 lines (312 loc) · 7.84 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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/bin/sh
# Formats Java, YAML, and Shell files using Google Java Format, yamlfmt, and shfmt
# Supports Linux, macOS, and Windows across multiple architectures
set -e
# Security: Ensure we're in the project directory
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR" || exit 1
YAMLFMT_DIR="yamllint"
SHFMT_DIR="shfmt"
JAVA_FORMATTER="google-java-format-1.28.0-all-deps.jar"
JAVA_SOURCE_DIR="src"
if [ -t 1 ]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
else
RED=''
GREEN=''
YELLOW=''
BLUE=''
NC=''
fi
log_info() {
printf "${BLUE}[INFO]${NC} %s\n" "$1"
}
log_success() {
printf "${GREEN}[SUCCESS]${NC} %s\n" "$1"
}
log_warning() {
printf "${YELLOW}[WARNING]${NC} %s\n" "$1"
}
log_error() {
printf "${RED}[ERROR]${NC} %s\n" "$1"
}
detect_yamlfmt_binary() {
OS=$(uname -s)
ARCH=$(uname -m)
case "$OS" in
Linux)
case "$ARCH" in
x86_64 | amd64)
echo "yamlfmt-linux-x86"
;;
aarch64 | arm64)
echo "yamlfmt-linux-arm64"
;;
i386 | i686)
log_error "32-bit Linux is not supported"
echo "unsupported"
;;
*)
log_error "Unsupported Linux architecture: $ARCH"
echo "unsupported"
;;
esac
;;
Darwin)
case "$ARCH" in
x86_64 | amd64)
echo "yamlfmt-darwin-x86"
;;
arm64)
echo "yamlfmt-darwin-arm64"
;;
*)
log_error "Unsupported macOS architecture: $ARCH"
echo "unsupported"
;;
esac
;;
MINGW* | MSYS* | CYGWIN*)
case "$ARCH" in
x86_64 | amd64)
echo "yamlfmt-x86.exe"
;;
aarch64 | arm64)
echo "yamlfmt-arm64.exe"
;;
*)
log_error "Unsupported Windows architecture: $ARCH"
echo "unsupported"
;;
esac
;;
*)
log_error "Unsupported operating system: $OS"
echo "unsupported"
;;
esac
}
detect_shfmt_binary() {
OS=$(uname -s)
ARCH=$(uname -m)
case "$OS" in
Linux)
case "$ARCH" in
x86_64 | amd64)
echo "shfmt_v3.12.0_linux_amd64"
;;
aarch64 | arm64)
echo "shfmt_v3.12.0_linux_arm64"
;;
armv7l | armv6l)
echo "shfmt_v3.12.0_linux_arm"
;;
i386 | i686)
echo "shfmt_v3.12.0_linux_386"
;;
*)
log_error "Unsupported Linux architecture: $ARCH"
echo "unsupported"
;;
esac
;;
Darwin)
case "$ARCH" in
x86_64 | amd64)
echo "shfmt_v3.12.0_darwin_amd64"
;;
arm64)
echo "shfmt_v3.12.0_darwin_arm64"
;;
*)
log_error "Unsupported macOS architecture: $ARCH"
echo "unsupported"
;;
esac
;;
MINGW* | MSYS* | CYGWIN*)
case "$ARCH" in
x86_64 | amd64)
echo "shfmt_v3.12.0_windows_amd64.exe"
;;
i386 | i686)
echo "shfmt_v3.12.0_windows_386.exe"
;;
*)
log_error "Unsupported Windows architecture: $ARCH"
echo "unsupported"
;;
esac
;;
*)
log_error "Unsupported operating system: $OS"
echo "unsupported"
;;
esac
}
validate_environment() {
if [ ! -d "$YAMLFMT_DIR" ]; then
log_error "yamlfmt directory not found: $YAMLFMT_DIR"
return 1
fi
if [ -L "$YAMLFMT_DIR" ]; then
log_error "Security: $YAMLFMT_DIR is a symbolic link, which is not allowed"
return 1
fi
if [ ! -d "$SHFMT_DIR" ]; then
log_warning "shfmt directory not found: $SHFMT_DIR"
log_warning "Skipping shell script formatting"
fi
if [ -L "$SHFMT_DIR" ]; then
log_error "Security: $SHFMT_DIR is a symbolic link, which is not allowed"
return 1
fi
if [ ! -f "$JAVA_FORMATTER" ]; then
log_error "Java formatter not found: $JAVA_FORMATTER"
return 1
fi
if [ ! -d "$JAVA_SOURCE_DIR" ]; then
log_warning "Java source directory not found: $JAVA_SOURCE_DIR"
log_warning "Skipping Java formatting"
return 2
fi
return 0
}
format_java() {
log_info "Formatting Java files..."
java_files=$(find "$JAVA_SOURCE_DIR" -type f -name "*.java" 2>/dev/null | wc -l)
if [ "$java_files" -eq 0 ]; then
log_warning "No Java files found in $JAVA_SOURCE_DIR"
return 0
fi
log_info "Found $java_files Java file(s)"
if ! find "$JAVA_SOURCE_DIR" -type f -name "*.java" -print0 |
xargs -0 -n 500 java -jar "$JAVA_FORMATTER" --replace 2>&1; then
log_error "Java formatting failed"
return 1
fi
log_success "Java files formatted successfully"
return 0
}
format_yaml() {
yamlfmt_binary="$1"
yamlfmt_path="${YAMLFMT_DIR}/${yamlfmt_binary}"
log_info "Formatting YAML files..."
yamlfmt_realpath=$(cd "$(dirname "$yamlfmt_path")" && pwd)/$(basename "$yamlfmt_path")
expected_dir=$(cd "$YAMLFMT_DIR" && pwd)
case "$yamlfmt_realpath" in
"$expected_dir/"*) ;;
*)
log_error "Security: yamlfmt binary path traversal detected"
return 1
;;
esac
if [ ! -f "$yamlfmt_path" ]; then
log_error "yamlfmt binary not found: $yamlfmt_path"
log_info "Available binaries in $YAMLFMT_DIR:"
ls -1 "$YAMLFMT_DIR"/yamlfmt-* 2>/dev/null || log_info " None found"
return 1
fi
if [ -L "$yamlfmt_path" ]; then
log_error "Security: yamlfmt binary is a symbolic link, which is not allowed"
return 1
fi
chmod +x "$yamlfmt_path" 2>/dev/null || {
log_warning "Could not set executable permissions on $yamlfmt_path"
}
yaml_files=$(find . -type f \( -name "*.yml" -o -name "*.yaml" \) ! -path "./.git/*" ! -path "./.*" 2>/dev/null | wc -l)
if [ "$yaml_files" -eq 0 ]; then
log_warning "No YAML files found"
return 0
fi
log_info "Found $yaml_files YAML file(s)"
if ! find . -type f \( -name "*.yml" -o -name "*.yaml" \) \
! -path "./.git/*" ! -path "./.*" \
-print0 | xargs -0 "$yamlfmt_path" 2>&1; then
log_error "YAML formatting failed"
return 1
fi
log_success "YAML files formatted successfully"
return 0
}
format_shell() {
shfmt_binary="$1"
shfmt_path="${SHFMT_DIR}/${shfmt_binary}"
log_info "Formatting shell script files..."
shfmt_realpath=$(cd "$(dirname "$shfmt_path")" && pwd)/$(basename "$shfmt_path")
expected_dir=$(cd "$SHFMT_DIR" && pwd)
case "$shfmt_realpath" in
"$expected_dir/"*) ;;
*)
log_error "Security: shfmt binary path traversal detected"
return 1
;;
esac
if [ ! -f "$shfmt_path" ]; then
log_error "shfmt binary not found: $shfmt_path"
log_info "Available binaries in $SHFMT_DIR:"
ls -1 "$SHFMT_DIR"/shfmt_* 2>/dev/null || log_info " None found"
return 1
fi
if [ -L "$shfmt_path" ]; then
log_error "Security: shfmt binary is a symbolic link, which is not allowed"
return 1
fi
chmod +x "$shfmt_path" 2>/dev/null || {
log_warning "Could not set executable permissions on $shfmt_path"
}
shell_files=$(find . -type f \( -name "*.sh" -o -name "*.bash" \) ! -path "./.git/*" ! -path "./.*" 2>/dev/null | wc -l)
if [ "$shell_files" -eq 0 ]; then
log_warning "No shell script files found"
return 0
fi
log_info "Found $shell_files shell script file(s)"
if ! "$shfmt_path" -l -w . 2>&1; then
log_error "Shell script formatting failed"
return 1
fi
log_success "Shell script files formatted successfully"
return 0
}
main() {
log_info "Starting code formatting for the current project"
log_info "Platform: $(uname -s) $(uname -m)"
validate_result=0
validate_environment || validate_result=$?
if [ $validate_result -eq 1 ]; then
log_error "Environment validation failed"
exit 1
fi
YAMLFMT_BINARY=$(detect_yamlfmt_binary)
SHFMT_BINARY=$(detect_shfmt_binary)
if [ "$YAMLFMT_BINARY" = "unsupported" ]; then
log_warning "Platform not supported for yamlfmt"
log_warning "OS: $(uname -s), Architecture: $(uname -m)"
log_warning "Skipping YAML formatting"
else
log_info "Using yamlfmt binary: $YAMLFMT_BINARY"
fi
if [ "$SHFMT_BINARY" = "unsupported" ]; then
log_warning "Platform not supported for shfmt"
log_warning "OS: $(uname -s), Architecture: $(uname -m)"
log_warning "Skipping shell script formatting"
else
log_info "Using shfmt binary: $SHFMT_BINARY"
fi
if [ $validate_result -ne 2 ]; then
format_java || exit 1
fi
if [ "$YAMLFMT_BINARY" != "unsupported" ]; then
format_yaml "$YAMLFMT_BINARY" || exit 1
fi
if [ "$SHFMT_BINARY" != "unsupported" ] && [ -d "$SHFMT_DIR" ]; then
format_shell "$SHFMT_BINARY" || exit 1
fi
echo ""
log_success "All formatting completed successfully"
}
main "$@"