-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathinstall.sh
executable file
·204 lines (162 loc) · 3.97 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env bash
set -euo pipefail
GITHUB_REPOSITORY="devmatteini/dra"
info(){
echo -e "$1" >&2
}
error(){
echo -e "$1" >&2
exit 1
}
check_command(){
if ! command -v "$1" &>/dev/null; then
return 1
fi
}
check_dependencies(){
local os=$1
if ! check_command curl && ! check_command wget; then
error "Missing 'curl' and 'wget'"
fi
check_command grep || error "Missing 'grep'"
check_command cut || error "Missing 'cut'"
if [[ $os == "Windows" ]]; then
check_command unzip || error "Missing 'unzip'"
else
check_command tar || error "Missing 'tar'"
fi
}
http_get(){
local url="$1"
local output_path="$2"
if command -v curl &>/dev/null; then
curl --proto =https --tlsv1.2 -sSfL -o "$output_path" "$url"
else
wget --https-only --secure-protocol=TLSv1_2 --quiet -O "$output_path" "$url"
fi
}
load_latest_release(){
local stdout="-"
http_get "https://api.github.com/repos/$GITHUB_REPOSITORY/releases/latest" "$stdout" |
grep tag_name |
cut -d'"' -f4
}
get_os(){
local os
os=$(uname -s)
case "$os" in
MINGW* | Win*) echo "Windows" ;;
*) echo "$os" ;;
esac
}
get_arch(){
uname -m
}
get_target(){
local os=$1
local arch=$2
local system="$arch-$os"
case "$system" in
arm64-Darwin) echo "aarch64-apple-darwin" ;;
aarch64-Linux) echo "aarch64-unknown-linux-gnu" ;;
armv6l-Linux) echo "arm-unknown-linux-gnueabihf" ;;
armv7l-Linux) echo "arm-unknown-linux-gnueabihf" ;;
x86_64-Darwin) echo "x86_64-apple-darwin" ;;
x86_64-Windows) echo "x86_64-pc-windows-msvc" ;;
x86_64-Linux) echo "x86_64-unknown-linux-musl" ;;
*) error "Unsupported system: $system" ;;
esac
}
get_archive_extension(){
local target=$1
case "$target" in
*windows*) echo "zip" ;;
*) echo "tar.gz" ;;
esac
}
download_asset(){
local version=$1
local asset=$2
local temp_dir=$3
local output_path="$temp_dir/$asset"
http_get "https://github.com/$GITHUB_REPOSITORY/releases/download/$version/$asset" "$output_path"
echo "$output_path"
}
extract_archive(){
local asset_path=$1
local output_dir=$2
case "$asset_path" in
*zip) unzip -q -j -d "$output_dir" "$asset_path" ;;
*tar.gz) tar xf "$asset_path" --strip-components=1 -C "$output_dir" ;;
*) error "Unknown archive $asset_path" ;;
esac
}
copy_executable(){
local asset_dir=$1
local destination=$2
local os=$3
if [[ "$os" == "Windows" ]]; then
cp "$asset_dir"/dra.exe "$destination"
else
cp "$asset_dir"/dra "$destination"
fi
}
help(){
cat <<'EOF'
Install latest release of dra from GitHub Releases
USAGE:
install.sh [options]
FLAGS:
-h, --help Display this message
OPTIONS:
--to <DESTINATION> Save dra to custom path [default: current working directory]
EOF
}
installation_completed(){
cat <<'EOF'
Thanks for installing dra!
You can run `dra --help` to get started and see useful examples.
More examples can be found in the documentation:
- https://github.com/devmatteini/dra#usage
- https://github.com/devmatteini/dra#examples
EOF
}
main(){
destination="$PWD"
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
help
exit 0
;;
--to)
destination="$2"
shift
shift
;;
*)
error "Unknown option $1"
;;
esac
done
os=$(get_os)
arch=$(get_arch)
check_dependencies "$os"
target=$(get_target "$os" "$arch")
archive_extension=$(get_archive_extension "$target")
version=$(load_latest_release)
asset="dra-$version-$target.$archive_extension"
info "OS: $arch-$os"
info "Repository: $GITHUB_REPOSITORY"
info "Release: $version"
info "Asset: $asset"
info "\nDownloading $asset"
temp_dir=$(mktemp -d)
asset_path=$(download_asset "$version" "$asset" "$temp_dir")
info "Extracting archive $asset_path"
extract_archive "$asset_path" "$temp_dir"
copy_executable "$temp_dir" "$destination" "$os"
info "dra saved to $destination"
installation_completed
}
main "$@"