-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.sh
executable file
·100 lines (89 loc) · 1.72 KB
/
update.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
#!/bin/bash
set -e
set -u
set -o pipefail
shopt -s inherit_errexit
IFS=$'\n'
cd "$(dirname "$(readlink -f "$0")")"
for cmd in git curl; do
[ -z "$(command -v "$cmd")" ] && echo "missing $cmd" && exit 1
done
function checkOutBranch() {
local branch="$1"
local folder="${2-}"
[ -z "$folder" ] && folder="$branch"
! git branch | grep -q "$branch" && git fetch origin "$branch":"$branch"
[ -d "$folder" ] && rm -rf "$folder"
mkdir "$folder"
git clone -q "$(pwd)" "$folder"
cd "$folder"
git config --local push.autoSetupRemote true
git fetch -q origin "$branch":"$branch"
git checkout -q "$branch"
cd ..
}
function removeBranch() {
local branch="$1"
local folder="${2-}"
[ -z "$folder" ] && folder="$branch"
rm -rf "$folder"
}
function pushBranch() {
local branch="$1"
git push origin "$branch"
}
case "${1-}" in
checkOutHosts)
checkOutBranch hosts
exit
;;
removeHosts)
removeBranch hosts
exit
;;
pushHosts)
pushBranch hosts
exit
;;
checkOutDeploy)
checkOutBranch deploy
exit
;;
removeDeploy)
removeBranch deploy
exit
;;
pushDeploy)
pushBranch deploy
exit
;;
*)
;;
esac
$0 checkOutHosts
while read -r line; do
grep -q '^\s*#' <<< "$line" && continue
file=$(cut -d',' -f1 <<< "$line")
url=$(cut -d',' -f2 <<< "$line")
curl \
-o hosts/"$file" \
--etag-save hosts/"$file".etag \
--etag-compare hosts/"$file".etag \
"$url"
done < lists.txt
(
cd hosts
for file in *; do
listsfile=$(sed -e 's/\.etag$//' <<< "$file")
grep -q "^$listsfile" < ../lists.txt && continue
rm "$file"
done
)
(
cd hosts
git add .
git commit -m "Update lists" || true
git push -q
)
$0 removeHosts
$0 pushHosts