Add run-formatters.sh script and update GitHub workflow
This commit adds a new bin/run-formatters.sh script that can both check and fix code formatting.
The script handles Go code formatting with gofumpt and webui formatting with Prettier.
The GitHub workflow has been updated to use this script in check mode.
Co-Authored-By: sketch <hello@sketch.dev>
diff --git a/.github/workflows/formatting.yml b/.github/workflows/formatting.yml
index 69d8721..61c5d75 100644
--- a/.github/workflows/formatting.yml
+++ b/.github/workflows/formatting.yml
@@ -24,10 +24,6 @@
working-directory: ./webui
run: npm ci
- - name: Check Prettier formatting
- working-directory: ./webui
- run: npx prettier --check .
-
# Setup for gofumpt
- name: Setup Go
uses: actions/setup-go@v4
@@ -40,5 +36,5 @@
go install mvdan.cc/gofumpt@v0.8.0
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
- - name: Check Go formatting
- run: gofumpt -l . | grep . && { echo "Go files need formatting"; exit 1; } || echo "Go formatting check passed"
+ - name: Check formatting
+ run: bin/run-formatters.sh check
diff --git a/bin/run-formatters.sh b/bin/run-formatters.sh
new file mode 100755
index 0000000..30087e3
--- /dev/null
+++ b/bin/run-formatters.sh
@@ -0,0 +1,58 @@
+#!/bin/bash
+set -e
+
+# Script to run formatters for both Go and webui files
+# Usage: bin/run-formatters.sh [check]
+# If 'check' is provided, only checks formatting without making changes
+
+CHECK_MODE=false
+if [ "$1" = "check" ]; then
+ CHECK_MODE=true
+ echo "Running in check mode (formatting will not be modified)"
+else
+ echo "Running in fix mode (formatting will be modified)"
+fi
+
+# Go formatting with gofumpt
+echo "Checking Go formatting..."
+if [ "$CHECK_MODE" = true ]; then
+ # In check mode, we want to display the files that need formatting and exit with error if any
+ FILES_TO_FORMAT=$(gofumpt -l .)
+ if [ -n "$FILES_TO_FORMAT" ]; then
+ echo "The following Go files need formatting:"
+ echo "$FILES_TO_FORMAT"
+ exit 1
+ else
+ echo "Go formatting check passed"
+ fi
+else
+ # In fix mode, we apply the formatting
+ echo "Fixing Go formatting with gofumpt"
+ gofumpt -w .
+ echo "Go formatting complete"
+fi
+
+# Webui formatting with Prettier
+echo "Checking webui formatting..."
+if [ -d "./webui" ]; then
+ cd webui
+ if [ "$CHECK_MODE" = true ]; then
+ echo "Checking webui formatting with Prettier"
+ if ! npx prettier --check .; then
+ echo "Webui files need formatting"
+ exit 1
+ fi
+ else
+ echo "Fixing webui formatting with Prettier"
+ npx prettier --write .
+ echo "Webui formatting complete"
+ fi
+else
+ echo "No webui directory found, skipping Prettier check"
+fi
+
+if [ "$CHECK_MODE" = true ]; then
+ echo "All formatting checks passed"
+else
+ echo "All formatting has been fixed"
+fi