-
-
Notifications
You must be signed in to change notification settings - Fork 540
/
_common.sh
executable file
·86 lines (76 loc) · 2.62 KB
/
_common.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
#!/usr/bin/env bash
set -eo pipefail
# Tool name, based on filename.
# Tool filename MUST BE same as in package manager/binary name
TOOL=${0##*/}
readonly TOOL=${TOOL%%.*}
# Get "TOOL_VERSION"
# shellcheck disable=SC1091 # Created in Dockerfile before execution of this script
source /.env
env_var_name="${TOOL//-/_}"
env_var_name="${env_var_name^^}_VERSION"
# shellcheck disable=SC2034 # Used in other scripts
readonly VERSION="${!env_var_name}"
# Skip tool installation if the version is set to "false"
if [[ $VERSION == false ]]; then
echo "'$TOOL' skipped"
exit 0
fi
#######################################################################
# Install the latest or specific version of the tool from GitHub release
# Globals:
# TOOL - Name of the tool
# VERSION - Version of the tool
# Arguments:
# GH_ORG - GitHub organization name where the tool is hosted
# DISTRIBUTED_AS - How the tool is distributed.
# Can be: 'tar.gz', 'zip' or 'binary'
# GH_RELEASE_REGEX_LATEST - Regular expression to match the latest
# release URL
# GH_RELEASE_REGEX_SPECIFIC_VERSION - Regular expression to match the
# specific version release URL
# UNUSUAL_TOOL_NAME_IN_PKG - If the tool in the tar.gz package is
# not in the root or named differently than the tool name itself,
# For example, includes the version number or is in a subdirectory
#######################################################################
function common::install_from_gh_release {
local -r GH_ORG=$1
local -r DISTRIBUTED_AS=$2
local -r GH_RELEASE_REGEX_LATEST=$3
local -r GH_RELEASE_REGEX_SPECIFIC_VERSION=$4
local -r UNUSUAL_TOOL_NAME_IN_PKG=$5
case $DISTRIBUTED_AS in
tar.gz | zip)
local -r PKG="${TOOL}.${DISTRIBUTED_AS}"
;;
binary)
local -r PKG="$TOOL"
;;
*)
echo "Unknown DISTRIBUTED_AS: '$DISTRIBUTED_AS'. Should be one of: 'tar.gz', 'zip' or 'binary'." >&2
exit 1
;;
esac
# Download tool
local -r RELEASES="https://api.github.com/repos/${GH_ORG}/${TOOL}/releases"
if [[ $VERSION == latest ]]; then
curl -L "$(curl -s "${RELEASES}/latest" | grep -o -E -i -m 1 "$GH_RELEASE_REGEX_LATEST")" > "$PKG"
else
curl -L "$(curl -s "$RELEASES" | grep -o -E -i -m 1 "$GH_RELEASE_REGEX_SPECIFIC_VERSION")" > "$PKG"
fi
# Make tool ready to use
if [[ $DISTRIBUTED_AS == tar.gz ]]; then
if [[ -z $UNUSUAL_TOOL_NAME_IN_PKG ]]; then
tar -xzf "$PKG" "$TOOL"
else
tar -xzf "$PKG" "$UNUSUAL_TOOL_NAME_IN_PKG"
mv "$UNUSUAL_TOOL_NAME_IN_PKG" "$TOOL"
fi
rm "$PKG"
elif [[ $DISTRIBUTED_AS == zip ]]; then
unzip "$PKG"
rm "$PKG"
else
chmod +x "$PKG"
fi
}