Skip to content

Commit 60e7f45

Browse files
authored
Merge pull request #2 from cytopia/release-0.2
Be 100% posix and remove bash as a dependency to shrink size
2 parents c638404 + 89bd6e4 commit 60e7f45

File tree

3 files changed

+92
-74
lines changed

3 files changed

+92
-74
lines changed

Dockerfile

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@ RUN set -eux \
3333
&& chmod +x /usr/bin/terraform
3434

3535
# Use a clean tiny image to store artifacts in
36-
FROM alpine:3.9
36+
FROM alpine:3.8
3737
LABEL \
3838
maintainer="cytopia <[email protected]>" \
3939
repo="https://github.com/cytopia/docker-terragrunt-fmt"
40-
RUN set -eux \
41-
&& apk add --no-cache bash
4240
COPY --from=builder /usr/bin/terraform /usr/bin/terraform
4341
COPY data/terragrunt-fmt.sh /terragrunt-fmt.sh
42+
COPY data/fmt.sh /fmt.sh
4443

4544
WORKDIR /data
4645
ENTRYPOINT ["/terragrunt-fmt.sh"]

data/fmt.sh

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/sh
2+
3+
# Be strict
4+
set -e
5+
set -u
6+
7+
###
8+
### Inputs
9+
###
10+
_list="${1}"
11+
_write="${2}"
12+
_diff="${3}"
13+
_check="${4}"
14+
_file="${5}"
15+
# shellcheck disable=SC2155
16+
_temp="/tmp/$(basename "${_file}").tf"
17+
_ret=0
18+
19+
20+
###
21+
### Build command (only append if default values are overwritten)
22+
###
23+
_cmd="terraform fmt"
24+
if [ "${_list}" = "0" ]; then
25+
_cmd="${_cmd} -list=false"
26+
else
27+
_cmd="${_cmd} -list=true"
28+
fi
29+
if [ "${_write}" = "1" ]; then
30+
_cmd="${_cmd} -write=true"
31+
else
32+
_cmd="${_cmd} -write=false"
33+
fi
34+
if [ "${_diff}" = "1" ]; then
35+
_cmd="${_cmd} -diff"
36+
fi
37+
if [ "${_check}" = "1" ]; then
38+
_cmd="${_cmd} -check"
39+
fi
40+
41+
###
42+
### Output and execute command
43+
###
44+
echo "${_cmd} ${_file}"
45+
cp -f "${_file}" "${_temp}"
46+
if ! eval "${_cmd} ${_temp}"; then
47+
_ret=1
48+
fi
49+
50+
###
51+
### If -write was specified, copy file back
52+
###
53+
if [ "${_write}" = "1" ]; then
54+
# Get owner and permissions of current file
55+
_UID="$(stat -c %u "${_file}")"
56+
_GID="$(stat -c %g "${_file}")"
57+
_PERM="$(stat -c %a "${_file}")"
58+
59+
# Adjust permissions of temporary file
60+
chown ${_UID}:${_GID} "${_temp}"
61+
chmod ${_PERM} "${_temp}"
62+
63+
# Overwrite existing file
64+
mv -f "${_temp}" "${_file}"
65+
fi
66+
67+
###
68+
### Exit
69+
###
70+
exit "${_ret}"

data/terragrunt-fmt.sh

+20-71
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
#!/usr/bin/env bash
1+
#!/bin/sh
22

33
# Be strict
44
set -e
55
set -u
6-
set -o pipefail
76

87

98
# --------------------------------------------------------------------------------
@@ -55,60 +54,6 @@ print_usage() {
5554
}
5655

5756

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-
11257
# --------------------------------------------------------------------------------
11358
# ENTRYPOINT (EVALUATE ARGUMENTS)
11459
# --------------------------------------------------------------------------------
@@ -301,7 +246,7 @@ fi
301246
### (1/3) Single file
302247
###
303248
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}"
305250
exit "${?}"
306251
else
307252
###
@@ -311,33 +256,37 @@ else
311256

312257
# evaluate ignore paths
313258
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}\/}*\" \)"
315260
else
316261
_EXCLUDE=""
317262
fi
318263

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"
320268
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 )"
328278

329279
###
330280
### (3/3) Current directory only
331281
###
332282
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"
335284
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
338287
ret="1"
339288
fi
340-
done < <(eval "${find_cmd}" 2>/dev/null)
289+
done
341290
exit "${ret}"
342291
fi
343292
fi

0 commit comments

Comments
 (0)