-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
192 lines (161 loc) · 4.54 KB
/
install.sh
File metadata and controls
192 lines (161 loc) · 4.54 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
#!/usr/bin/env bash
##
# Big Bite package installer script.
#
# Prerequisites:
# - GitHub CLI
##
# Set some colors.
RED=$(tput setaf 1);
GREEN=$(tput setaf 2);
YELLOW=$(tput setaf 3);
BLUE=$(tput setaf 4);
BOLD=$(tput bold);
RESET=$(tput sgr0);
NEWLINE=$'\n'
##
# Preflight checks.
##
# Set the platform.
PLATFORM=$(/usr/bin/uname -m)
# Set the installation path.
INSTALL_PATH=/usr/local/bin/
# Set the GitHub organization.
ORG=bigbite
# Make sure Mac OS.
if [[ ${OSTYPE} != 'darwin'* ]]
then
echo "${NEWLINE}${BOLD}${YELLOW}MacOS only supported at this time.${RESET}"
exit 1
fi
# Make sure arm64 or x86_64 as those are the binaries created.
if [[ ${PLATFORM} != "arm64" ]] && [[ ${PLATFORM} != "x86_64" ]]
then
echo "${NEWLINE}${BOLD}${YELLOW}Only arm64 and x86_64 architecture supported at this time.${RESET}"
exit 1
fi
# Make sure the GitHub CLI is installed.
if ! [ -x "$(command -v gh)" ]
then
echo "${NEWLINE}Please install the GitHub CLI to run this script: https://cli.github.com/"
exit 1
fi
# Check for flags.
while getopts r:p:v: flag
do
case "${flag}" in
r) REPO=${OPTARG};;
p) PACKAGE_NAME=${OPTARG};;
v) VERSION=${OPTARG};;
*) echo "usage: $0 -r [-p] [-v]" >&2
exit 1 ;;
esac
done
# Make sure the repo is set.
if [ -z ${REPO+x} ]
then
echo "${NEWLINE}${BOLD}${YELLOW}Argument required for the repository: -r${RESET}"
exit 1;
fi
# Set the package name to the either the repo name or the specified package name incase it differs.
PACKAGE_NAME=${PACKAGE_NAME:-$REPO}
# Set the GitHub auth token from the GH_TOKEN environmnet variable, or try via GitHub CLI if not set.
GH_TOKEN=${GH_TOKEN:-$(gh auth token 2>/dev/null)}
# Exit if not token found.
if [ -z "$GH_TOKEN" ]
then
echo "${NEWLINE}${BOLD}${RED}No GitHub token found${RESET}"
echo "To get started with GitHub CLI, please run: gh auth login"
echo "Alternatively, populate the GH_TOKEN environment variable with a GitHub API authentication token."
exit 1;
fi
##
# Installation.
##
# Handle if command failed.
cmd_fail_and_exit () {
if [ -z "$1" ]
then
echo "${BOLD}${RED}>>>${RESET} Failed"
else
echo "${BOLD}${RED}>>>${RESET} Failed: ${1}"
fi
echo "${NEWLINE}${RED}${BOLD}Failed to install ${PACKAGE_NAME}.${RESET}"
exit 1
}
# Handle if command was successfull.
cmd_success() {
if [ -z "$1" ]
then
echo "${BOLD}${GREEN}>>>${RESET} Done"
else
echo "${BOLD}${GREEN}>>>${RESET} Done: ${1}"
fi
}
# Check if version entered, if not find latest version.
if [ -z ${VERSION+x} ]
then
echo "${NEWLINE}${BOLD}${BLUE}Checking latest release tag ... ${VERSION}"
VERSION=$(curl --silent --fail -w '%{http_code}' -H "Authorization: token ${GH_TOKEN}" "https://api.github.com/repos/${ORG}/${REPO}/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
RETURN_CODE=$?
if [ ${RETURN_CODE} -ne 0 ] || [ -z "${VERSION}" ] || [ "${VERSION}" = "null" ]
then
cmd_fail_and_exit
else
cmd_success "$VERSION"
fi
fi
# Set binary filenanme to match output of vercel/pkg.
if [[ ${PLATFORM} == "x86_64" ]]
then
BINARY="${PACKAGE_NAME}-${VERSION}-macos-x64"
elif [[ ${PLATFORM} == "arm64" ]]
then
BINARY="${PACKAGE_NAME}-${VERSION}-macos-arm64"
fi
# Display binary that is being downloaded.
echo "${NEWLINE}${BOLD}${BLUE}Downloading ${BINARY} ...${RESET}"
# Download using the GitHub CLI.
gh release download --clobber -R "${ORG}/${REPO}" "${VERSION}" -p "${BINARY}"
# Check if return code is not 0 incase of failure.
RETURN_CODE=$?
if [ ${RETURN_CODE} -ne 0 ]
then
cmd_fail_and_exit "$ERROR"
else
cmd_success
fi
# Make sure the binary is executable.
chmod +x "${BINARY}"
# Check if install path exists, create it if not.
if [ ! -d "${INSTALL_PATH}" ]
then
echo "${NEWLINE}${BLUE}${BOLD}Creating ${INSTALL_PATH} ...${RESET}"
mkdir -p "${INSTALL_PATH}" || sudo mkdir -p "${INSTALL_PATH}"
# If failed to create install path, display error message and exit.
RETURN_CODE=$?
if [ ${RETURN_CODE} -ne 0 ]
then
cmd_fail_and_exit
else
cmd_success
fi
fi
# Display where the downloaded binary is being installed to.
echo "${NEWLINE}${BLUE}${BOLD}Installing to ${INSTALL_PATH} ...${RESET}"
# Move the binary to the install path.
{
mv -f "${BINARY}" "${INSTALL_PATH}${PACKAGE_NAME}" 2>/dev/null
} || {
sudo mv -f "${BINARY}" "${INSTALL_PATH}${PACKAGE_NAME}" 2>/dev/null
}
# If failed to install, display error message and exit.
RETURN_CODE=$?
if [ ${RETURN_CODE} -ne 0 ]
then
cmd_fail_and_exit
else
cmd_success
fi
# All done.
echo "${NEWLINE}${GREEN}${BOLD}Successfully installed ${PACKAGE_NAME} ${VERSION}!${RESET}"