forked from pecigonzalo/pre-commit-shfmt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shfmt.sh
executable file
·46 lines (42 loc) · 1.07 KB
/
shfmt.sh
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
#!/usr/bin/env bash
set -e # exit on command errors
set -o nounset # abort on unbound variable
set -o pipefail # capture fail exit codes in piped commands
# set -x # execution tracing debug messages
indentation=""
args=""
while [[ $# -gt 0 ]] && [[ ."$1" == .-* || ."$1" == .--* ]]; do
# Get param and value using parameter expansion, splitting on = or " "
param="${1%[ =]*}"
value="${1#*[ =]}"
shift
case "$param" in
-i | --indent)
indentation="-i $value"
;;
-ci | --case-indent)
args+=" -ci"
;;
-s | --simplify)
args+=" -s"
;;
-bn)
args+=" -bn"
;;
-fn)
args+=" -fn"
;;
-*)
echo "Error: Unknown option: $param" >&2
exit 1
;;
*) # No more options
break 2
;;
esac
done
for file in "$@"; do
if file "$file" | grep -i 'shell script' >/dev/null; then
shfmt -l -w $indentation $args "$file"
fi
done