-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathformat.sh
More file actions
executable file
Β·200 lines (171 loc) Β· 5.98 KB
/
format.sh
File metadata and controls
executable file
Β·200 lines (171 loc) Β· 5.98 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/bash
set -eu
readonly CYAN='\033[0;36m'
readonly NC='\033[0m'
readonly WHITE_BOLD='\033[1;37m'
print_usage() {
echo -e "${WHITE_BOLD}DESCRIPTION${NC}"
echo -e " Try to fix everything that can be fixed to make the system-tests CI happy."
echo
echo -e "${WHITE_BOLD}USAGE${NC}"
echo -e " ./format.sh [options...]"
echo
echo -e "${WHITE_BOLD}OPTIONS${NC}"
echo -e " ${CYAN}--check${NC} Only performs checks without modifying files. Command unsed in the CI."
echo -e " ${CYAN}--help${NC} Prints this message and exits."
echo
}
COMMAND=fix
while [[ "$#" -gt 0 ]]; do
case $1 in
-c|--check) COMMAND=check ;;
-h|--help) print_usage; exit 0 ;;
*) echo "Invalid argument: ${1:-}"; echo; print_usage; exit 1 ;;
esac
shift
done
if [[ -z "${IN_NIX_SHELL:-}" ]]; then
if [ ! -d "venv/" ]; then
echo "Runner is not installed, installing it (ETA 60s)"
./build.sh -i runner
elif ! diff requirements.txt venv/requirements.txt; then
./build.sh -i runner
fi
source venv/bin/activate
fi
echo "Running mypy type checks..."
if ! mypy --config pyproject.toml; then
echo "Mypy type checks failed. Please fix the errors above. π₯ π π₯"
exit 1
fi
echo "Running ruff formatter..."
if [ "$COMMAND" == "fix" ]; then
ruff format
else
ruff format --check --diff
fi
if [ "$COMMAND" == "fix" ]; then
ruff_args="--fix"
else
ruff_args=""
fi
if ! ruff check $ruff_args; then
echo "ruff checks failed. Please fix the errors above. π₯ π π₯"
exit 1
fi
echo "Checking trailing whitespaces..."
INCLUDE_PATTERN='.*\.(md|yml|yaml|sh|cs|Dockerfile|java|sql|ts|js|php)$'
EXCLUDE_PATTERN='utils/build/virtual_machine'
# Check all files tracked by git, and matching include/exclude patterns
FILES="$(git ls-files | grep -v -E "$EXCLUDE_PATTERN" | grep -E "$INCLUDE_PATTERN" | while read f ; do grep -l ' $' "$f" || true ; done)"
# shim for sed -i on GNU sed (Linux) and BSD sed (macOS)
_sed_i() {
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' -r "$@"
else
sed -i "$@"
fi
}
if [ "$COMMAND" == "fix" ]; then
echo "$FILES" | while read file ; do
if [[ -n "$file" ]]; then
echo "Fixing $file"
_sed_i 's/ *$//g' "$file"
fi
done
else
if [ -n "$FILES" ]; then
echo "Some trailing white spaces has been found, please fix them π₯ π π₯"
echo "$FILES"
exit 1
fi
fi
echo "Running yamlfmt checks..."
if ! which yamlfmt > /dev/null; then
echo "yamlfmt is not installed, installing it (ETA 5s)"
YAMLFMT_VERSION="0.16.0"
YAMLFMT_OS=""
case "$(uname -s)" in
Darwin) YAMLFMT_OS="Darwin" ;;
Linux) YAMLFMT_OS="Linux" ;;
CYGWIN*|MINGW*|MSYS*) YAMLFMT_OS="Windows" ;;
*) echo "Unsupported OS"; return 1 ;;
esac
YAMLFMT_ARCH=""
case "$(uname -m)" in
arm64|aarch64) YAMLFMT_ARCH="arm64" ;;
x86_64) YAMLFMT_ARCH="x86_64" ;;
i386|i686) YAMLFMT_ARCH="i386" ;;
*) echo "Unsupported architecture"; return 1 ;;
esac
YAMLFMT_SHA256=""
case "${YAMLFMT_OS}_${YAMLFMT_ARCH}" in
Darwin_arm64) YAMLFMT_SHA256="fcffb2efdfdd27fb5bb658a8156972fda14f0864f336c181705b98eee5f6c139" ;;
Darwin_x86_64) YAMLFMT_SHA256="740d23864fffcf1865a9e0a221840baae6b5f40b8a20ad2d5e79c1b9de9eaec7" ;;
Linux_arm64) YAMLFMT_SHA256="208b9c0c4e67472e5205d3f826205b2f20da59a180b548cff02621401355bead" ;;
Linux_i386) YAMLFMT_SHA256="1c20a6a7ca58736ba10e5c4fc02743d1163815d38e5332872033e775f9f048a1" ;;
Linux_x86_64) YAMLFMT_SHA256="7819fa7c7e994d239009d30cbd58897149d7e7dd5847aedf7abd19c332298033" ;;
Windows_arm64) YAMLFMT_SHA256="1adc6fa71e6e2fad3da09df409e2454e96a5c4a61a8669a6ae4023c163fc2a14" ;;
Windows_i386) YAMLFMT_SHA256="de013077d923d9064cdd1ffedfd6d56274271772007fe214c6db7afdf571228d" ;;
Windows_x86_64) YAMLFMT_SHA256="dea055eb85a30d923850e46b462bb5f0e8f3ca9aee3b33b76a55f22995224e1b" ;;
*) echo "No known checksum for ${YAMLFMT_OS}_${YAMLFMT_ARCH}"; return 1 ;;
esac
YAMLFMT_URL="https://github.com/google/yamlfmt/releases/download/v${YAMLFMT_VERSION}/yamlfmt_${YAMLFMT_VERSION}_${YAMLFMT_OS}_${YAMLFMT_ARCH}.tar.gz"
curl -Lo "$PWD"/venv/bin/yamlfmt.tar.gz "$YAMLFMT_URL"
# Validate checksum of downloaded archive
if command -v sha256sum > /dev/null; then
echo "$YAMLFMT_SHA256 *$PWD/venv/bin/yamlfmt.tar.gz" | sha256sum --check --strict
elif command -v shasum > /dev/null; then
echo "$YAMLFMT_SHA256 *$PWD/venv/bin/yamlfmt.tar.gz" | shasum -a 256 --check --strict
else
echo "ERROR: no sha256sum or shasum found, cannot verify download"; return 1
fi
tar -xzf "$PWD"/venv/bin/yamlfmt.tar.gz -C "$PWD"/venv/bin/
chmod +x "$PWD"/venv/bin/yamlfmt
fi
echo "Running yamlfmt formatter..."
if [ "$COMMAND" == "fix" ]; then
yamlfmt manifests/
else
yamlfmt -lint manifests/
fi
echo "Running yamllint checks..."
if ! ./venv/bin/yamllint -s manifests/; then
echo "yamllint checks failed. Please fix the errors above. π₯ π π₯"
exit 1
fi
echo "Running parser checks..."
if [ "$COMMAND" == "fix" ]; then
if ! python utils/manifest/format.py; then
echo "Manifest parser failed. Please fix the errors above. π₯ π π₯"
exit 1
fi
else
if ! python utils/manifest/validate.py; then
echo "Manifest parser failed. Please fix the errors above. π₯ π π₯"
exit 1
fi
fi
echo "Running shellcheck checks..."
if ! ./utils/scripts/shellcheck.sh; then
echo "shellcheck checks failed. Please fix the errors above. π₯ π π₯"
exit 1
fi
echo "Running Node.js linters"
# currently only fastify requires linting
# this can be added later
nodejs_dirs=("express" "fastify")
for dir in "${nodejs_dirs[@]}"; do
docker run \
--rm \
-w /app \
-v "$PWD"/utils/build/docker/nodejs/"$dir":/app \
-e NODE_NO_WARNINGS=1 \
node:18-alpine \
sh -c "npm install --silent && npm run --silent ${COMMAND}_lint"
if [ $? -ne 0 ]; then
echo "$dir linter failed. Please fix the errors above. π₯ π π₯"
exit 1
fi
done
echo "All good, the system-tests CI will be happy! β¨ π° β¨"