-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·474 lines (395 loc) · 13.4 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·474 lines (395 loc) · 13.4 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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#!/bin/sh
temp_d=$(mktemp -d)
trap 'rm -rf "${temp_d}"' EXIT
releases_file="${temp_d}"/releases.json
downloaded_cli_tar_file="${temp_d}"/chs-dev.tar.gz
backup_cli="${temp_d}"/chs-dev.bak
backup_symlink="${temp_d}"/chs-dev.sym.bak
F_FG_RED="$(tput setaf 1)"
F_FG_GREEN="$(tput setaf 2)"
F_FG_YELLOW="$(tput setaf 3)"
F_FG_CYAN="$(tput setaf 6)"
F_BOLD="$(tput bold)"
F_RESET="$(tput sgr0)"
# Prints usage
usage() {
cat <<EOF
install.sh [options] [COMMAND]
Installs the chs-dev CLI tool.
OPTIONS
-------
-B - builds and installs the local directory
-d <directory> - local directory to install CLI to (Defaults to ~/.chs-dev)
-f - forces the installation - will overwrite existing installation if it
exists. Without the flag, user will have to confirm reinstallation
-l <loglevel> - sets the log level to set logging output, valid options are DEBUG,
INFO, WARN, ERROR (defaults to INFO)
-n <name> - sets the name of the symlink file (defaults to chs-dev)
-S - when set does not create symlink to binary
-s <path-to-symlink-loc> - Sets the path to the place to output the symlink to (Defaults to ~/.companies_house_config/bin)
-v <version> - installs a specific version (Defaults to latest)
-W - suppresses the warning about the chs-dev not being on the path
-h print this message and exit
ARGUMENTS
---------
COMMAND - Specifies the action to do, must be install or uninstall. Defaults to installl
EOF
}
# Logs error and then exits process with error exit code
panic() {
log ERROR "${1:?message required}" >&2
revert_uninstall
exit 1
}
# Logs messages to terminal and controls logging
log() {
log_level="${1:?level required}"
log_message="${2:?message required}"
case "${LOGGING_LEVEL}" in
DEBUG) ;;
INFO)
if [ "${log_level}" = 'DEBUG' ]; then
return 0
fi
;;
WARN*)
if [ "${log_level}" = 'DEBUG' ] || [ "${log_level}" = 'INFO' ]; then
return 0
fi
;;
ERROR)
if [ ! "${log_level}" = "ERROR" ]; then
return 0
fi
;;
esac
case "${log_level}" in
DEBUG) log_colour="" ;;
INFO) log_colour="${F_FG_GREEN}" ;;
WARN*) log_colour="${F_FG_YELLOW}" ;;
ERROR) log_colour="${F_FG_RED}" ;;
*) panic "Invalid log level: ${log_level}" ;;
esac
printf '[%s] - [%s] - %s\n' "${F_FG_CYAN}$(date +'%Y-%m-%dT%H:%M:%S')${F_RESET}" "${log_colour}${F_BOLD}${log_level}${F_RESET}" "${log_message}"
}
# Checks that the requested version is valid i.e. there is a release with the
# name of the version supplied
# RETURNS
# 0 - version is valid
# 1 - version is not valid
validate_version() {
log DEBUG 'validating version'
{
jq -r '.[] | .name' "${releases_file}"
printf -- '%s' "$?" >"${temp_d}"/jq_status
} |
{
grep -q "${VERSION}"
printf -- '%s' "$?" >"${temp_d}"/grep_status
}
if ! grep -q '^0$' "${temp_d}"/jq_status "${temp_d}"/grep_status; then
log ERROR "Version ${VERSION} not found"
return 1
fi
}
# Sets the variable OS to the name of the operating system. Panics if the
# operating system is unsupported by the CLI (i.e. missing compiled CLI
# artifacts). Panics if not OSX or Linux compatible
determine_operating_system() {
log DEBUG 'determining operating system'
uname_out="$(uname -a | tr '[:upper:]' '[:lower:]')"
case "${uname_out}" in
*microsoft*) OS="linux" ;;
linux*) OS="linux" ;;
darwin*) OS="darwin" ;; # Apple Mac
*)
panic "Unsupported operating system. Must be Linux or Mac."
;;
esac
}
# Sets the variable CHIPSET with the name of the current chipset architecture.
# Panics if the chipset is not arm64 or x86
determine_chipset() {
log DEBUG 'determining chipset'
uname_out="$(uname -m | tr '[:upper:]' '[:lower:]')"
case "${uname_out}" in
aarch64 | arm64) CHIPSET=arm64 ;;
x86_64*) CHIPSET=x64 ;;
*)
panic "Unsupported chipset. Supports: arm64 or x64 architectures."
;;
esac
}
# Downloads the releases to local file
download_releases() {
curl --fail-with-body -s -L \
-o "${releases_file}" \
-H 'Accept: application/vnd.github+json' \
https://api.github.com/repos/companieshouse/chs-dev/releases
}
# Interrogates the release to determine the download URL for the CLI tarball
determine_download_url() {
release_file="${temp_d}"/release.json
if [ "${VERSION}" = "${DEFAULT_VERSION}" ]; then
log DEBUG 'Fetching latest release'
curl --fail-with-body -s -L -H \
'Accept: application/vnd.github+json' \
https://api.github.com/repos/companieshouse/chs-dev/releases/latest >"${release_file}" || return $?
else
log DEBUG "Looking up version ${VERSION}"
jq \
--arg version "${VERSION}" \
'.[] | select(.name == $version)' \
"${releases_file}" >"${release_file}"
fi
install_version="$(jq -r '.name' "${release_file}")"
log INFO "Installing version ${install_version}"
jq -r --arg expected_suffix "-${OS}-${CHIPSET}.tar.gz" \
'.assets[] | select(.name | endswith($expected_suffix)) | .browser_download_url' \
"${release_file}" >"${temp_d}"/download_url
}
# Downloads the tarball to temporary directory.
download_cli_tarball() {
log DEBUG "Downloading - '${DOWNLOAD_URL}'"
curl --fail-with-body -s \
-o "${downloaded_cli_tar_file}" \
-L \
-H 'Accept: application/tar+gzip' \
"${DOWNLOAD_URL}"
}
# If Symlink directory is not found on path then output instructions on how to
# add to path
add_chs_dev_cli_to_path() {
if printf -- '%s' "${PATH}" | grep -qv -- "${SYMLINK_DIRECTORY}" && [ -z "${SUPPRESS_NOT_ON_PATH_WARN}" ]; then
log WARN "Does not look like chs-dev will be on your path"
cat <<EOF
If you haven't installed via dev-env-setup you will need to modify your shell
profile to add the following:
export PATH="\${PATH}":${SYMLINK_DIRECTORY}
This can be added to your ~/.bashrc or ~/.zshrc file (depending on your shell)
EOF
fi
}
# Given the installation directory, unzip the downloaded tarball and extract
# to the installation directory and then create symlink to chs-dev binary
install_cli_tarball() {
log DEBUG "installing tarball..."
return_status=0
if [ ! -d "${INSTALLATION_DIRECTORY}" ]; then
log DEBUG "Creating ${INSTALLATION_DIRECTORY}"
mkdir -p "${INSTALLATION_DIRECTORY}" || return_status=$?
fi
if [ "${return_status}" -eq 0 ]; then
log DEBUG "Unzipping tarball..."
tar -xf "${downloaded_cli_tar_file}" \
-C "${temp_d}" || return_status=$?
fi
if [ "${return_status}" -eq 0 ]; then
(
cd "${temp_d}"/chs-dev/ || exit 1
tar c .
) | (
cd "${INSTALLATION_DIRECTORY}" || exit 1
tar xf -
) || return_status=$?
if [ "${return_status}" -ne 0 ]; then
panic 'Unable to install CLI to destination'
fi
fi
if [ -z "${NO_SYMLINK}" ]; then
log DEBUG "Installing symlink"
mkdir -p "${SYMLINK_DIRECTORY}" || return_status=$?
if [ "${return_status}" -eq 0 ]; then
ln -s "${INSTALLATION_DIRECTORY}"/bin/chs-dev "${SYMLINK_DIRECTORY}"/"${SYMLINK_NAME}" || return_status=$?
fi
if [ "${return_status}" -eq 0 ]; then
add_chs_dev_cli_to_path || return_status=$?
fi
else
log WARN "Skipping symlink installation"
printf -- '\n'
cat <<'EOF'
It is unlikely that chs-dev will be on your path, therefore you will have to
add it to your shell profile. Your options:
* Create symlink to the `bin/chs-dev` executable in your local installation
directory in a directory which you can add to your path
* Create a function/alias in your shell profile to the absolute path to the
`bin/chs-dev` executable in your local installation directory
EOF
fi
return "${return_status}"
}
# Function which prompts user to confirm they are happy to continue
# ARGUMENTS
# 1 - prompt - prompt to make to the user for confirmation
# RETURNS
# 0 - Action confirmed
# 1 - User has explicitly not confirmed the action
user_confirm_action() {
prompt="${1:?prompt required}"
if [ -z "${FORCE}" ]; then
printf -- '%s\n-- To continue press y (or to exit press another key)\n' "${prompt}"
# explicitly read from tty since stdin may be a pipe
read -r user_confirmation </dev/tty
case "${user_confirmation}" in
[Yy]) return 0 ;;
*)
log WARN "User not entered 'y' bailing out..."
return 1
;;
esac
else
log DEBUG 'In force mode autoconfirming'
return 0
fi
}
# Runs through the steps of installing the chs-dev cli
install() {
# Try to find chs-dev and if present offer to uninstall it before
# installing the latest version
if [ -L "${SYMLINK_DIRECTORY}"/"${SYMLINK_NAME}" ] ||
[ -d "${INSTALLATION_DIRECTORY}" ]; then
chs_dev_version_installed="$("${SYMLINK_NAME}" --version || : "")"
upgrade_downgrade="upgrade"
if [ -n "${chs_dev_version_installed}" ]; then
chs_dev_version_number_installed="$(grep -oE 'chs-dev/[0-9]+\.[0-9]+\.[0-9]+' <<< "${chs_dev_version_installed}" | cut -d'/' -f2)"
if [[ -n "${VERSION}" && "${VERSION}" != 'latest' ]]; then
# Compare versions to determine upgrade or downgrade
if [[ $(sort -V <<< "${chs_dev_version_number_installed}"$'\n'"${VERSION}" | head -n1) = "${VERSION}" && "${chs_dev_version_number_installed}" != "${VERSION}" ]]; then
upgrade_downgrade="downgrade"
else
upgrade_downgrade="upgrade"
fi
fi
log WARN "chs-dev (version ${chs_dev_version_installed}) already installed"
else
log WARN "chs-dev already installed"
fi
if user_confirm_action "Do you want to ${upgrade_downgrade} to ${VERSION}?"; then
FORCE=1 uninstall
else
panic "chs-dev cli not installed"
fi
fi
if [ -n "${THIS_DIR}" ]; then
if ! make clean package; then
panic 'Could not build local package'
fi
fi
# Download the most recent releases and validate the version
if [ -z "${THIS_DIR}" ]; then
if ! download_releases; then
panic "Cannot download latest releases."
fi
if [ ! "${VERSION}" = "${DEFAULT_VERSION}" ]; then
if ! validate_version; then
panic 'Could not install chs-dev'
fi
fi
fi
# Set the required information in order to download tarball
determine_operating_system
determine_chipset
if [ -z "${THIS_DIR}" ]; then
# captured in variable so no way of doing ! cmd
# shellcheck disable=SC2181
if ! determine_download_url; then
panic "Could not find download for release specified."
fi
# Download tarball
DOWNLOAD_URL="$(cat "${temp_d}"/download_url)"
log INFO 'Downloading CLI tarball. Will take a few moments...'
if ! download_cli_tarball; then
panic "Could not download release tarball."
fi
else
log 'DEBUG' 'Looking for tar to install in built artefacts'
for tar_ball in dist/*.tar.gz; do
log 'DEBUG' "tar_ball=${tar_ball}"
if printf -- '%s' "${tar_ball}" | grep -Eq "${OS}-${CHIPSET}\.tar\.gz$"; then
log 'DEBUG' "using tar_ball=${tar_ball}"
downloaded_cli_tar_file="${tar_ball}"
break
fi
done
if [ -z "${downloaded_cli_tar_file}" ]; then
panic "CLI not built as expected"
fi
fi
# Install tarball
if install_cli_tarball; then
log INFO 'chs-dev CLI installed successfully.'
else
panic "CLI not installed"
fi
}
# When called will attempt to revert any possible uninstall by moving the files
# from the backed up location to their installed location.
revert_uninstall() {
if [ -d "${backup_cli}" ]; then
log "DEBUG" 'Restoring installation'
mv "${backup_cli}" "${INSTALLATION_DIRECTORY}"
fi
if [ -f "${backup_symlink}" ]; then
log "DEBUG" 'Restoring symlink'
mv "${backup_symlink}" "${SYMLINK_DIRECTORY}"/"${SYMLINK_NAME}"
fi
}
# Uninstalls the installed CLI - does this by moving the files to the temporary
# directory and so are removed after script is successful
uninstall() {
log INFO "Uninstalling CLI tool"
# Get user to confirm they are happy to uninstall chs-dev CLI
if ! user_confirm_action "This will uninstall chs-dev CLI do you want to continue?"; then
panic "chs-dev cli not uninstalled"
fi
# Remove symlink and the chs-dev directory
log DEBUG "Removing symlink"
mv "${SYMLINK_DIRECTORY}"/"${SYMLINK_NAME}" "${backup_symlink}"
log DEBUG "Removing CLI files"
mv "${INSTALLATION_DIRECTORY}" "${backup_cli}"
log INFO "chs-dev CLI uninstalled successfully"
}
if ! command -v jq >/dev/null 2>&1; then
panic 'Required utility: jq is not installed'
fi
# Setup default values
DEFAULT_LOGGING_LEVEL=INFO
DEFAULT_VERSION=latest
DEFAULT_INSTALLATION_DIRECTORY="${HOME}"/.chs-dev
DEFAULT_SYMLINK_DIRECTORY="${HOME}"/.companies_house_config/bin
DEFAULT_SYMLINK_NAME=chs-dev
DEFAULT_COMMAND=install
# Parse options
while getopts 'd:l:n:s:v:BSfhW' opt; do
case "${opt}" in
d) INSTALLATION_DIRECTORY="${OPTARG}" ;;
l) LOGGING_LEVEL="${OPTARG}" ;;
n) SYMLINK_NAME="${OPTARG}" ;;
s) SYMLINK_DIRECTORY="${OPTARG}" ;;
v) VERSION="${OPTARG}" ;;
B) THIS_DIR=1 ;;
S) NO_SYMLINK=1 ;;
f) FORCE=1 ;;
W) SUPPRESS_NOT_ON_PATH_WARN=1 ;;
h)
usage
exit 0
;;
*) ;;
esac
done
# Determine and validate the command
shift "$((OPTIND - 1))"
COMMAND="${1-"${DEFAULT_COMMAND}"}"
case "${COMMAND}" in
install | uninstall) ;;
*) panic "Command can only be install or uninstall" ;;
esac
# Set the values of the variables to the defaults if unset
: "${LOGGING_LEVEL:="${DEFAULT_LOGGING_LEVEL}"}"
: "${VERSION:="${DEFAULT_VERSION}"}"
: "${INSTALLATION_DIRECTORY:="${DEFAULT_INSTALLATION_DIRECTORY}"}"
: "${SYMLINK_DIRECTORY:="${DEFAULT_SYMLINK_DIRECTORY}"}"
: "${SYMLINK_NAME:="${DEFAULT_SYMLINK_NAME}"}"
"${COMMAND}"