Skip to content

Commit 13594d4

Browse files
authored
feat: add an install script for easy install (#95)
1 parent 7c8f976 commit 13594d4

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

install.sh

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env bash
2+
3+
ARCH="$(uname -m)"
4+
OS="$(uname)"
5+
6+
# convert to standard arch names used in files
7+
if [ "x${ARCH}" = "xx86_64" ]; then
8+
ARCH="amd64"
9+
fi
10+
11+
# convert to standard os names used in files
12+
# TODO: support Windows
13+
if [ "x${OS}" = "xDarwin" ]; then
14+
OS="darwin"
15+
else
16+
OS="linux"
17+
fi
18+
19+
# either specify the version in an environment variable or get the latest from GitHub
20+
if [ "x${ADC_VERSION}" = "x" ]; then
21+
ADC_VERSION=$(curl -L -s https://github.com/api7/adc/releases/latest |
22+
grep "adc/releases/tag/" | head -1 | awk -F '"' '{print $4}' |
23+
awk -F '/' '{print $NF}')
24+
fi
25+
26+
if [ "x${ADC_VERSION}" = "x" ]; then
27+
printf "Unable to find the latest version of ADC. Please set the ADC_VERSION environment variable and try again. For example, export ADC_VERSION=0.5.0\n"
28+
exit 1
29+
fi
30+
31+
# if version has v in prefix, remove it
32+
ADC_VERSION=${ADC_VERSION#v}
33+
34+
FILENAME="adc_${ADC_VERSION}_${OS}_${ARCH}.tar.gz"
35+
36+
# example download URL format: https://github.com/api7/adc/releases/download/v0.5.0/adc_0.5.0_darwin_arm64.tar.gz
37+
URL="https://github.com/api7/adc/releases/download/v${ADC_VERSION}/${FILENAME}"
38+
39+
printf "Downloading ADC v${ADC_VERSION} for ${OS} ${ARCH}...\n\n"
40+
# printf "Download URL: %s\n" "$URL"
41+
42+
curl -L ${URL} -o ${PWD}/adc.tar.gz
43+
if [ $? -ne 0 ]; then
44+
echo "Error downloading ADC. Please check your internet connection and try again."
45+
exit 1
46+
fi
47+
48+
# temporary folder name to extract the downloaded file
49+
TEMP_FOLDER_NAME=$(tr -dc A-Za-z0-9 </dev/urandom 2>/dev/null | head -c 16)
50+
if [ -z "$TEMP_FOLDER_NAME" ]; then
51+
TEMP_FOLDER_NAME="TEMP_ADC_FOLDER"
52+
fi
53+
54+
mkdir $TEMP_FOLDER_NAME
55+
56+
printf "\nExtracting ADC to temporary folder %s...\n" "$TEMP_FOLDER_NAME"
57+
58+
tar -xzf "${PWD}/adc.tar.gz" -C "${PWD}/${TEMP_FOLDER_NAME}"
59+
if [ $? -ne 0 ]; then
60+
echo "Error extracting ADC. The downloaded file might be corrupted. Please try again and make sure that you are installing the correct version."
61+
exit 1
62+
fi
63+
64+
INSTALL_DIR=${ADC_DIR}
65+
if [ -z "$INSTALL_DIR" ]; then
66+
INSTALL_DIR="/usr/local/bin"
67+
fi
68+
69+
printf "Installing ADC in $INSTALL_DIR...\n"
70+
71+
WHOAMI=$(whoami)
72+
73+
# install adc binary, use sudo if user doesn't have permission to install in INSTALL_DIR
74+
if mv "${PWD}/$TEMP_FOLDER_NAME/adc" "$INSTALL_DIR/adc" >/dev/null 2>&1; then
75+
echo "ADC installed successfully!"
76+
else
77+
if sudo mv ${PWD}/$TEMP_FOLDER_NAME/adc $INSTALL_DIR/adc; then
78+
echo "ADC installed successfully with sudo permissions!"
79+
else
80+
echo "Unable to install ADC. Please check the permissions of the user $WHOAMI for the directory $INSTALL_DIR."
81+
exit 1
82+
fi
83+
fi
84+
85+
# clean up temporary files
86+
printf "Removing temporary files...\n"
87+
rm -rf adc.tar.gz ${PWD}/$TEMP_FOLDER_NAME/
88+
89+
printf "Done!\n"

0 commit comments

Comments
 (0)