Skip to content

Commit d0da84f

Browse files
committedDec 24, 2021
Refactor check_manually to improve readability
1 parent 1641b04 commit d0da84f

File tree

1 file changed

+63
-63
lines changed

1 file changed

+63
-63
lines changed
 

‎check_manually.sh

+63-63
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,78 @@
22

33
SOURCE_FILE=README.md
44

5-
LINKS=$(grep -oP "http.*" "$SOURCE_FILE" \
6-
| sed -e "s|)$||" -e "s|) .*||" \
7-
| grep -v img.shields.io \
8-
| grep -v travis-ci.org)
5+
function check_deprecated_repos() {
6+
GITHUB_LINKS=$(grep -o "https://github.com/.*" "$SOURCE_FILE" \
7+
| sed 's|).*||')
98

10-
mapfile -t LINKS <<< "$LINKS"
9+
mapfile -t GITHUB_LINKS <<< "$GITHUB_LINKS"
1110

12-
for link in "${LINKS[@]}"
13-
do
14-
echo "Testing $link"
15-
STATUS_CODE="$(curl -LI "$link" -o /dev/null -w '%{http_code}\n' -s)"
16-
if [[ "$STATUS_CODE" != "200" ]]
17-
then
18-
FALSE_LINKS+=("$link")
11+
for link in "${GITHUB_LINKS[@]}"; do
12+
echo "Checking if repo $link is not deprecated"
13+
DEPRECATED=$(curl "$link" -s | awk -v depr_repo_res='false' \
14+
'/This repository has been archived by the owner. It is now read-only./ \
15+
{depr_repo_res="true"} END {print depr_repo_res}')
16+
if [[ "$DEPRECATED" == "true" ]]; then
17+
deprecated_repos+=("$link")
1918
fi
20-
done
19+
done
2120

22-
FDROID_LINKS=$(grep -oP "https://f-droid.*" "$SOURCE_FILE" \
23-
| sed "s|)].*||" \
24-
| grep -v "https://f-droid.org/)")
21+
if [ -n "${deprecated_repos[*]}" ]; then
22+
depr_repo_res=1
23+
fi
24+
}
2525

26-
mapfile -t FDROID_LINKS <<< "$FDROID_LINKS"
26+
function check_false_links() {
27+
LINKS=$(grep -oP "http.*" "$SOURCE_FILE" \
28+
| sed -e "s|)$||" -e "s|).*||" \
29+
| grep -v img.shields.io \
30+
| grep -v travis-ci.org)
2731

28-
for link in "${FDROID_LINKS[@]}"
29-
do
30-
echo "Testing $link"
31-
STATUS_CODE="$(curl -LI "$link" -o /dev/null -w '%{http_code}\n' -s)"
32-
if [[ "$STATUS_CODE" != "200" ]]
33-
then
34-
FALSE_LINKS+=("$link")
35-
fi
36-
done
32+
mapfile -t LINKS <<< "$LINKS"
3733

38-
if [ -n "${FALSE_LINKS[*]}" ]
39-
then
40-
clear
41-
echo "Some links weren't reachable."
42-
for link in "${FALSE_LINKS[@]}"
43-
do
44-
echo "$link"
45-
done
46-
exit 1
47-
else
48-
echo "No false link was found"
49-
fi
34+
for link in "${LINKS[@]}"
35+
do
36+
echo "Checking $link"
37+
STATUS_CODE="$(curl -LI "$link" -o /dev/null -w '%{http_code}\n' -s)"
38+
if [[ "$STATUS_CODE" != "200" ]]
39+
then
40+
bad_links+=("$link")
41+
fi
42+
done
5043

51-
GITHUB_LINKS=$(grep -o "https://github.com/.*" README.md \
52-
| sed 's|).*||')
44+
if [ -n "${bad_links[*]}" ]
45+
then
46+
link_res=1
47+
fi
48+
}
5349

54-
mapfile -t GITHUB_LINKS <<< "$GITHUB_LINKS"
50+
function check_links() {
51+
local depr_repo_res=0
52+
local deprecated_repos
53+
check_deprecated_repos
5554

56-
for link in "${GITHUB_LINKS[@]}"
57-
do
58-
echo "Checking if repo $link is not deprecated"
59-
DEPRECATED=$(curl "$link" -s | awk -v result='false' \
60-
'/This repository has been archived by the owner. It is now read-only./ \
61-
{result="true"} END {print result}')
62-
if [[ "$DEPRECATED" == "true" ]]
63-
then
64-
GITHUB_DEPRECATED_LINKS+=("$link")
65-
fi
66-
done
55+
local link_res=0
56+
local bad_links
57+
check_false_links
58+
59+
clear
60+
if [[ link_res -eq "1" ]]; then
61+
echo "Unreachable links were found:"
62+
for link in "${bad_links[@]}"; do
63+
echo "$link"
64+
done
65+
fi
6766

68-
if [ -n "${GITHUB_DEPRECATED_LINKS[*]}" ]
69-
then
70-
clear
71-
echo "Deprecated Github Repos were found:"
72-
for link in "${GITHUB_DEPRECATED_LINKS[@]}"
73-
do
74-
echo "$link"
67+
if [[ depr_repo_res -eq "1" ]]; then
68+
echo "Deprecated repo were found:"
69+
for link in "${deprecated_repos[@]}"; do
70+
echo "$link"
7571
done
76-
exit 1
77-
else
78-
echo "No deprecated repo was found"
79-
fi
72+
fi
73+
74+
if [[ depr_repo_res -eq 0 && link_res -eq 0 ]]; then
75+
echo "All links are validated!"
76+
fi
77+
}
78+
79+
check_links

0 commit comments

Comments
 (0)
Please sign in to comment.