forked from coding/coding-download-center
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coding-generic-sync.sh
executable file
·75 lines (71 loc) · 2.6 KB
/
coding-generic-sync.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
#!/bin/bash
set -e
if [ "$CODING_GENERIC_REGISTRY" = "" ]; then
echo "WARNING: env CODING_GENERIC_REGISTRY not set, only run download"
elif [ "$CODING_ARTIFACTS_USERNAME" = "" ]; then
echo "WARNING: env CODING_ARTIFACTS_USERNAME not set, only run check and download"
fi
top_dir=$(
cd "$(dirname "$0")"
pwd
)
dl_dir=$top_dir/dl
mkdir -p "$dl_dir"
index_file="$top_dir/index.md"
thead_line_num=$(grep -n "\-|\-" "$index_file" | awk -F: '{print $1}')
offset=$((thead_line_num + 1))
tail -n +$offset "$index_file" | while read -r line; do
i=0
http_code=200
sha256=""
while IFS='|' read -ra tmp; do
for part in "${tmp[@]}"; do
part=$(echo "${part}" | tr -d '[:space:]')
if [ $i -eq 0 ]; then
package=$part
printf "\n%s\n" "$package"
file_prefix="${part%%.*}"
file_suffix=""
if [[ $part == *"."* ]]; then
file_suffix=".""${part#*.}"
fi
elif [ $i -eq 1 ]; then
version=$part
filename=$file_prefix-$version$file_suffix
elif [ $i -eq 2 ]; then
uri=$part
elif [ $i -eq 3 ]; then
sha256=$part
echo "$sha256 $dl_dir/$filename" > "$dl_dir/$filename.sha256sum"
fi
i=$((i + 1))
done
done <<< "$line"
http_code=404
if [ "$CODING_GENERIC_REGISTRY" != "" ]; then
mirror_full_url="${CODING_GENERIC_REGISTRY}$package?version=$version"
echo "check $mirror_full_url"
header=$(curl -sI "$mirror_full_url")
http_code=$(echo "$header" | head -n 1 | awk '{print $2}')
if [ "$http_code" -eq 200 ]; then
echo "skip: file exists on mirror"
fi
fi
if [ "$http_code" -ne 200 ]; then
if [ -f "$dl_dir/$filename" ] && sha256sum -c "$dl_dir/$filename.sha256sum"; then
echo "skip: file exists on local"
else
wget -O "$dl_dir/$filename" "$uri"
sha256sum -c "$dl_dir/$filename.sha256sum"
fi
# coding-generic 自带校验功能,上传成功即可,无需再下载校验。
if [ "$CODING_GENERIC_REGISTRY" != "" ] &&
[ "$CODING_ARTIFACTS_USERNAME" != "" ] &&
[ "$CODING_ARTIFACTS_PASSWORD" != "" ]; then
coding-generic --username="${CODING_ARTIFACTS_USERNAME}:${CODING_ARTIFACTS_PASSWORD}" \
--path="${dl_dir}/${filename}" \
--registry="${CODING_GENERIC_REGISTRY}chunks/${package}?version=${version}"
fi
fi
done
echo 'the end'