-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsurreal.sh
executable file
·287 lines (211 loc) · 7.56 KB
/
surreal.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env sh
# Copyright (c) 2022 SurrealDB Ltd.
# This is a simple script that can be downloaded and run from
# https://install.surrealdb.com in order to install the SurrealDB
# command-line tools and database server. It automatically detects
# the host operating platform, and cpu architecture type, and
# downloads the latest binary for the relevant platform.
# This install script attempts to install the SurrealDB binary
# automatically, or otherwise it will prompt the user to specify
# the desired install location.
# NOTE: This version has been modified by removing an extra = in line 224 (see https://github.com/surrealdb/install.surrealdb.com/issues/11, https://github.com/surrealdb/install.surrealdb.com/pull/12)
# in order to succeed with the installation.
# you may need to run "chmod +x surreal.sh"
set -u
VERSION=""
BETA=false
NIGHTLY=false
INTERACTIVE=false
INSTALL_DIR="/usr/local/bin"
SURREALDB_ROOT="https://download.surrealdb.com"
expand() {
case "$1" in
(\~) echo "$HOME";;
(\~/*) echo "$HOME/${1#\~/}";;
(\~[^/]*/*) local user=$(eval echo ${1%%/*}) && echo "$user/${1#*/}";;
(\~[^/]*) eval echo ${1};;
(*) echo "$1";;
esac
}
install() {
echo ""
echo " .d8888b. 888 8888888b. 888888b."
echo "d88P Y88b 888 888 'Y88b 888 '88b"
echo "Y88b. 888 888 888 888 .88P"
echo " 'Y888b. 888 888 888d888 888d888 .d88b. 8888b. 888 888 888 8888888K."
echo " 'Y88b. 888 888 888P' 888P' d8P Y8b '88b 888 888 888 888 'Y88b"
echo " '888 888 888 888 888 88888888 .d888888 888 888 888 888 888"
echo "Y88b d88P Y88b 888 888 888 Y8b. 888 888 888 888 .d88P 888 d88P"
echo " 'Y8888P' 'Y88888 888 888 'Y8888 'Y888888 888 8888888P' 8888888P'"
echo ""
# Parse script arguments
while [ $# -ge 1 ]; do
case "$1" in
-n|--nightly)
NIGHTLY=true
;;
-b|--beta)
BETA=true
;;
-v|--version)
VERSION="$2"
shift
;;
-i|--interactive)
INTERACTIVE=true
;;
*)
INSTALL_DIR="$1"
shift
;;
esac
shift
done
# Check for correct version
if [ "$NIGHTLY" = true ]; then
if [ "$VERSION" != "" ]; then
err "Error: select either a version or the nightly release"
fi
if [ "$BETA" = true ]; then
err "Error: select either the beta release or the nightly release"
fi
fi
if [ "$BETA" = true ]; then
if [ "$VERSION" != "" ]; then
err "Error: select either a version or the beta release"
fi
fi
# Check for necessary commands
command -v uname >/dev/null 2>&1 || {
err "Error: you need to have 'uname' installed and in your path"
}
command -v mkdir >/dev/null 2>&1 || {
err "Error: you need to have 'mkdir' installed and in your path"
}
command -v read >/dev/null 2>&1 || {
err "Error: you need to have 'read' installed and in your path"
}
command -v tar >/dev/null 2>&1 || {
err "Error: you need to have 'tar' installed and in your path"
}
# Check for curl or wget commands
local _cmd
if command -v curl >/dev/null 2>&1; then
_cmd=curl
elif command -v wget >/dev/null 2>&1; then
_cmd=wget
else
err "Error: you need to have 'curl' or 'wget' installed and in your path"
fi
# Fetch the latest SurrealDB version
local _ver
if [ "$NIGHTLY" = true ]; then
echo "Fetching the latest nightly version..."
_ver="nightly"
elif [ "$BETA" = true ]; then
echo "Fetching the latest beta version..."
_ver=$(fetch "$_cmd" "$SURREALDB_ROOT/beta.txt" "Error: could not fetch the latest SurrealDB beta version number")
elif [ "$VERSION" != "" ]; then
echo "Fetching $VERSION..."
_ver="$VERSION"
else
echo "Fetching the latest database version..."
_ver=$(fetch "$_cmd" "$SURREALDB_ROOT/latest.txt" "Error: could not fetch the latest SurrealDB release version number")
fi
# Compute the current system architecture
echo "Fetching the host system architecture..."
local _oss
local _cpu
local _arc
_oss="$(uname -s)"
_cpu="$(uname -m)"
case "$_oss" in
Linux) _oss=linux;;
Darwin) _oss=darwin;;
MINGW* | MSYS* | CYGWIN*) _oss=windows;;
*) err "Error: unsupported operating system: $_oss";;
esac
case "$_cpu" in
arm64 | aarch64) _cpu=arm64;;
x86_64 | x86-64 | x64 | amd64) _cpu=amd64;;
*) err "Error: unsupported CPU architecture: $_cpu";;
esac
_arc="${_oss}-${_cpu}"
# Compute the download file extension type
local _ext
case "$_oss" in
linux) _ext="tgz";;
darwin) _ext="tgz";;
windows) _ext="exe";;
esac
# Define the latest SurrealDB download url
local _url
_url="${SURREALDB_ROOT}/${_ver}/surreal-${_ver}.${_arc}.${_ext}"
# Download and unarchive the latest SurrealDB binary
cd /tmp
echo "Installing surreal-${_ver} for ${_arc}..."
if [ "$_cmd" = curl ]; then
curl --silent --fail --location "$_url" --output "surreal-${_ver}.${_arc}.${_ext}" || {
err "Error: could not fetch the latest SurrealDB file"
}
elif [ "$_cmd" = wget ]; then
wget --quiet "$_url" -O "surreal-${_ver}.${_arc}.${_ext}" || {
err "Error: could not fetch the latest SurrealDB file"
}
fi
tar -zxf "surreal-${_ver}.${_arc}.${_ext}" || {
err "Error: unable to extract the downloaded archive file"
}
# Install the SurrealDB binary into the specified directory
local _loc="$INSTALL_DIR"
mkdir -p "$_loc" 2>/dev/null
if [ ! -d "$_loc" ] || ! touch "$_loc/surreal" 2>/dev/null; then
if [ "$INTERACTIVE" = true ]; then
echo ""
read -p "Where would you like to install the 'surreal' binary [~/.surrealdb]? " _loc
_loc=${_loc:-~/.surrealdb} && _loc=$(expand "$_loc")
else
_loc=~/.surrealdb
fi
mkdir -p "$_loc"
fi
mv "surreal" "$_loc" 2>/dev/null || {
err "Error: we couldn't install the 'surreal' binary into $_loc"
}
# Show some simple instructions
echo ""
echo "SurrealDB successfully installed in:"
echo " ${_loc}/surreal"
echo ""
if [ "${_loc}" != "${INSTALL_DIR}" ]; then
echo "To ensure that surreal is in your \$PATH run:"
echo " PATH=${_loc}:\$PATH"
echo "Or to move the binary to ${INSTALL_DIR} run:"
echo " sudo mv ${_loc}/surreal ${INSTALL_DIR}"
echo ""
fi
echo "To see the command-line options run:"
echo " surreal help"
echo "To start an in-memory database server run:"
echo " surreal start --log debug --user root --pass root memory"
echo "For help with getting started visit:"
echo " https://surrealdb.com/docs"
echo ""
# Exit cleanly
exit 0
}
err() {
echo "$1" >&2 && exit 1
}
fetch() {
if [ "$1" = curl ]; then
curl --silent --fail --location "$2" || {
err "$3"
}
elif [ "$1" = wget ]; then
wget --quiet "$2" || {
err "$3"
}
fi
}
install "$@" || exit 1