-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
149 lines (126 loc) · 4.53 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
#!/usr/bin/env sh
# This install script is intended to download and install the latest available
# release of a platform specific binary. It will attempt to download the version of the
# binary required by your platform. It is designed for Golang projects but can be adapted for
# other languages too.
#
# Environment variables:
# - INSTALL_DIRECTORY (optional): defaults to current directory
# - OVERRIDE_RELEASE_TAG (optional): defaults to fetching the latest release
# - OVERRIDE_OS (optional): use a specific value for OS (mostly for testing)
# - OVERRIDE_ARCH (optional): use a specific value for ARCH (mostly for testing)
#
# You can install using this script:
# $ curl https://raw.githubusercontent.com/<project>/master/install.sh | sh
#
# To adapt this to your own project, replace <project> with your user and project name.
# ensure we exit on failure
set -e
# ensure pipelines fail
set -o pipefail
# URLS
PROJECT_URL="https://github.com/astromechza/score-flyio"
RELEASES_URL="${PROJECT_URL}/releases"
BINARY_NAME="score-flyio"
# destination dir can be overriden
INSTALL_DIRECTORY=${INSTALL_DIRECTORY:-.}
downloadJSON() {
url="$2"
echo "Fetching $url.."
if test -x "$(command -v curl)"; then
response=$(curl -s -L -w 'HTTPSTATUS:%{http_code}' -H 'Accept: application/json' "$url")
body=$(echo "$response" | sed -e 's/HTTPSTATUS\:.*//g')
code=$(echo "$response" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
elif test -x "$(command -v wget)"; then
temp=$(mktemp)
body=$(wget -q --header='Accept: application/json' -O - --server-response --content-on-error "$url" 2> "$temp")
code=$(awk '/^ HTTP/{print $2}' < "$temp" | tail -1)
rm "$temp"
else
echo "Neither curl nor wget was available to perform http requests."
exit 1
fi
if [ "$code" != 200 ]; then
echo "Request failed with code $code"
exit 1
fi
eval "$1='$body'"
}
downloadFile() {
url="$1"
destination="$2"
echo "Fetching $url.."
if test -x "$(command -v curl)"; then
code=$(curl -s -w '%{http_code}' -L "$url" -o "$destination")
elif test -x "$(command -v wget)"; then
code=$(wget -q -O "$destination" --server-response "$url" 2>&1 | awk '/^ HTTP/{print $2} | tail -1')
else
echo "Neither curl nor wget was available to perform http requests."
exit 1
fi
if [ "$code" != 200 ]; then
echo "Request failed with code $code"
exit 1
fi
}
initArch() {
ARCH=$(uname -m)
if [ -n "$OVERRIDE_ARCH" ]; then
echo "Using OVERRIDE_ARCH"
ARCH="$OVERRIDE_ARCH"
fi
case $ARCH in
amd64) ARCH="amd64";;
arm64) ARCH="arm64";;
x86_64) ARCH="amd64";;
i386) ARCH="386";;
*) echo "Architecture ${ARCH} is not supported by this installation script"; exit 1;;
esac
echo "ARCH = $ARCH"
}
initOS() {
OS=$(uname | tr '[:upper:]' '[:lower:]')
if [ -n "$OVERRIDE_OS" ]; then
echo "Using OVERRIDE_OS"
OS="$OVERRIDE_OS"
fi
case "$OS" in
darwin) OS='darwin';;
linux) OS='linux';;
freebsd) OS='freebsd';;
mingw*) OS='windows';;
msys*) OS='windows';;
windows) OS='windows';;
*) echo "OS ${OS} is not supported by this installation script"; exit 1;;
esac
echo "OS = $OS"
}
# identify platform based on uname output
initArch
initOS
# if OVERRIDE_RELEASE_TAG was not provided, assume latest
if [ -z "$OVERRIDE_RELEASE_TAG" ]; then
downloadJSON LATEST_RELEASE "$RELEASES_URL/latest"
OVERRIDE_RELEASE_TAG=$(echo "${LATEST_RELEASE}" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//' )
fi
echo "Release Tag = $OVERRIDE_RELEASE_TAG"
# fetch the real release data to make sure it exists before we attempt a download
downloadJSON RELEASE_DATA "$RELEASES_URL/tag/$OVERRIDE_RELEASE_TAG"
# assemble expected release artifact name
# you will also need to modify this pattern when adapting it to your project
DOWNLOAD_BINARY="${BINARY_NAME}-${OVERRIDE_RELEASE_TAG}-${OS}-${ARCH}"
# add .exe if on windows
if [ "${OS}" = "windows" ]; then
DOWNLOAD_BINARY="$DOWNLOAD_BINARY.exe"
fi
BINARY_URL="$RELEASES_URL/download/$OVERRIDE_RELEASE_TAG/$DOWNLOAD_BINARY"
DOWNLOAD_FILE=$(mktemp)
downloadFile "$BINARY_URL" "$DOWNLOAD_FILE"
echo "Setting executable permissions."
chmod +x "$DOWNLOAD_FILE"
# add .exe if on windows
if [ "${OS}" = "windows" ]; then
BINARY_NAME="$BINARY_NAME.exe"
fi
echo "Moving executable to $INSTALL_DIRECTORY/$BINARY_NAME"
mv "$DOWNLOAD_FILE" "$INSTALL_DIRECTORY/$BINARY_NAME"