-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
executable file
·61 lines (50 loc) · 1.39 KB
/
pre-commit
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
#!/bin/bash
# SETUP: ln -s ../../pre-commit .git/hooks/pre-commit
_fail(){ echo "::error::$@"; FAIL=1; }
_fail_exit(){ _fail "$@"; exit 1; }
FILE="source"
test_0_list_has_unique_ip_addrs(){
REPEATED=`grep -ve '^#' -ve '^$' $FILE | sort | uniq -d`
if [ -n "$REPEATED" ]; then
echo "${REPEATED}"
_fail "IP addresses are duplicated"
fi
}
# Filter:
# grep -vFf duplicated_list.txt new_list.txt | awk '{print $1}' > final_list.txt
test_1_ip_is_valid(){
# extract only up to first space, exclude comments (hopefully)
IP_LIST=`grep -ve '^#' -ve '^$' $FILE | awk '{print $1}'`
while IFS= read -r ip; do
# count how many dots are in string
dots="${ip//[^.]/}"
if [ "${#dots}" -ne 3 ]; then
_fail "Invalid IP address: $ip"
continue
fi
IFS='.' read -r -a blocks <<< "$ip"
for block in "${blocks[@]}"; do
if ! [[ $block =~ ^[0-9]+(\/[0-9]+)?$ ]]; then
_fail "Invalid IP address: $ip"
continue 2 # Skip to the next IP address
fi
if ((block < 0 || block > 255)); then
_fail "Invalid IP address: $ip"
continue 2 # Skip to the next IP address
fi
done
done <<< $IP_LIST
}
# ---
FAIL=0
function_names=$(declare -F | awk '{print $3}')
for func in $function_names; do
if [[ $func == test_* ]]; then
echo "Executing function: $func"
$func
echo "--------"
fi
done
if [ "$FAIL" = "1" ]; then
exit 1
fi