Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Terragrunt HCL formatting command #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ function installTerragrunt {

function main {
# Source the other files to gain access to their functions
scriptDir=$(dirname ${0})
scriptDir=$(dirname ${0})
source ${scriptDir}/terragrunt_fmt.sh
source ${scriptDir}/terragrunt_hclfmt.sh
source ${scriptDir}/terragrunt_init.sh
source ${scriptDir}/terragrunt_validate.sh
source ${scriptDir}/terragrunt_plan.sh
Expand All @@ -166,6 +167,10 @@ function main {
cd ${GITHUB_WORKSPACE}/${tfWorkingDir}

case "${tfSubcommand}" in
hclfmt)
installTerragrunt
terragruntHCLFmt ${*}
;;
fmt)
installTerragrunt
terragruntFmt ${*}
Expand Down
55 changes: 55 additions & 0 deletions src/terragrunt_hclfmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash

function terragruntHCLFmt {
# Gather the output of `terragrunt hclfmt`.
echo "fmt: info: checking if Terragrunt HCL files in ${tfWorkingDir} are correctly formatted"
if [ ${tfBinary} != "terragrunt" ]; then
echo "skipping formatting HCL files"
exit 0
fi

fmtOutput=$(${tfBinary} hclfmt --terragrunt-check ${*} 2>&1)
fmtExitCode=${?}

# Exit code of 0 indicates success. Print the output and exit.
if [ ${fmtExitCode} -eq 0 ]; then
echo "hclfmt: info: Terragrunt files in ${tfWorkingDir} are correctly formatted"
echo "${fmtOutput}"
echo
exit ${fmtExitCode}
fi

# Exit code of 2 indicates a parse error. Print the output and exit.
if [ ${fmtExitCode} -eq 1 ]; then
echo "hclfmt: error: failed to format Terragrunt files"
echo "${fmtOutput}"
fi

if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${tfComment}" == "1" ]; then
fmtCommentWrapper="#### \`${tfBinary} hclfmt\` Failed:

\`\`\`
${fmtOutput}
\`\`\`

*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`, Workspace: \`${tfWorkspace}\`*"

fmtCommentWrapper=$(stripColors "${fmtCommentWrapper}")
echo "fmt: info: creating JSON"
fmtPayload=$(echo "${fmtCommentWrapper}" | jq -R --slurp '{body: .}')
fmtCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url)
echo "fmt: info: commenting on the pull request"
echo "${fmtPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${fmtCommentsURL}" > /dev/null
fi

# Write changes to branch
echo "::set-output name=tf_actions_fmt_written::false"
if [ "${tfFmtWrite}" == "1" ]; then
echo "fmt: info: Terraform files in ${tfWorkingDir} will be formatted"
terraform fmt -write=true ${fmtRecursive} "${*}"
fmtExitCode=${?}
echo "::set-output name=tf_actions_fmt_written::true"
fi

exit ${fmtExitCode}
}