Skip to content

Commit d62d6bc

Browse files
Add scrips formatting
1 parent 0f3050d commit d62d6bc

7 files changed

Lines changed: 107 additions & 40 deletions

File tree

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
LICENSE
55
.gitignore
66
test.sh
7+
format.sh

.githooks/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
./format.sh -s

.github/workflows/build.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@ jobs:
3636
- name: Checkout
3737
uses: actions/checkout@v4
3838

39-
- name: Lint shell scripts
40-
run: |
41-
sudo apt-get update && sudo apt-get install -y shellcheck
42-
shellcheck devwork-versions .profile || true
43-
echo "Shell script linting completed"
39+
- name: Check shell script formatting
40+
run: ./format.sh -c
4441

4542
- name: Set up QEMU
4643
uses: docker/setup-qemu-action@v3

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ docker run --rm devwork:24-node devwork-versions
234234

235235
This is a personal project but issues and suggestions are welcome. The image is rebuilt weekly with the latest security updates and Oh My Zsh plugins.
236236

237+
To set up the pre-commit hook that auto-formats shell scripts:
238+
239+
```bash
240+
git config core.hooksPath .githooks
241+
```
242+
237243
## License
238244

239245
MIT License - See LICENSE file for details.

devwork-versions

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
# Helper function to safely get version or return NOT FOUND
66
get_version() {
7-
tool_name="$1"
8-
version_cmd="$2"
7+
tool_name="$1"
8+
version_cmd="$2"
99

10-
if command -v "$(echo "$version_cmd" | awk '{print $1}')" >/dev/null 2>&1; then
11-
version=$(eval "$version_cmd" 2>&1 | head -1 || echo "ERROR")
12-
else
13-
version="NOT FOUND"
14-
fi
10+
if command -v "$(echo "$version_cmd" | awk '{print $1}')" >/dev/null 2>&1; then
11+
version=$(eval "$version_cmd" 2>&1 | head -1 || echo "ERROR")
12+
else
13+
version="NOT FOUND"
14+
fi
1515

16-
printf "%-12s %s\n" "$tool_name:" "$version"
16+
printf "%-12s %s\n" "$tool_name:" "$version"
1717
}
1818

1919
# Display versions

format.sh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/bin/sh
2+
# format.sh - Format shell scripts using shfmt
3+
# Requires: docker (uses mvdan/shfmt container image)
4+
#
5+
# Usage:
6+
# ./format.sh Format all shell scripts
7+
# ./format.sh -s Format only staged files and re-stage them (git hook)
8+
# ./format.sh -c Check formatting without writing (CI)
9+
10+
set -e
11+
12+
ALL_SHELL_FILES=".profile devwork-versions test.sh format.sh"
13+
CHECK=false
14+
STAGED_ONLY=false
15+
16+
GREEN='\033[0;32m'
17+
YELLOW='\033[1;33m'
18+
RED='\033[0;31m'
19+
NC='\033[0m'
20+
21+
log() { printf "${GREEN}[✔]${NC} %s\n" "$1"; }
22+
err() { printf "${RED}[✘]${NC} %s\n" "$1"; }
23+
24+
while getopts "cs" opt; do
25+
case $opt in
26+
c) CHECK=true ;;
27+
s) STAGED_ONLY=true ;;
28+
*) printf "Usage: %s [-c] [-s]\n" "$0" && exit 1 ;;
29+
esac
30+
done
31+
32+
if ! command -v docker >/dev/null 2>&1; then
33+
err "docker not found"
34+
exit 1
35+
fi
36+
37+
if $STAGED_ONLY; then
38+
STAGED=$(git diff --cached --name-only)
39+
FILES=""
40+
for f in $ALL_SHELL_FILES; do
41+
echo "$STAGED" | grep -qx "$f" && FILES="$FILES $f"
42+
done
43+
else
44+
FILES=$ALL_SHELL_FILES
45+
fi
46+
47+
if $CHECK; then
48+
printf "\n${YELLOW}==> Checking shell script formatting (shfmt)${NC}\n"
49+
SHFMT_FLAGS="-l"
50+
else
51+
printf "\n${YELLOW}==> Formatting shell scripts (shfmt)${NC}\n"
52+
SHFMT_FLAGS="-w"
53+
fi
54+
55+
for f in $FILES; do
56+
[ -f "$f" ] || continue
57+
# shellcheck disable=SC2086
58+
docker run --rm -u "$(id -u):$(id -g)" -v "$PWD:/mnt" -w /mnt mvdan/shfmt:v3 $SHFMT_FLAGS -i 2 -ci "$f"
59+
$STAGED_ONLY && git add "$f"
60+
log "$f"
61+
done

test.sh

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,46 @@ GREEN='\033[0;32m'
1717
YELLOW='\033[1;33m'
1818
NC='\033[0m'
1919

20-
log() { printf "${GREEN}[✔]${NC} %s\n" "$1"; }
20+
log() { printf "${GREEN}[✔]${NC} %s\n" "$1"; }
2121
warn() { printf "${YELLOW}[!]${NC} %s\n" "$1"; }
22-
err() { printf "${RED}[✘]${NC} %s\n" "$1"; }
22+
err() { printf "${RED}[✘]${NC} %s\n" "$1"; }
2323

2424
# --- Shellcheck ---
2525
printf "\n${YELLOW}==> Linting shell scripts${NC}\n"
2626
if command -v shellcheck >/dev/null 2>&1; then
27-
shellcheck devwork-versions .profile && log "shellcheck passed" || warn "shellcheck reported issues"
27+
shellcheck devwork-versions .profile && log "shellcheck passed" || warn "shellcheck reported issues"
2828
else
29-
warn "shellcheck not installed, skipping lint"
29+
warn "shellcheck not installed, skipping lint"
3030
fi
3131

3232
# --- Build & test each version ---
3333
for VERSION in $VERSIONS; do
34-
printf "\n${YELLOW}==> Node.js $VERSION${NC}\n"
35-
TAG="devwork:$VERSION-node"
34+
printf "\n${YELLOW}==> Node.js $VERSION${NC}\n"
35+
TAG="devwork:$VERSION-node"
3636

37-
# Build
38-
printf "Building $TAG...\n"
39-
if docker build --build-arg NODE_VERSION="$VERSION" -t "$TAG" .; then
40-
log "Build succeeded"
41-
else
42-
err "Build failed for Node $VERSION"
43-
FAIL=$((FAIL + 1))
44-
continue
45-
fi
37+
# Build
38+
printf "Building $TAG...\n"
39+
if docker build --build-arg NODE_VERSION="$VERSION" -t "$TAG" .; then
40+
log "Build succeeded"
41+
else
42+
err "Build failed for Node $VERSION"
43+
FAIL=$((FAIL + 1))
44+
continue
45+
fi
4646

47-
# Test
48-
printf "Testing $TAG...\n"
49-
if docker run --rm "$TAG" devwork-versions; then
50-
log "Tests passed"
51-
PASS=$((PASS + 1))
52-
else
53-
err "Tests failed for Node $VERSION"
54-
FAIL=$((FAIL + 1))
55-
fi
47+
# Test
48+
printf "Testing $TAG...\n"
49+
if docker run --rm "$TAG" devwork-versions; then
50+
log "Tests passed"
51+
PASS=$((PASS + 1))
52+
else
53+
err "Tests failed for Node $VERSION"
54+
FAIL=$((FAIL + 1))
55+
fi
5656

57-
# Size report
58-
SIZE=$(docker images --format "{{.Size}}" "$TAG" | head -1)
59-
log "Image size: $SIZE"
57+
# Size report
58+
SIZE=$(docker images --format "{{.Size}}" "$TAG" | head -1)
59+
log "Image size: $SIZE"
6060
done
6161

6262
# --- Summary ---

0 commit comments

Comments
 (0)