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