Skip to content

Commit cbb392e

Browse files
author
Sébastien HOUZÉ
authored
Merge pull request #12 from inextensodigital/chore-add-install-script
chore: add install.sh script
2 parents 42a32cb + b499ec6 commit cbb392e

1 file changed

Lines changed: 217 additions & 0 deletions

File tree

install.sh

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
#!/usr/bin/env bash
2+
3+
PROJECT_NAME="github"
4+
5+
: ${USE_SUDO:="true"}
6+
: ${GH_CLI_INSTALL_DIR:="/usr/local/bin"}
7+
8+
# initArch discovers the architecture for this system.
9+
initArch() {
10+
ARCH=$(uname -m)
11+
case $ARCH in
12+
armv5*) ARCH="armv5";;
13+
armv6*) ARCH="armv6";;
14+
armv7*) ARCH="arm";;
15+
aarch64) ARCH="arm64";;
16+
x86) ARCH="386";;
17+
x86_64) ARCH="amd64";;
18+
i686) ARCH="386";;
19+
i386) ARCH="386";;
20+
esac
21+
}
22+
23+
# initOS discovers the operating system for this system.
24+
initOS() {
25+
OS=$(echo `uname`|tr '[:upper:]' '[:lower:]')
26+
27+
case "$OS" in
28+
# Minimalist GNU for Windows
29+
mingw*) OS='windows';;
30+
esac
31+
}
32+
33+
# runs the given command as root (detects if we are root already)
34+
runAsRoot() {
35+
local CMD="$*"
36+
37+
if [ $EUID -ne 0 -a $USE_SUDO = "true" ]; then
38+
CMD="sudo $CMD"
39+
fi
40+
41+
$CMD
42+
}
43+
44+
# verifySupported checks that the os/arch combination is supported for
45+
# binary builds.
46+
verifySupported() {
47+
local supported="darwin-amd64\nlinux-amd64\nwindows-amd64"
48+
if ! echo "${supported}" | grep -q "${OS}-${ARCH}"; then
49+
echo "No prebuilt binary for ${OS}-${ARCH}."
50+
echo "To build from source, go to https://github.com/inextensodigital/github"
51+
exit 1
52+
fi
53+
54+
if ! type "curl" > /dev/null ; then
55+
echo "curl is required"
56+
exit 1
57+
fi
58+
59+
if ! type "jq" > /dev/null ; then
60+
echo "jq is required"
61+
exit 1
62+
fi
63+
}
64+
65+
# checkDesiredVersion checks if the desired version is available.
66+
checkDesiredVersion() {
67+
if [ "x$DESIRED_VERSION" == "x" ]; then
68+
# Get tag from release URL
69+
local latest_release_url="https://github.com/inextensodigital/github/releases/latest"
70+
TAG=$(curl -Ls -o /dev/null -w %{url_effective} $latest_release_url | grep -oE "[^/]+$" )
71+
else
72+
TAG=$DESIRED_VERSION
73+
fi
74+
}
75+
76+
# checkGithubInstalledVersion checks which version of github is installed and
77+
# if it needs to be changed.
78+
checkGithubInstalledVersion() {
79+
if [[ -f "${GH_CLI_INSTALL_DIR}/${PROJECT_NAME}" ]]; then
80+
local version=$(github version | grep '^github version ' | cut -d' ' -f3)
81+
if [[ "$version" == "$TAG" ]]; then
82+
echo "github ${version} is already ${DESIRED_VERSION:-latest}"
83+
return 0
84+
else
85+
echo "github ${TAG} is available. Changing from version ${version}."
86+
return 1
87+
fi
88+
else
89+
return 1
90+
fi
91+
}
92+
93+
# downloadFile downloads the latest binary package and also the checksum
94+
# for that binary.
95+
downloadFile() {
96+
echo $TAG $OS $ARCH
97+
DOWNLOAD_URL=$(
98+
curl -s https://api.github.com/repos/inextensodigital/github/releases/tags/$TAG | \
99+
jq -r '.assets[] | select(.name | test("'$OS'-'$ARCH'$")) | .browser_download_url'
100+
)
101+
CHECKSUM_URL=$(
102+
curl -s https://api.github.com/repos/inextensodigital/github/releases/tags/$TAG | \
103+
jq -r '.assets[] | select(.name | test("'$OS'-'$ARCH'.sha256$")) | .browser_download_url'
104+
)
105+
CHECKSUM_URL="$DOWNLOAD_URL.sha256"
106+
GH_CLI_TMP_ROOT="$(mktemp -dt github-installer-XXXXXX)"
107+
GH_CLI_TMP_FILE="$GH_CLI_TMP_ROOT/$PROJECT_NAME"
108+
GH_CLI_SUM_FILE="$GH_CLI_TMP_ROOT/$PROJECT_NAME.sha256"
109+
echo "Downloading $DOWNLOAD_URL"
110+
curl -SsL "$CHECKSUM_URL" -o "$GH_CLI_SUM_FILE"
111+
curl -SsL "$DOWNLOAD_URL" -o "$GH_CLI_TMP_FILE"
112+
}
113+
114+
# installFile verifies the SHA256 for the file, then unpacks and
115+
# installs it.
116+
installFile() {
117+
GH_CLI_TMP="$GH_CLI_TMP_ROOT/$PROJECT_NAME"
118+
local sum=$(openssl sha1 -sha256 ${GH_CLI_TMP_FILE} | awk '{print $2}')
119+
local expected_sum=$(cat ${GH_CLI_SUM_FILE} | awk '{print $1}')
120+
if [ "$sum" != "$expected_sum" ]; then
121+
echo "SHA sum of ${GH_CLI_TMP_FILE} does not match. Aborting."
122+
exit 1
123+
fi
124+
125+
echo "Preparing to install $PROJECT_NAME into ${GH_CLI_INSTALL_DIR}"
126+
chmod +x "$GH_CLI_TMP"
127+
runAsRoot cp "$GH_CLI_TMP" "$GH_CLI_INSTALL_DIR"
128+
echo "$PROJECT_NAME installed into $GH_CLI_INSTALL_DIR/$PROJECT_NAME"
129+
}
130+
131+
# fail_trap is executed if an error occurs.
132+
fail_trap() {
133+
result=$?
134+
if [ "$result" != "0" ]; then
135+
if [[ -n "$INPUT_ARGUMENTS" ]]; then
136+
echo "Failed to install $PROJECT_NAME with the arguments provided: $INPUT_ARGUMENTS"
137+
help
138+
else
139+
echo "Failed to install $PROJECT_NAME"
140+
fi
141+
echo -e "\tFor support, go to https://github.com/inextensodigital/github."
142+
fi
143+
# cleanup
144+
exit $result
145+
}
146+
147+
# testVersion tests the installed client to make sure it is working.
148+
testVersion() {
149+
set +e
150+
GITHUB="$(which $PROJECT_NAME)"
151+
if [ "$?" = "1" ]; then
152+
echo "$PROJECT_NAME not found. Is $GH_CLI_INSTALL_DIR on your "'$PATH?'
153+
exit 1
154+
fi
155+
set -e
156+
}
157+
158+
# help provides possible cli installation arguments
159+
help () {
160+
echo "Accepted cli arguments are:"
161+
echo -e "\t[--help|-h ] ->> prints this help"
162+
echo -e "\t[--version|-v <desired_version>] . When not defined it defaults to latest"
163+
echo -e "\te.g. --version v2.4.0 or -v latest"
164+
echo -e "\t[--no-sudo] ->> install without sudo"
165+
}
166+
167+
# cleanup temporary files to avoid https://github.com/inextensodigital/github/issues/2977
168+
cleanup() {
169+
if [[ -d "${GH_CLI_TMP_ROOT:-}" ]]; then
170+
rm -rf "$GH_CLI_TMP_ROOT"
171+
fi
172+
}
173+
174+
# Execution
175+
176+
#Stop execution on any error
177+
trap "fail_trap" EXIT
178+
set -e
179+
180+
# Parsing input arguments (if any)
181+
export INPUT_ARGUMENTS="${@}"
182+
set -u
183+
while [[ $# -gt 0 ]]; do
184+
case $1 in
185+
'--version'|-v)
186+
shift
187+
if [[ $# -ne 0 ]]; then
188+
export DESIRED_VERSION="${1}"
189+
else
190+
echo -e "Please provide the desired version. e.g. --version v2.4.0 or -v latest"
191+
exit 0
192+
fi
193+
;;
194+
'--no-sudo')
195+
USE_SUDO="false"
196+
;;
197+
'--help'|-h)
198+
help
199+
exit 0
200+
;;
201+
*) exit 1
202+
;;
203+
esac
204+
shift
205+
done
206+
set +u
207+
208+
initArch
209+
initOS
210+
verifySupported
211+
checkDesiredVersion
212+
if ! checkGithubInstalledVersion; then
213+
downloadFile
214+
installFile
215+
fi
216+
testVersion
217+
cleanup

0 commit comments

Comments
 (0)