| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
set -e |
| 4 |
|
| 5 |
# Default to all packages if none specified |
| 6 |
PACKAGES="${1:-./...}" |
| 7 |
|
| 8 |
echo "==> Checking packages: $PACKAGES" |
| 9 |
|
| 10 |
# Timing arrays |
| 11 |
declare -a STEP_NAMES |
| 12 |
declare -a STEP_TIMES |
| 13 |
|
| 14 |
time_step() { |
| 15 |
local name="$1" |
| 16 |
shift |
| 17 |
local start=$(date +%s.%N) |
| 18 |
"$@" |
| 19 |
local end=$(date +%s.%N) |
| 20 |
local elapsed=$(echo "$end - $start" | bc) |
| 21 |
STEP_NAMES+=("$name") |
| 22 |
STEP_TIMES+=("$elapsed") |
| 23 |
} |
| 24 |
|
| 25 |
# Check gofmt |
| 26 |
run_gofmt() { |
| 27 |
echo "==> Running gofmt..." |
| 28 |
# Convert package pattern to path (e.g., ./hugolib/... -> ./hugolib) |
| 29 |
local path="${PACKAGES%/...}" |
| 30 |
GOFMT_OUTPUT=$(gofmt -l "$path" 2>&1) || true |
| 31 |
if [ -n "$GOFMT_OUTPUT" ]; then |
| 32 |
echo "gofmt found issues in:" |
| 33 |
echo "$GOFMT_OUTPUT" |
| 34 |
exit 1 |
| 35 |
fi |
| 36 |
echo " OK" |
| 37 |
} |
| 38 |
|
| 39 |
# Run staticcheck |
| 40 |
run_staticcheck() { |
| 41 |
local staticcheck_bin |
| 42 |
if command -v staticcheck &> /dev/null; then |
| 43 |
staticcheck_bin=$(command -v staticcheck) |
| 44 |
else |
| 45 |
echo "==> Installing staticcheck..." |
| 46 |
go install honnef.co/go/tools/cmd/staticcheck@latest |
| 47 |
staticcheck_bin="$(go env GOBIN)" |
| 48 |
if [ -z "$staticcheck_bin" ]; then |
| 49 |
staticcheck_bin="$(go env GOPATH)/bin" |
| 50 |
fi |
| 51 |
staticcheck_bin="$staticcheck_bin/staticcheck" |
| 52 |
fi |
| 53 |
echo "==> Running staticcheck..." |
| 54 |
"$staticcheck_bin" $PACKAGES |
| 55 |
echo " OK" |
| 56 |
} |
| 57 |
|
| 58 |
# Run tests |
| 59 |
run_tests() { |
| 60 |
echo "==> Running tests..." |
| 61 |
local output |
| 62 |
if ! output=$(go test -failfast $PACKAGES 2>&1); then |
| 63 |
echo "$output" |
| 64 |
exit 1 |
| 65 |
fi |
| 66 |
echo " OK" |
| 67 |
} |
| 68 |
|
| 69 |
# Run all steps with timing |
| 70 |
TOTAL_START=$(date +%s.%N) |
| 71 |
|
| 72 |
time_step "gofmt" run_gofmt |
| 73 |
time_step "staticcheck" run_staticcheck |
| 74 |
time_step "tests" run_tests |
| 75 |
|
| 76 |
TOTAL_END=$(date +%s.%N) |
| 77 |
TOTAL_ELAPSED=$(echo "$TOTAL_END - $TOTAL_START" | bc) |
| 78 |
|
| 79 |
# Print timing summary |
| 80 |
echo "" |
| 81 |
echo "==> All checks passed!" |
| 82 |
echo "" |
| 83 |
echo "Timing summary:" |
| 84 |
echo "---------------" |
| 85 |
for i in "${!STEP_NAMES[@]}"; do |
| 86 |
printf " %-15s %6.2fs\n" "${STEP_NAMES[$i]}" "${STEP_TIMES[$i]}" |
| 87 |
done |
| 88 |
echo "---------------" |
| 89 |
printf " %-15s %6.2fs\n" "Total" "$TOTAL_ELAPSED" |