-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·447 lines (366 loc) · 14.2 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!/bin/sh
set -e
VERSION="1.28.0"
DIRECTORY="/usr/local/bin"
REGISTRY_URL="https://registry.npmjs.org"
INSTALL_SCRIPT_URL="https://dotenvx.sh"
# ./install.sh
# ___________________________________________________________________________________________________
# | _ |
# | | | | | |
# | __| | ___ | |_ ___ _ ____ ____ __ |
# | / _` |/ _ \| __/ _ \ '_ \ \ / /\ \/ / |
# | | (_| | (_) | || __/ | | \ V / > < |
# | \__,_|\___/ \__\___|_| |_|\_/ /_/\_\ |
# | |
# | |
# | *a better dotenv*–from the creator of [`dotenv`](https://github.com/motdotla/dotenv). |
# | |
# | * run anywhere (cross-platform) |
# | * multi-environment |
# | * encrypted envs |
# | |
# | ## Install |
# | |
# | ```sh |
# | curl -sfS https://dotenvx.sh | sh |
# | ``` |
# | |
# | or self-execute this file: |
# | |
# | ```sh |
# | curl -sfS https://dotenvx.sh > install.sh |
# | chmod +x install.sh |
# | ./install.sh |
# | ``` |
# | |
# | more install examples: |
# | |
# | ```sh |
# | # curl examples |
# | curl -sfS "https://dotenvx.sh" | sudo sh |
# | curl -sfS "https://dotenvx.sh?version=0.44.5" | sh |
# | curl -sfS "https://dotenvx.sh?directory=." | sh |
# | curl -sfS "https://dotenvx.sh?directory=/custom/path&version=0.44.5" | sh |
# | |
# | # self-executing examples |
# | ./install.sh --version=0.44.5 |
# | ./install.sh --directory=. |
# | ./install.sh --directory=/custom/path --version=0.44.5 |
# | ./install.sh --help |
# | ``` |
# | |
# | ## Usage |
# | |
# | ```sh |
# | $ echo "HELLO=World" > .env |
# | $ echo "console.log('Hello ' + process.env.HELLO)" > index.js |
# | |
# | $ node index.js |
# | Hello undefined # without dotenvx |
# | |
# | $ dotenvx run -- node index.js |
# | Hello World # with dotenvx |
# | ``` |
# | |
# | see [`dotenvx`](https://github.com/dotenvx/dotenvx) for extended usage guides. |
# | |
# |_________________________________________________________________________________________________|
# usage ---------------------------------
usage() {
echo "Usage: $0 [options] [command]"
echo ""
echo "install dotenvx – a better dotenv"
echo ""
echo "Options:"
echo " --directory directory to install dotenvx to (default: \"/usr/local/bin\")"
echo " --version version of dotenvx to install (default: \"$VERSION\")"
echo ""
echo "Commands:"
echo " install install dotenvx"
echo " help display help"
}
# machine checks ------------------------
is_version_valid() {
if [ -z "$VERSION" ]; then
echo "[INSTALLATION_FAILED] VERSION ($VERSION) is blank in install.sh script"
echo "? set VERSION to valid semantic semver version and try again"
return 1
fi
local semver_regex="^([0-9]+)\.([0-9]+)\.([0-9]+)$"
if echo "$VERSION" | grep -Eq "$semver_regex"; then
return 0
else
echo "[INSTALLATION_FAILED] VERSION ($VERSION) is not a valid semantic version in install.sh script"
echo "? set VERSION to valid semantic semver version and try again"
return 1
fi
}
is_directory_writable() {
# check installation directory is writable
if [ ! -w "$(directory)" ]; then
echo "[INSTALLATION_FAILED] the installation directory [$(directory)] is not writable by the current user"
echo "? run as root [$(help_sudo_install_command "$0")] or choose a writable directory like your current directory [$(help_customize_directory_command "$0")]"
return 1
fi
return 0
}
is_curl_installed() {
local curl_path="$(which_curl)"
if [ -z "$curl_path" ]; then
echo "[INSTALLATION_FAILED] curl is required and is not installed"
echo "? install curl [$(help_install_curl_command)] and try again"
return 1
fi
return 0
}
is_os_supported() {
local os="$(os)"
case "$os" in
linux) os="linux" ;;
darwin) os="darwin" ;;
*)
echo "[INSTALLATION_FAILED] your operating system ${os} is currently unsupported"
echo "? request support by opening an issue at [https://github.com/dotenvx/dotenvx/issues]"
return 1
;;
esac
return 0
}
is_arch_supported() {
local arch="$(arch)"
case "$arch" in
x86_64) arch="x86_64" ;;
amd64) arch="amd64" ;;
arm64) arch="arm64" ;;
aarch64) arch="aarch64" ;;
*)
echo "[INSTALLATION_FAILED] your architecture ${arch} is currently unsupported - must be x86_64, amd64, arm64, or aarch64"
echo "? request support by opening an issue at [https://github.com/dotenvx/dotenvx/issues]"
return 1
;;
esac
return 0
}
# is_* checks ---------------------------
is_piped() {
[ "$0" = "sh" ] || [ "$0" = "bash" ]
}
is_ci() {
[ -n "$CI" ] && [ $CI != 0 ]
}
is_test_mode() {
[ -n "$TEST_MODE" ] && [ $TEST_MODE != 0 ]
}
is_windows() {
[ "$(os)" = "windows" ]
}
is_installed() {
local flagged_version="$1"
local current_version=$("$(directory)/$(binary_name)" --version 2>/dev/null || echo "0")
# if --version flag passed
if [ -n "$flagged_version" ]; then
if [ "$current_version" = "$flagged_version" ]; then
# return true since version already installed
return 0
else
# return false since version not installed
return 1
fi
fi
# if no version flag passed
if [ "$current_version" != "$VERSION" ]; then
# return false since latest is not installed
return 1
fi
echo "[dotenvx@$current_version] already installed ($(directory)/$(binary_name))"
# return true since version already installed
return 0
}
# helpers -------------------------------
directory() {
local dir=$DIRECTORY
case "$dir" in
~*/*)
dir="$HOME/${dir#\~/}"
;;
~*)
dir="$HOME/${dir#\~}"
;;
esac
echo "${dir}"
return 0
}
os() {
echo "$(uname -s | tr '[:upper:]' '[:lower:]')"
return 0
}
arch() {
echo "$(uname -m | tr '[:upper:]' '[:lower:]')"
return 0
}
os_arch() {
echo "$(os)-$(arch)"
return 0
}
filename() {
echo "dotenvx-$VERSION-$(os_arch).tar.gz"
return 0
}
download_url() {
echo "$REGISTRY_URL/@dotenvx/dotenvx-$(os_arch)/-/dotenvx-$(os_arch)-$VERSION.tgz"
return 0
}
progress_bar() {
if $(is_ci); then
echo "--no-progress-meter"
else
echo "--progress-bar"
fi
return 0
}
binary_name() {
if $(is_windows); then
echo "dotenvx.exe"
else
echo "dotenvx"
fi
return 0
}
# which_* -------------------------------
which_curl() {
local result
result=$(command -v curl 2>/dev/null) # capture the output without displaying it on the screen
echo "$result"
return 0
}
which_dotenvx() {
local result
result=$(command -v dotenvx 2>/dev/null) # capture the output without displaying it on the screen
echo "$result"
return 0
}
# warnings* -----------------------------
warn_of_any_conflict() {
local dotenvx_path="$(which_dotenvx)"
if [ "$dotenvx_path" != "" ] && [ "$dotenvx_path" != "$(directory)/$(binary_name)" ]; then
echo "[DOTENVX_CONFLICT] conflicting dotenvx found at $dotenvx_path" >&2
echo "? we recommend updating your path to include $(directory)" >&2
fi
return 0
}
# help text -----------------------------
help_sudo_install_command() {
if is_piped; then
echo "curl -sfS $INSTALL_SCRIPT_URL | sudo $0"
else
echo "sudo $0"
fi
return 0
}
help_customize_directory_command() {
if is_piped; then
echo "curl -sfS \"$INSTALL_SCRIPT_URL?directory=.\" | $0"
else
echo "$0 --directory=."
fi
return 0
}
help_install_curl_command() {
if command -v apt-get >/dev/null 2>&1; then
echo "sudo apt-get update && sudo apt-get install -y curl"
elif command -v yum >/dev/null 2>&1; then
echo "sudo yum install -y curl"
elif command -v brew >/dev/null 2>&1; then
echo "brew install curl"
elif command -v pkg >/dev/null 2>&1; then
echo "sudo pkg install curl"
else
echo "install curl manually"
fi
return 0
}
# install/run ---------------------------
install_dotenvx() {
# 0. override version
VERSION="${1:-$VERSION}"
# 1. setup tmpdir
local tmpdir=$(command mktemp -d)
local pipe="$tmpdir/pipe"
mkfifo "$pipe"
install_failed_cleanup() {
echo "[INSTALLATION_FAILED] failed to download from registry [$(download_url)]"
echo "? verify the download url and try downloading manually"
rm -r "$tmpdir"
}
# TODO: handle dotenvx.exe when on a windows machine? binary is not package/dotenvx, it's package/dotenvx.exe
# Start curl in the background and redirect output to the pipe
curl $(progress_bar) --fail -L --proto '=https' "$(download_url)" > "$pipe" &
curl_pid=$!
# Start tar in the background to read from the pipe
sh -c "tar xz --directory $(directory) --strip-components=1 -f '$pipe' 'package/$(binary_name)'" &
tar_pid=$!
if ! wait $curl_pid || ! wait $tar_pid; then
install_failed_cleanup
return 1
fi
# 3. clean up
rm -r "$tmpdir"
# warn of any conflict
warn_of_any_conflict
# let user know
echo "[dotenvx@$VERSION] installed successfully ($(directory)/$(binary_name))"
echo "now type: dotenvx help"
return 0
}
run() {
# parse arguments
for arg in "$@"; do
case $arg in
version=* | --version=*)
VERSION="${arg#*=}"
;;
directory=* | --directory=*)
DIRECTORY="${arg#*=}"
;;
help | --help)
usage
return 0
;;
*)
# Unknown option
echo "Unknown option: $arg"
usage
return 1
;;
esac
done
# machine checks
is_version_valid
is_directory_writable
is_curl_installed
is_os_supported
is_arch_supported
# install logic
if [ -n "$VERSION" ]; then
# Check if the specified version is already installed
if is_installed "$VERSION"; then
echo "[dotenvx@$VERSION] already installed ($(directory)/$(binary_name))"
return 0
else
install_dotenvx "$VERSION"
fi
else
if is_installed; then
echo "[dotenvx@$VERSION] already installed ($(directory)/$(binary_name))"
return 0
else
install_dotenvx
fi
fi
}
if ! is_test_mode; then
run "$@"
exit $?
fi
# "thanks for using dotenvx!" - mot