#!/usr/bin/env bash set -o errexit set -o nounset fail() { echo "$1" >&2 exit 1 } preflight() { local cmd=$1 local msg="%s not found: install it or use the --no-verify flag for the git commit to avoid the running this pre-commit hook.\n" command -v $cmd >/dev/null 2>&1 || fail \ "$(printf "$msg" $cmd)" } # # preflight checks # preflight terraform preflight yq # terraform fmt -check tf_files=($(git diff --cached --name-only --diff-filter=ACM -- '*.tf' '*.tfvars')) if [ ${#tf_files[@]} -gt 0 ]; then # terraform fmt -check returns a non-zero exit status and list of files to be # formatted: override that exit status so this entire script doesn't exit prematurely output=$(printf '%s\n' "${tf_files[@]}" | xargs terraform fmt -check 2>&1 || true) if [ -n "$output" ]; then printf 'The following files are incorrectly formated: run `terraform fmt` to fix them.\n' >&2 fail $output fi fi # yq eval yaml_files=($(git diff --cached --name-only --diff-filter=ACM -- '*.yml' '*.yaml')) if [ ${#yaml_files[@]} -gt 0 ]; then broken_files=() for f in ${yaml_files[@]}; do yq --exit-status 'tag == "!!map" or tag== "!!seq"' >/dev/null 2>&1 $f || broken_files+=($f) done if [ ${#broken_files[@]} -gt 0 ]; then echo 'The following files appear to contain invalid yaml:' >&2 fail "$(printf '%s\n' ${broken_files[@]})" # echo 'The following files appear to contain invalid yaml:' >&2 # printf '%s\n' ${broken_files[@]} >&2 # exit 1 fi fi