Skip to content

Commit 4972532

Browse files
committed
Script to validate URLs
1 parent 17fee1c commit 4972532

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

scripts/check_urls.sh

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,38 @@
77

88
set -euo pipefail
99

10+
status=0
1011
green='\e[1;32m'; red='\e[1;31m'; cyan='\e[1;36m'; yellow='\e[1;33m'; reset='\e[0m'
11-
last= rc=0
12-
while IFS=: read -r f u; do
13-
if [ "$f" != "$last" ]; then
14-
[ -n "$last" ] && echo
15-
printf '%s:\n' "$f"
16-
last=$f
12+
last_filepath=
13+
14+
while IFS=: read -r filepath url; do
15+
if [ "$filepath" != "$last_filepath" ]; then
16+
printf '\n%s:\n' "$filepath"
17+
last_filepath=$filepath
18+
fi
19+
code=$(curl -gsLm30 -o /dev/null -w "%{http_code}" -I "$url") || code=000
20+
if [ "$code" -ge 400 ]; then
21+
code=$(curl -gsLm30 -o /dev/null -w "%{http_code}" -r 0-0 -A "Mozilla/5.0" "$url") || code=000
1722
fi
18-
if curl --fail -s -m10 -o /dev/null "$u"; then
19-
printf " ${green}[OK]${reset} ${cyan}%s${reset}\n" "$u"
23+
if [ "$code" -ge 200 ] && [ "$code" -lt 400 ]; then
24+
printf "${green}%s${reset} ${cyan}%s${reset}\n" "$code" "$url"
2025
else
21-
printf "${red}[FAIL]${reset} ${yellow}%s${reset}\n" "$u"
22-
rc=1
26+
printf "${red}%s${reset} ${yellow}%s${reset}\n" "$code" "$url" >&2
27+
status=1
2328
fi
2429
done < <(
25-
git --no-pager grep --no-color -I -o -E 'https?://[^[:space:]<>\")\{]+' \
30+
git --no-pager grep --no-color -I -o -E \
31+
'https?://[^[:space:]<>\")\{\(\$]+' \
2632
-- '*' \
27-
':(exclude).*' ':(exclude)**/.*' ':(exclude)**/*.lock' ':(exclude)**/third-party/**' \
28-
| sed 's/[."\’]$//'
33+
':(exclude).*' \
34+
':(exclude)**/.*' \
35+
':(exclude)**/*.lock' \
36+
':(exclude)**/*.svg' \
37+
':(exclude)**/*.xml' \
38+
':(exclude)**/third-party/**' \
39+
| sed 's/[[:punct:]]*$//' \
40+
| grep -Ev '://(0\.0\.0\.0|127\.0\.0\.1|localhost)([:/])' \
41+
|| true
2942
)
30-
exit $rc
43+
44+
exit $status

0 commit comments

Comments
 (0)