#!/usr/bin/env bash
# nerf-tg-fmt -- Run terragrunt hcl format to canonically format HCL files. Recurses through all HCL files (.hcl) under the current directory by default.
# Generated from tg manifest. Do not edit directly.
# nerf:threat:read=workspace
# nerf:threat:write=workspace

if [[ "${BASH_VERSINFO[0]:-0}" -lt 4 ]]; then
  echo "error: nerf-tg-fmt requires bash 4+. Found bash ${BASH_VERSION:-unknown}" >&2
  echo "  hint: on macOS, install a newer bash via 'brew install bash'" >&2
  exit 1
fi

set -euo pipefail

_NERF_DRY_RUN=""

usage() {
  cat >&2 <<'EOF'
Usage: nerf-tg-fmt [--check] [--diff]

Switches:
  --check
      Check formatting without modifying files (exits non-zero if files are unformatted)
  --diff
      Print diff between original and modified file versions. Without --check, files are still rewritten in place.

Maps to: terragrunt hcl format --non-interactive <check> <diff>

Run terragrunt hcl format to canonically format HCL files. Recurses through all HCL files (.hcl) under the current directory by default.
EOF
  exit 1
}

CHECK=""
DIFF=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --check) if [[ -n "${CHECK}" ]]; then echo "error: --check can only be specified once" >&2; exit 1; fi; CHECK="true"; shift 1 ;;
    --diff) if [[ -n "${DIFF}" ]]; then echo "error: --diff can only be specified once" >&2; exit 1; fi; DIFF="true"; shift 1 ;;
    --nerf-dry-run) _NERF_DRY_RUN="true"; shift 1 ;;
    -h|--help) usage ;;
    --) shift; break ;;
    *) echo "error: unknown argument: $1" >&2; usage ;;
  esac
done

if [[ "$_NERF_DRY_RUN" == "true" ]]; then
  _NERF_DRY_CMD=(terragrunt hcl format --non-interactive ${CHECK:+"--check"} ${DIFF:+"--diff"})
  printf 'dry-run:'
  for _a in "${_NERF_DRY_CMD[@]}"; do printf " %q" "$_a"; done
  echo
  exit 0
fi

exec terragrunt hcl format --non-interactive ${CHECK:+"--check"} ${DIFF:+"--diff"}
