-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-lastest-release.sh
executable file
·77 lines (67 loc) · 2.59 KB
/
install-lastest-release.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
#!/usr/bin/env bash
#
# Install the latest relase, or informed version, of the 'path-helper' binary.
# This may # require "sudo" to install on the target directory, set by environment
# variable "INSTALL_DIR". By default installs the latest release.
#
# Usage:
# $ hack/install-lastest-release.sh [version]
#
set -Eeu -o pipefail
declare -r APP="path-helper"
# The version to install, or 'latest' to install the latest release.
declare -r VERSION="${1:-latest}"
declare -r TMP_DIR="${TMP_DIR:-/tmp}"
# Installation directory, where the binary will be installed.
declare -r INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}"
# The repository owner and name, and the base domain path.
declare -r REPO_OWNER="${REPO_OWNER:-otaviof}"
declare -r REPO_NAME="${REPO_NAME:-${APP}}"
declare -r BASE_DOMAIN_PATH="github.com/repos/${REPO_OWNER}/${REPO_NAME}"
# Get the operating system and CPU type.
declare -r LOWER_OSTYPE=$(uname -s | tr '[:upper:]' '[:lower:]')
declare -r MACHINE_TYPE="$(uname -m)"
declare -r CPUTYPE="${MACHINE_TYPE/x86_64/amd64}"
# Search for the specific relase artifact URL, using the operating system and CPU
# type. Returns the full URL to the artifact, or empty if not found.
get_release_artifact_url() {
local _url="https://api.${BASE_DOMAIN_PATH}/releases"
local release_part="${LOWER_OSTYPE}_${CPUTYPE}.tar.gz"
if [[ "${VERSION}" == "latest" ]]; then
echo $(
curl -s ${_url}/latest |
jq -r '.assets[].browser_download_url' |
grep -i ${release_part} |
head -n 1
)
else
echo $(
curl -s ${_url} |
jq -r ".[] | select(.tag_name == \"${VERSION}\") | .assets[].browser_download_url" |
grep -i ${release_part} |
head -n 1
)
fi
}
# Installs the application from the artifact URL, if found.
install_release() {
echo "Installing '${APP}' (${VERSION}) for ${LOWER_OSTYPE} (${CPUTYPE})..."
local _artifact_url=$(get_release_artifact_url)
if [[ -z "${_artifact_url}" ]]; then
echo "Release not found for version '${VERSION}' " \
"on ${LOWER_OSTYPE} (${CPUTYPE})!"
exit 1
fi
local _tarball="${REPO_NAME}.tar.gz"
echo "Installing '${APP}' ${VERSION} version from '${_artifact_url}'..."
curl -sL ${_artifact_url} -o "${_tarball}" >/dev/null &&
tar -zxvpf "${_tarball}" ${APP} >/dev/null &&
install -m 0755 ${APP} ${INSTALL_DIR}/${APP} >/dev/null &&
echo "The '${APP}' ${VERSION} version is installed at '${INSTALL_DIR}'"
}
#
# Main
#
pushd ${TMP_DIR} >/dev/null &&
install_release
popd