-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch
executable file
·153 lines (119 loc) · 2.77 KB
/
fetch
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
#!/usr/bin/env bash
set -eu
set -o pipefail
readonly SELF=$(basename "${BASH_SOURCE[0]}")
readonly OS=$(uname)
readonly VERSION=0.0.1
OPT_HELP=
OPT_DEBUG=
OPT_VERBOSE=
API="https://api.github.com"
REPO=
FILE=
VER=
PARSER=
function error {
if [[ "${OS}" == "Darwin" ]]; then
echo "error: ${@}" >&2
else
echo -e "\e[0;31m\e[1merror: \e[0;0m${@}" >&2
fi
exit 1
}
function warn {
if [[ "${OS}" == "Darwin" ]]; then
echo "warning: ${@}" >&2
else
echo -e "\e[0;33mwarning: \e[0;0m${@}" >&2
fi
}
function debug {
if [[ -n "${OPT_DEBUG}" ]]; then
echo '**'
echo \${@}: ${@}
echo \$OPT_HELP: "${OPT_HELP}"
echo \$OPT_DEBUG: "${OPT_DEBUG}"
echo \$OPT_VERBOSE: "${OPT_VERBOSE}"
fi
}
function parse_arguments {
debug ${FUNCNAME[0]} "$@"
local opts=$(getopt -n "${SELF}" --options hdv --longoptions help,debug,verbose -- "$@")
if [[ $? != 0 ]]; then error "failed to parsing arguments."; fi
eval set -- "${opts}"
while true; do
case "$1" in
(-h|--help) OPT_HELP=true; shift ;;
(-d|--debug) OPT_DEBUG=true; shift ;;
(-v|--verbose) OPT_VERBOSE=true; shift ;;
(--) shift; break ;;
(*) break ;;
esac
done
}
function process_arguments {
debug ${FUNCNAME[0]} "$@"
if [[ -n "${OPT_HELP}" || "${#}" -lt 1 ]]; then
display_usage
else
#return 0
REPO="$1"
FILE="$2"
VER="$3"
fi
}
function display_usage {
debug ${FUNCNAME[0]} "$@"
cat << EOF
USAGE:
${SELF} [OPTIONS]... <organization>/<repository> <file> <version>
OPTIONS:
-h, --help Show this help
EXAMPLE:
TOKEN=12345 ./${SELF} themimixcompany/streams streams_unix_X64 latest
EOF
exit 0
}
function check_token {
debug ${FUNCNAME[0]} "$@"
if [[ -z "$TOKEN" ]]; then
echo "TOKEN not set. Exiting."
exit 1
fi
}
function gh_curl {
debug ${FUNCNAME[0]} "$@"
curl -H "Authorization: token $TOKEN" -H "Accept: application/vnd.github.v3.raw" $@
}
function set_parser {
debug ${FUNCNAME[0]} "$@"
if [[ "$VER" = "latest" ]]; then
PARSER=".[0].assets | map(select(.name == \"$FILE\"))[0].id"
else
PARSER=". | map(select(.tag_name == \"$VER\"))[0].assets | map(select(.name == \"$FILE\"))[0].id"
fi
}
function set_asset_id {
debug ${FUNCNAME[0]} "$@"
ASSET_ID=`gh_curl -s $API/repos/$REPO/releases | jq "$PARSER"`
if [[ "$ASSET_ID" = "null" ]]; then
echo "ERROR: version not found $VER"
exit 1
fi
}
function fetch_component {
debug ${FUNCNAME[0]} "$@"
wget --auth-no-challenge --header='Accept:application/octet-stream' \
https://$TOKEN:@api.github.com/repos/$REPO/releases/assets/$ASSET_ID \
-O $FILE
}
function main {
debug ${FUNCNAME[0]} "$@"
parse_arguments "$@"
process_arguments "$@"
check_token
set_parser
set_asset_id
fetch_component
}
main "$@"