-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathghdl.sh
executable file
·156 lines (136 loc) · 3.38 KB
/
ghdl.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env bash
#
# GitHub File Downloader
#
#/ Usage:
#/ ./ghdl.sh [-r] <url>
#/
#/ Options:
#/ <url> GitHub URL
#/ -r optional, recursively download all files under each directory
#/ -h | --help display this help message
set -e
set -u
usage() {
printf "%b\n" "$(grep '^#/' "$0" | cut -c4-)" >&2 && exit 1
}
set_var() {
_GITHUB_URL="https://github.com"
_SCRIPT_PATH=$(dirname "$0")
}
set_command() {
_CURL="$(command -v curl)" || command_not_found "curl" "https://curl.haxx.se/download.html"
}
set_args() {
expr "$*" : ".*--help" > /dev/null && usage
_RECURSIVE_MODE=false
while getopts ":hr" opt; do
case $opt in
r)
_RECURSIVE_MODE=true
;;
h)
usage
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
;;
esac
done
shift $((OPTIND-1))
_URL="$@"
}
print_info() {
# $1: info message
printf "%b\n" "\033[32m[INFO]\033[0m $1" >&2
}
print_warn() {
# $1: warning message
printf "%b\n" "\033[33m[WARNING]\033[0m $1" >&2
}
print_error() {
# $1: error message
printf "%b\n" "\033[31m[ERROR]\033[0m $1" >&2
exit 1
}
command_not_found() {
# $1: command name
# $2: installation URL
if [[ -n "${2:-}" ]]; then
print_error "$1 command not found! Install from $2"
else
print_error "$1 command not found!"
fi
}
check_var() {
if [[ -z "${_URL:-}" ]]; then
usage
fi
}
get_page() {
# $1: page URL
$_CURL -sS "$1" || print_error "Cannot get page from $1"
}
get_file() {
# $1: file or directory URL
local type path
type=$(awk -F '/' '{print $4}' <<< "$1")
if [[ "$type" == "blob" ]]; then
local pre post flink fname fpath upath
pre=$(awk -F '/' '{printf "%s/%s", $2, $3}' <<< "$1")
post=$(awk -F '/blob/' '{print $2}' <<< "$1")
flink="${_GITHUB_URL}/${pre}/raw/${post}"
fpath="$(dirname "$_SCRIPT_PATH/${post#*/}")"
fname="$(basename "$post")"
upath="$(dirname "$(get_path_from_url "$_URL")")/"
if [[ ! "$upath" =~ ^\. ]]; then
path="$(sed -E 's:'"$upath"'::' <<< "$fpath")"
else
path="$fpath"
fi
print_info "Downloading $flink"
$_CURL -sSL -o "$path/$fname" "$flink" --create-dirs || print_warn "Error occurs when downloading $flink"
elif [[ "$type" == "tree" ]]; then
if [[ "$_RECURSIVE_MODE" == true ]]; then
path="$_SCRIPT_PATH/$(get_path_from_url "$1")"
download_content "$_GITHUB_URL/$1" "$path"
else
print_info "Skip directory $1"
fi
else
print_warn "Skip unknown file type $1"
fi
}
list_content() {
# $1: GitHub URL
get_page "$1" \
| grep span \
| grep js-navigation-open \
| grep title= \
| sed -E 's/.*href="//' \
| awk -F '">' '{print $1}'
}
get_path_from_url() {
# $1: GitHub URL
local p
p=$(awk -F '/tree/' '{print $2}' <<< "$1")
echo "${p#*/}"
}
download_content(){
# $1: GitHub URL
local u
for u in $(list_content "$1"); do
get_file "$u"
done
}
main() {
set_args "$@"
set_command
set_var
check_var
download_content "$_URL"
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi