#!/usr/bin/env bash set -euo pipefail VERSION="1.0.0" SCRIPT_NAME="$(basename "$0")" usage() { cat <&2 exit 2 fi LIMIT="$2" shift 2 ;; -*) echo "error: unknown option: $1" >&2; usage >&2; exit 2 ;; *) TARGET="$1"; shift ;; esac done TARGET="${TARGET:-./...}" if ! [[ "$LIMIT" =~ ^[0-9]+$ ]]; then echo "error: --limit must be a non-negative integer, got: $LIMIT" >&2 exit 2 fi if ! command -v go &>/dev/null; then echo "error: go is not installed or not in PATH" >&2 exit 2 fi if ! command -v gofmt &>/dev/null; then echo "error: gofmt is not installed or not in PATH" >&2 exit 2 fi GOFMT_STATUS="pass" GOFMT_FINDINGS=() GOFMT_DIR="${TARGET%%/...}" GOFMT_DIR="${GOFMT_DIR:-.}" UNFORMATTED=$(gofmt -l "$GOFMT_DIR" 2>&1) || true if [[ -n "$UNFORMATTED" ]]; then GOFMT_STATUS="fail" while IFS= read -r f; do [[ -n "$f" ]] && GOFMT_FINDINGS+=("$f") done <<< "$UNFORMATTED" fi GOVET_STATUS="pass" GOVET_OUTPUT="" if ! GOVET_OUTPUT=$(go vet "$TARGET" 2>&1); then GOVET_STATUS="fail" fi LINT_STATUS="skip" LINT_OUTPUT="" if command -v golangci-lint &>/dev/null; then LINT_STATUS="pass" if ! LINT_OUTPUT=$(golangci-lint run "$TARGET" 2>&1); then LINT_STATUS="fail" fi elif ! $FORCE; then echo "error: golangci-lint not installed (use --force to skip)" >&2 exit 2 fi FAILED=0 [[ "$GOFMT_STATUS" == "fail" ]] && FAILED=1 [[ "$GOVET_STATUS" == "fail" ]] && FAILED=1 [[ "$LINT_STATUS" == "fail" ]] && FAILED=1 if $JSON_OUTPUT; then GOFMT_TRUNCATED=false GOFMT_DISPLAY=("${GOFMT_FINDINGS[@]+"${GOFMT_FINDINGS[@]}"}") if [[ $LIMIT -gt 0 && ${#GOFMT_DISPLAY[@]} -gt $LIMIT ]]; then GOFMT_DISPLAY=("${GOFMT_FINDINGS[@]:0:$LIMIT}") GOFMT_TRUNCATED=true fi GOFMT_JSON="[" first=true for f in "${GOFMT_DISPLAY[@]+"${GOFMT_DISPLAY[@]}"}"; do $first || GOFMT_JSON+="," first=false GOFMT_JSON+="\"$(json_escape "$f")\"" done GOFMT_JSON+="]" GOVET_TRUNCATED=false GOVET_DISPLAY="$GOVET_OUTPUT" if [[ $LIMIT -gt 0 && -n "$GOVET_OUTPUT" ]]; then GOVET_ARR=() while IFS= read -r line; do GOVET_ARR+=("$line") done <<< "$GOVET_OUTPUT" if [[ ${#GOVET_ARR[@]} -gt $LIMIT ]]; then GOVET_DISPLAY="" for (( i=0; i