Skip to content

Commit

Permalink
Merge pull request #2 from cytopia/release-0.2
Browse files Browse the repository at this point in the history
Be 100% posix and remove bash as a dependency to shrink size
  • Loading branch information
cytopia authored Jul 6, 2019
2 parents c638404 + 89bd6e4 commit 60e7f45
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 74 deletions.
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ RUN set -eux \
&& chmod +x /usr/bin/terraform

# Use a clean tiny image to store artifacts in
FROM alpine:3.9
FROM alpine:3.8
LABEL \
maintainer="cytopia <cytopia@everythingcli.org>" \
repo="https://github.com/cytopia/docker-terragrunt-fmt"
RUN set -eux \
&& apk add --no-cache bash
COPY --from=builder /usr/bin/terraform /usr/bin/terraform
COPY data/terragrunt-fmt.sh /terragrunt-fmt.sh
COPY data/fmt.sh /fmt.sh

WORKDIR /data
ENTRYPOINT ["/terragrunt-fmt.sh"]
70 changes: 70 additions & 0 deletions data/fmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/sh

# Be strict
set -e
set -u

###
### Inputs
###
_list="${1}"
_write="${2}"
_diff="${3}"
_check="${4}"
_file="${5}"
# shellcheck disable=SC2155
_temp="/tmp/$(basename "${_file}").tf"
_ret=0


###
### Build command (only append if default values are overwritten)
###
_cmd="terraform fmt"
if [ "${_list}" = "0" ]; then
_cmd="${_cmd} -list=false"
else
_cmd="${_cmd} -list=true"
fi
if [ "${_write}" = "1" ]; then
_cmd="${_cmd} -write=true"
else
_cmd="${_cmd} -write=false"
fi
if [ "${_diff}" = "1" ]; then
_cmd="${_cmd} -diff"
fi
if [ "${_check}" = "1" ]; then
_cmd="${_cmd} -check"
fi

###
### Output and execute command
###
echo "${_cmd} ${_file}"
cp -f "${_file}" "${_temp}"
if ! eval "${_cmd} ${_temp}"; then
_ret=1
fi

###
### If -write was specified, copy file back
###
if [ "${_write}" = "1" ]; then
# Get owner and permissions of current file
_UID="$(stat -c %u "${_file}")"
_GID="$(stat -c %g "${_file}")"
_PERM="$(stat -c %a "${_file}")"

# Adjust permissions of temporary file
chown ${_UID}:${_GID} "${_temp}"
chmod ${_PERM} "${_temp}"

# Overwrite existing file
mv -f "${_temp}" "${_file}"
fi

###
### Exit
###
exit "${_ret}"
91 changes: 20 additions & 71 deletions data/terragrunt-fmt.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env bash
#!/bin/sh

# Be strict
set -e
set -u
set -o pipefail


# --------------------------------------------------------------------------------
Expand Down Expand Up @@ -55,60 +54,6 @@ print_usage() {
}


_fmt() {
local list="${1}"
local write="${2}"
local diff="${3}"
local check="${4}"
local file="${5}"
# shellcheck disable=SC2155
local temp="/tmp/$(basename "${file}").tf"
local ret=0

# Build command (only append if default values are overwritten)
local cmd="terraform fmt"
if [ "${list}" = "0" ]; then
cmd="${cmd} -list=false"
else
cmd="${cmd} -list=true"
fi
if [ "${write}" = "1" ]; then
cmd="${cmd} -write=true"
else
cmd="${cmd} -write=false"
fi
if [ "${diff}" = "1" ]; then
cmd="${cmd} -diff"
fi
if [ "${check}" = "1" ]; then
cmd="${cmd} -check"
fi

# Output and execute command
echo "${cmd} ${file}"
cp -f "${file}" "${temp}"
if ! eval "${cmd} ${temp}"; then
ret=$(( ret + 1 ))
fi

# If -write was specified, copy file back
if [ "${write}" = "1" ]; then
# Get owner and permissions of current file
_UID="$(stat -c %u "${file}")"
_GID="$(stat -c %g "${file}")"
_PERM="$(stat -c %a "${file}")"

# Adjust permissions of temporary file
chown ${_UID}:${_GID} "${temp}"
chmod ${_PERM} "${temp}"

# Overwrite existing file
mv -f "${temp}" "${file}"
fi
return "${ret}"
}


# --------------------------------------------------------------------------------
# ENTRYPOINT (EVALUATE ARGUMENTS)
# --------------------------------------------------------------------------------
Expand Down Expand Up @@ -301,7 +246,7 @@ fi
### (1/3) Single file
###
if [ -f "${ARG_PATH}" ]; then
_fmt "${ARG_LIST}" "${ARG_WRITE}" "${ARG_DIFF}" "${ARG_CHECK}" "${ARG_PATH}"
/fmt.sh "${ARG_LIST}" "${ARG_WRITE}" "${ARG_DIFF}" "${ARG_CHECK}" "${ARG_PATH}"
exit "${?}"
else
###
Expand All @@ -311,33 +256,37 @@ else

# evaluate ignore paths
if [ -n "${ARG_IGNORE}" ]; then
_EXCLUDE=" -not \( -path \"${ARG_PATH}/${ARG_IGNORE//,/*\" -o -path \"${ARG_PATH}\/}*\" \)"
_EXCLUDE=" -not \( -path \"${ARG_PATH}/$( echo "${ARG_IGNORE}" | sed 's/,/*/g' )\" -o -path \"${ARG_PATH}\/}*\" \)"
else
_EXCLUDE=""
fi

find_cmd="find ${ARG_PATH}${_EXCLUDE} -name '*.hcl' -type f -print0"
# Store exit code
echo "0" > "/tmp/exit.txt"

find_cmd="find ${ARG_PATH}${_EXCLUDE} -name '*.hcl' -type f"
echo "[INFO] Finding files: ${find_cmd}"
ret=0
while IFS= read -rd '' file; do
if ! _fmt "${ARG_LIST}" "${ARG_WRITE}" "${ARG_DIFF}" "${ARG_CHECK}" "${file}"; then
ret="1"
fi
done < <(eval "${find_cmd}")
exit "${ret}"
eval "${find_cmd} -print0 | xargs -n1 -0 sh -c '\
if [ -f \"\${1}\" ]; then \
if ! /fmt.sh \"${ARG_LIST}\" \"${ARG_WRITE}\" \"${ARG_DIFF}\" \"${ARG_CHECK}\" \"\${1}\"; then \
echo 1 > /tmp/exit.txt; \
fi \
fi' --"

# Read exit code and return it
exit "$( cat /tmp/exit.txt )"

###
### (3/3) Current directory only
###
else
find_cmd="ls ${ARG_PATH}/*.hcl"
echo "[INFO] Finding files: ${find_cmd}"
echo "[INFO] Finding files: for file in *.hcl; do"
ret=0
while IFS= read -r file; do
if ! _fmt "${ARG_LIST}" "${ARG_WRITE}" "${ARG_DIFF}" "${ARG_CHECK}" "${file}"; then
for file in *.hcl; do
if ! /fmt.sh "${ARG_LIST}" "${ARG_WRITE}" "${ARG_DIFF}" "${ARG_CHECK}" "${file}"; then
ret="1"
fi
done < <(eval "${find_cmd}" 2>/dev/null)
done
exit "${ret}"
fi
fi

0 comments on commit 60e7f45

Please sign in to comment.