#!/usr/bin/env bash
# nerf-tf-fmt -- Run terraform fmt to canonically format Terraform files. Operates on the current directory by default; pass a target directory positionally to format elsewhere under the workspace.
# Generated from tf manifest. Do not edit directly.
# nerf:threat:read=workspace
# nerf:threat:write=workspace

if [[ "${BASH_VERSINFO[0]:-0}" -lt 4 ]]; then
  echo "error: nerf-tf-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-tf-fmt [-check] [-recursive] [-diff] [<directory>]

Switches:
  -check
      Check formatting without modifying files (exits non-zero if files are unformatted)
  -recursive
      Process files in subdirectories
  -diff
      Display diffs of formatting changes

Arguments:
  <directory>
      Subdirectory of the workspace to format (default current)

Maps to: terraform fmt <check> <recursive> <diff> <directory>

Run terraform fmt to canonically format Terraform files. Operates on the current directory by default; pass a target directory positionally to format elsewhere under the workspace.
EOF
  exit 1
}

CHECK=""
RECURSIVE=""
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 ;;
    -recursive) if [[ -n "${RECURSIVE}" ]]; then echo "error: -recursive can only be specified once" >&2; exit 1; fi; RECURSIVE="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 ;;
    *) break ;;
  esac
done

_DIRECTORY_SET=""
if [[ $# -gt 0 ]]; then
  DIRECTORY="$1"
  _DIRECTORY_SET=true
  shift
else
  DIRECTORY=""
fi
if [[ $# -gt 0 ]]; then
  echo "error: nerf-tf-fmt: unexpected extra arguments: $*" >&2
  echo "  hint: switches and options must come before positional arguments" >&2
  exit 1
fi

_nerf_check_path() {
  local _label=$1 _input=$2 _tests=$3
  local _cwd _canonical
  case "$_input" in
    *$'\n'*|*$'\r'*|*$'\t'*)
      echo "error: nerf-tf-fmt: ${_label}: contains illegal control character" >&2
      echo "  hint: paths must not contain newlines, carriage returns, or tabs" >&2
      return 1 ;;
  esac
  _cwd=$(realpath -- "$PWD") || {
    echo "error: nerf-tf-fmt: failed to canonicalize cwd '$PWD'" >&2
    echo "  hint: invoke from a valid directory" >&2
    return 1
  }
  _canonical=$(realpath -m -- "$_input") || {
    echo "error: nerf-tf-fmt: ${_label}: failed to canonicalize '${_input}'" >&2
    echo "  hint: pass a syntactically valid path" >&2
    return 1
  }
  if [[ ",$_tests," == *",under_cwd,"* ]]; then
    # Skip the prefix check when cwd is root: every absolute path qualifies, and the
    # naive prefix comparison would build "//" and reject otherwise-valid paths.
    if [[ "$_cwd" != "/" && "$_canonical" != "$_cwd" && "$_canonical" != "$_cwd"/* ]]; then
      echo "error: nerf-tf-fmt: ${_label}: 'under_cwd' failed: '${_input}'" >&2
      echo "  resolves to '${_canonical}', not under '${_cwd}'" >&2
      echo "  hint: pass a path inside the current workspace" >&2
      echo "  hint: symlinks are followed -- if the link's target is outside the workspace it is rejected" >&2
      return 1
    fi
  fi
  if [[ ",$_tests," == *",exists,"* ]] && [[ ! -e "$_input" ]]; then
    echo "error: nerf-tf-fmt: ${_label}: 'exists' failed: '${_input}' does not exist" >&2
    echo "  hint: create the path or pass an existing one" >&2
    return 1
  fi
  if [[ ",$_tests," == *",not_exists,"* ]] && [[ -e "$_input" ]]; then
    echo "error: nerf-tf-fmt: ${_label}: 'not_exists' failed: '${_input}' already exists" >&2
    echo "  hint: choose a different path or remove the existing one first" >&2
    return 1
  fi
  if [[ ",$_tests," == *",file,"* ]] && [[ ! -f "$_input" ]]; then
    echo "error: nerf-tf-fmt: ${_label}: 'file' failed: '${_input}' is not a regular file" >&2
    echo "  hint: pass a regular file path (not a directory, symlink-to-dir, device, or missing path)" >&2
    return 1
  fi
  if [[ ",$_tests," == *",dir,"* ]] && [[ ! -d "$_input" ]]; then
    echo "error: nerf-tf-fmt: ${_label}: 'dir' failed: '${_input}' is not a directory" >&2
    echo "  hint: pass a directory path (not a regular file or missing path)" >&2
    return 1
  fi
  if [[ ",$_tests," == *",symlink,"* ]] && [[ ! -L "$_input" ]]; then
    echo "error: nerf-tf-fmt: ${_label}: 'symlink' failed: '${_input}' is not a symlink" >&2
    echo "  hint: pass a symbolic link (the test does not follow the link)" >&2
    return 1
  fi
  if [[ ",$_tests," == *",not_symlink,"* ]] && [[ -L "$_input" ]]; then
    echo "error: nerf-tf-fmt: ${_label}: 'not_symlink' failed: '${_input}' is a symlink" >&2
    echo "  hint: pass a real path, not a symlink (the test does not follow the link)" >&2
    return 1
  fi
  if [[ ",$_tests," == *",readable,"* ]] && [[ ! -r "$_input" ]]; then
    echo "error: nerf-tf-fmt: ${_label}: 'readable' failed: '${_input}' is not readable" >&2
    echo "  hint: check filesystem permissions for the current user" >&2
    return 1
  fi
  if [[ ",$_tests," == *",writable,"* ]] && [[ ! -w "$_input" ]]; then
    echo "error: nerf-tf-fmt: ${_label}: 'writable' failed: '${_input}' is not writable" >&2
    echo "  hint: check filesystem permissions for the current user" >&2
    return 1
  fi
  if [[ ",$_tests," == *",executable,"* ]] && [[ ! -x "$_input" ]]; then
    echo "error: nerf-tf-fmt: ${_label}: 'executable' failed: '${_input}' is not executable" >&2
    echo "  hint: check filesystem permissions for the current user" >&2
    return 1
  fi
}

if [[ -n "${_DIRECTORY_SET}" ]] && [[ "${DIRECTORY}" == -* ]]; then
  echo "error: nerf-tf-fmt: <directory> cannot start with '-'" >&2
  echo "  hint: use -- before positional arguments if needed" >&2
  exit 1
fi

if [[ -n "${_DIRECTORY_SET}" ]]; then
  _nerf_check_path 'argument <directory>' "${DIRECTORY}" 'under_cwd' || exit 1
fi

if [[ "$_NERF_DRY_RUN" == "true" ]]; then
  _NERF_DRY_CMD=(terraform fmt ${CHECK:+"-check"} ${RECURSIVE:+"-recursive"} ${DIFF:+"-diff"} ${_DIRECTORY_SET:+"$DIRECTORY"})
  printf 'dry-run:'
  for _a in "${_NERF_DRY_CMD[@]}"; do printf " %q" "$_a"; done
  echo
  exit 0
fi

exec terraform fmt ${CHECK:+"-check"} ${RECURSIVE:+"-recursive"} ${DIFF:+"-diff"} ${_DIRECTORY_SET:+"$DIRECTORY"}
