-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget-cli.sh
More file actions
executable file
·83 lines (70 loc) · 2.52 KB
/
get-cli.sh
File metadata and controls
executable file
·83 lines (70 loc) · 2.52 KB
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
#!/bin/bash
get-cli() {
IS_VERBOSE=false
VERBOSE_FLAG=""
usage() {
printf -v text "%s" \
"get-cli downloads qwiki-cli to the current directory; ask for GitHub token if GITHUB_TOKEN is not set as environmental variable [OPTION...]\n" \
" -v, --verbose shows more info\n" \
" -d, --debug debug API calls by passing verbose flag to curl\n" \
" -u, --update update cli binary in /usr/bin/\n" \
" -h, --help shows this help message\n" \
" -r, --release-tag cli release tag, e.g. 0.1.12, default: latest\n"
printf "$text"
}
OPTS=`getopt -o vduhr: --long verbose,debug,help,update,release-tag: -- "$@"`
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi
eval set -- "$OPTS"
while true; do
case "$1" in
-v | --verbose )
IS_VERBOSE=true
shift ;;
-d | --debug )
DEBUG=true
shift ;;
-u | --update )
UPDATE_USR_BIN=true
shift ;;
-r | --release-tag )
RELEASE=$2
shift 2 ;;
-h | --help )
usage
return
shift ;;
-- )
shift
break ;;
* )
break ;;
esac
done
if [ -z "$GITHUB_TOKEN" ]; then
printf "GitHub token is required, please insert\n"
read TOKEN
if [ -z "$TOKEN" ]; then
exit 1
fi
else
TOKEN="$GITHUB_TOKEN"
fi
if [ -z "$RELEASE" ]; then
RELEASE="latest"
else
RELEASE="tags/${RELEASE}"
fi
ASSETID=$(curl -L${VERBOSE_FLAG}J -H 'Accept: application/json' "https://api.github.com/repos/modell-aachen/qwiki-cli/releases/$RELEASE?access_token=$TOKEN" | grep -Pzo "\"assets\":[\s\S]*?\"id\": \K\d*" | tr -d '\0')
curl -L${VERBOSE_FLAG}JO -H 'Accept: application/octet-stream' "https://api.github.com/repos/modell-aachen/qwiki-cli/releases/assets/$ASSETID?access_token=$TOKEN"
unset TOKEN
$IS_VERBOSE && printf "\nASSETID: $ASSETID for RELEASE: $RELEASE\n\n"
if [ ! -z "$UPDATE_USR_BIN" ]; then
$IS_VERBOSE && printf "replacing /usr/bin/qwiki\n"
mv ./qwiki /usr/bin/qwiki
chmod +x /usr/bin/qwiki
else
$IS_VERBOSE && printf "Update flag not set, not replacing /usr/bin/qwiki\n"
fi
printf "\ndone\n"
}
get-cli $@