-
Notifications
You must be signed in to change notification settings - Fork 4
/
install.sh
executable file
·148 lines (132 loc) · 3.85 KB
/
install.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
#!/bin/sh
set -e
if [ $# -eq 0 ]; then
echo "ERROR: Need to specify the install repository"
exit 1
fi
# eg. release-lab/whatchanged
target=""
owner=""
repo=""
exe_name=""
githubUrl=""
githubApiUrl=""
version=""
get_arch() {
# darwin/amd64: Darwin axetroydeMacBook-Air.local 20.5.0 Darwin Kernel Version 20.5.0: Sat May 8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64 x86_64
# linux/amd64: Linux test-ubuntu1804 5.4.0-42-generic #46~18.04.1-Ubuntu SMP Fri Jul 10 07:21:24 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
a=$(uname -m)
case ${a} in
"x86_64" | "amd64" )
echo "amd64"
;;
"i386" | "i486" | "i586")
echo "386"
;;
"aarch64" | "arm64" | "arm")
echo "arm64"
;;
"mips64el")
echo "mips64el"
;;
"mips64")
echo "mips64"
;;
"mips")
echo "mips"
;;
*)
echo ${NIL}
;;
esac
}
get_os(){
# darwin: Darwin
echo $(uname -s | awk '{print tolower($0)}')
}
# parse flag
for i in "$@"; do
case $i in
-r=*|--repo=*)
target="${i#*=}"
shift # past argument=value
;;
-v=*|--version=*)
version="${i#*=}"
shift # past argument=value
;;
-e=*|--exe=*)
exe_name="${i#*=}"
shift # past argument=value
;;
-g=*|--github=*)
githubUrl="${i#*=}"
shift # past argument=value
;;
*)
# unknown option
;;
esac
done
args=(`echo $target | tr '/' ' '`)
if [ ${#args[@]} -ne 2 ]; then
echo "ERROR: invalid params for repo '$1'"
echo "ERROR: the argument should be format like 'owner/repo'"
exit 1
else
owner=${args[0]}
repo=${args[1]}
fi
if [ -z "$exe_name" ]; then
exe_name=$repo
echo "INFO: file name is not specified, use '$repo'"
echo "INFO: if you want to specify the name of the executable, set flag --exe=name"
fi
if [ -z "$githubUrl" ]; then
githubUrl="https://github.com"
fi
if [ -z "$githubApiUrl" ]; then
githubApiUrl="https://api.github.com"
fi
downloadFolder="${TMPDIR:-/tmp}"
mkdir -p ${downloadFolder} # make sure download folder exists
os=$(get_os)
arch=$(get_arch)
file_name="${exe_name}_${os}_${arch}.tar.gz" # the file name should be download
downloaded_file="${downloadFolder}/${file_name}" # the file path should be download
executable_folder="/usr/local/bin" # Eventually, the executable file will be placed here
# if version is empty
if [ -z "$version" ]; then
asset_path=$(
command curl -L \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
${githubApiUrl}/repos/${owner}/${repo}/releases |
command grep -o "/${owner}/${repo}/releases/download/.*/${file_name}" |
command head -n 1
)
if [[ ! "$asset_path" ]]; then
echo "ERROR: unable to find a release asset called ${file_name}"
exit 1
fi
asset_uri="${githubUrl}${asset_path}"
else
asset_uri="${githubUrl}/${owner}/${repo}/releases/download/${version}/${file_name}"
fi
echo "[1/3] Download ${asset_uri} to ${downloadFolder}"
rm -f ${downloaded_file}
curl --fail --location --output "${downloaded_file}" "${asset_uri}"
echo "[2/3] Install ${exe_name} to the ${executable_folder}"
tar -xz -f ${downloaded_file} -C ${executable_folder}
exe=${executable_folder}/${exe_name}
chmod +x ${exe}
echo "[3/3] Set environment variables"
echo "${exe_name} was installed successfully to ${exe}"
if command -v $exe_name --version >/dev/null; then
echo "Run '$exe_name --help' to get started"
else
echo "Manually add the directory to your \$HOME/.bash_profile (or similar)"
echo " export PATH=${executable_folder}:\$PATH"
echo "Run '$exe_name --help' to get started"
fi
exit 0