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