1
- #! /usr/ bin/env bash
1
+ #! /bin/sh
2
2
3
3
# Be strict
4
4
set -e
5
5
set -u
6
- set -o pipefail
7
6
8
7
9
8
# --------------------------------------------------------------------------------
@@ -55,60 +54,6 @@ print_usage() {
55
54
}
56
55
57
56
58
- _fmt () {
59
- local list=" ${1} "
60
- local write=" ${2} "
61
- local diff=" ${3} "
62
- local check=" ${4} "
63
- local file=" ${5} "
64
- # shellcheck disable=SC2155
65
- local temp=" /tmp/$( basename " ${file} " ) .tf"
66
- local ret=0
67
-
68
- # Build command (only append if default values are overwritten)
69
- local cmd=" terraform fmt"
70
- if [ " ${list} " = " 0" ]; then
71
- cmd=" ${cmd} -list=false"
72
- else
73
- cmd=" ${cmd} -list=true"
74
- fi
75
- if [ " ${write} " = " 1" ]; then
76
- cmd=" ${cmd} -write=true"
77
- else
78
- cmd=" ${cmd} -write=false"
79
- fi
80
- if [ " ${diff} " = " 1" ]; then
81
- cmd=" ${cmd} -diff"
82
- fi
83
- if [ " ${check} " = " 1" ]; then
84
- cmd=" ${cmd} -check"
85
- fi
86
-
87
- # Output and execute command
88
- echo " ${cmd} ${file} "
89
- cp -f " ${file} " " ${temp} "
90
- if ! eval " ${cmd} ${temp} " ; then
91
- ret=$(( ret + 1 ))
92
- fi
93
-
94
- # If -write was specified, copy file back
95
- if [ " ${write} " = " 1" ]; then
96
- # Get owner and permissions of current file
97
- _UID=" $( stat -c %u " ${file} " ) "
98
- _GID=" $( stat -c %g " ${file} " ) "
99
- _PERM=" $( stat -c %a " ${file} " ) "
100
-
101
- # Adjust permissions of temporary file
102
- chown ${_UID} :${_GID} " ${temp} "
103
- chmod ${_PERM} " ${temp} "
104
-
105
- # Overwrite existing file
106
- mv -f " ${temp} " " ${file} "
107
- fi
108
- return " ${ret} "
109
- }
110
-
111
-
112
57
# --------------------------------------------------------------------------------
113
58
# ENTRYPOINT (EVALUATE ARGUMENTS)
114
59
# --------------------------------------------------------------------------------
301
246
# ## (1/3) Single file
302
247
# ##
303
248
if [ -f " ${ARG_PATH} " ]; then
304
- _fmt " ${ARG_LIST} " " ${ARG_WRITE} " " ${ARG_DIFF} " " ${ARG_CHECK} " " ${ARG_PATH} "
249
+ /fmt.sh " ${ARG_LIST} " " ${ARG_WRITE} " " ${ARG_DIFF} " " ${ARG_CHECK} " " ${ARG_PATH} "
305
250
exit " ${?} "
306
251
else
307
252
# ##
@@ -311,33 +256,37 @@ else
311
256
312
257
# evaluate ignore paths
313
258
if [ -n " ${ARG_IGNORE} " ]; then
314
- _EXCLUDE=" -not \( -path \" ${ARG_PATH} /${ARG_IGNORE// ,/* \" -o -path \" ${ARG_PATH} \/ } *\" \)"
259
+ _EXCLUDE=" -not \( -path \" ${ARG_PATH} /$( echo " $ {ARG_IGNORE} " | sed ' s/ ,/*/g ' ) \" -o -path \" ${ARG_PATH} \/}*\" \)"
315
260
else
316
261
_EXCLUDE=" "
317
262
fi
318
263
319
- find_cmd=" find ${ARG_PATH}${_EXCLUDE} -name '*.hcl' -type f -print0"
264
+ # Store exit code
265
+ echo " 0" > " /tmp/exit.txt"
266
+
267
+ find_cmd=" find ${ARG_PATH}${_EXCLUDE} -name '*.hcl' -type f"
320
268
echo " [INFO] Finding files: ${find_cmd} "
321
- ret=0
322
- while IFS= read -rd ' ' file; do
323
- if ! _fmt " ${ARG_LIST} " " ${ARG_WRITE} " " ${ARG_DIFF} " " ${ARG_CHECK} " " ${file} " ; then
324
- ret=" 1"
325
- fi
326
- done < <( eval " ${find_cmd} " )
327
- exit " ${ret} "
269
+ eval " ${find_cmd} -print0 | xargs -n1 -0 sh -c '\
270
+ if [ -f \"\$ {1}\" ]; then \
271
+ if ! /fmt.sh \" ${ARG_LIST} \" \" ${ARG_WRITE} \" \" ${ARG_DIFF} \" \" ${ARG_CHECK} \" \"\$ {1}\" ; then \
272
+ echo 1 > /tmp/exit.txt; \
273
+ fi \
274
+ fi' --"
275
+
276
+ # Read exit code and return it
277
+ exit " $( cat /tmp/exit.txt ) "
328
278
329
279
# ##
330
280
# ## (3/3) Current directory only
331
281
# ##
332
282
else
333
- find_cmd=" ls ${ARG_PATH} /*.hcl"
334
- echo " [INFO] Finding files: ${find_cmd} "
283
+ echo " [INFO] Finding files: for file in *.hcl; do"
335
284
ret=0
336
- while IFS= read -r file ; do
337
- if ! _fmt " ${ARG_LIST} " " ${ARG_WRITE} " " ${ARG_DIFF} " " ${ARG_CHECK} " " ${file} " ; then
285
+ for file in * .hcl ; do
286
+ if ! /fmt.sh " ${ARG_LIST} " " ${ARG_WRITE} " " ${ARG_DIFF} " " ${ARG_CHECK} " " ${file} " ; then
338
287
ret=" 1"
339
288
fi
340
- done < <( eval " ${find_cmd} " 2> /dev/null )
289
+ done
341
290
exit " ${ret} "
342
291
fi
343
292
fi
0 commit comments