#!/usr/bin/env bash

set -Eeuo pipefail

readonly SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
readonly WORKSPACE_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)"

cd -- "${WORKSPACE_ROOT}"

unformatted="$(gofmt -l all module)"
if [[ -n "${unformatted}" ]]; then
  printf 'error: gofmt required:\n%s\n' "${unformatted}" >&2
  exit 1
fi

mapfile -t modules < <(go list -m -f '{{.Dir}}')
for module_dir in "${modules[@]}"; do
  echo "==> ${module_dir}"
  (
    cd -- "${module_dir}"
    if [[ "${SKIP_VET:-0}" != "1" ]]; then
      go vet ./...
    fi
    go test ./...
  )
done

echo "Workspace verification completed successfully."
