-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from cytopia/release-0.2
Be 100% posix and remove bash as a dependency to shrink size
- Loading branch information
Showing
3 changed files
with
92 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters