-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.sh
executable file
·137 lines (126 loc) · 2.54 KB
/
format.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/sh
script_dir="$(
cd "$(dirname "$0")"
pwd -P
)"
# shellcheck source=mainutil.sh
. "$script_dir"/mainutil.sh
# shellcheck source=shellutil.sh
. "$script_dir"/shellutil.sh
# set -o xtrace
node_image=creemama/shellutil-dev:18.16.0-alpine3.17
format() {
run_shfmt "$@"
run_prettier "$@"
run_shellcheck "$@"
}
main() {
local command_help
command_help='docker - Develop inside a Docker container.
format - (or docker-format) Run shfmt, prettier, and shellcheck.
prettier - (or docker-prettier) Run prettier.
shell-format - (or docker-shell-format) Run shfmt and shellcheck.
shfmt - (or docker-shellcheck) Run shfmt.
shellcheck - (or docker-shellcheck) Run shellcheck.'
local commands
commands="$(main_extract_commands "$command_help")"
# shellcheck disable=SC2086
if [ -z "${1:-}" ]; then
main_exit_with_no_command_error "$command_help"
elif [ "$1" = "$(arg 0 $commands)" ]; then
shift
run_docker "$@"
elif string_starts_with "$1" docker-; then
run_docker_command "$@"
elif [ "$1" = "$(arg 1 $commands)" ]; then
shift
format "$@"
elif [ "$1" = "$(arg 2 $commands)" ]; then
shift
run_prettier "$@"
elif [ "$1" = "$(arg 3 $commands)" ]; then
shift
shell_format "$@"
elif [ "$1" = "$(arg 4 $commands)" ]; then
shift
run_shfmt "$@"
elif [ "$1" = "$(arg 5 $commands)" ]; then
shift
run_shellcheck "$@"
else
main_exit_with_invalid_command_error "$1" "$command_help"
fi
}
run_docker() {
docker run -it --rm \
--volume "$(pwd -P):$(pwd -P)" \
--workdir "$(pwd -P)" \
"$node_image" \
sh "$@"
}
run_docker_command() {
local command
command="$(printf %s "${1:-}" | sed -E 's/^docker-//')"
shift
run_docker -c "$script_dir/format.sh $command $*"
}
run_prettier() {
if [ -f node_modules/.bin/prettier ]; then
./node_modules/.bin/prettier --write "${@:-.}"
else
prettier --write "${@:-.}"
fi
}
run_shellcheck() {
if [ -n "${2:-}" ]; then
for arg in "$@"; do
run_shellcheck "$arg"
done
return
fi
local files
if [ -n "${1:-}" ]; then
if [ -d "$1" ]; then
(
cd "$1"
run_shellcheck
)
return
else
files="$1"
fi
else
files='./*.sh'
fi
# shellcheck disable=SC2086
shellcheck -e SC3043 --external-sources $files
}
run_shfmt() {
if [ -n "${2:-}" ]; then
for arg in "$@"; do
run_shfmt "$arg"
done
return
fi
local files
if [ -n "${1:-}" ]; then
if [ -d "$1" ]; then
(
cd "$1"
run_shfmt
)
return
else
files="$1"
fi
else
files='./*.sh'
fi
# shellcheck disable=SC2086
shfmt -w $files
}
shell_format() {
run_shfmt "$@"
run_shellcheck "$@"
}
main "$@"