Skip to content

Commit

Permalink
Enhance get.sh script to work with curl command
Browse files Browse the repository at this point in the history
  • Loading branch information
mkumatag committed Dec 17, 2020
1 parent af25ed7 commit fe74948
Showing 1 changed file with 42 additions and 17 deletions.
59 changes: 42 additions & 17 deletions get.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,32 @@
configuration for the pvsadm tool.
'

: '
Usage ./get.sh
Examples:
# Download the latest released version of pvsadm tool
./get.sh
# Download the 0.1 release
VERSION=0.1 ./get.sh
# Run script via curl + bash
curl -sL https://raw.githubusercontent.com/ppc64le-cloud/pvsadm/master/get.sh | bash
# Run script via curl + bash, replace if any existing version exist in the /usr/local/bin path
curl -sL https://raw.githubusercontent.com/ppc64le-cloud/pvsadm/master/get.sh | FORCE=1 bash
'

# Trap ctrl-c and call ctrl_c()
trap ctrl_c INT

function ctrl_c() {
echo "Bye!"
}

VERSION=${VERSION:=latest}
FORCE=${FORCE:=0}

function identify_os() {

local OS="$(uname -s)"
Expand All @@ -33,53 +52,59 @@ function identify_os() {
ARCH=amd64
fi

export $ARCH
export $DISTRO
export ARCH
export DISTRO
}

function check_connectivity() {

curl --output /dev/null --silent --head --fail http://github.com
if [ ! $? -eq 0 ]; then
if ! curl --output /dev/null --silent --head --fail http://github.com; then
echo
echo "ERROR: please, check your internet connection."
exit
echo "ERROR: unable to reach github.com, please check your internet connection."
exit 1
fi
}

function install_pvsadmin() {
function install_pvsadm() {

if [[ "$1" == *"--force"* ]]; then
if [[ "${FORCE}" -eq 1 ]]; then
if command -v "pvsadm" &> /dev/null; then
rm -f /usr/local/bin/pvsadm
fi
fi

LATEST_RELEASE=$(curl --silent "https://api.github.com/repos/ppc64le-cloud/pvsadm/releases/latest" | jq -r '.tag_name')

if command -v "pvsadm" &> /dev/null; then
echo "pvsadm is already installed!"
pvsadm version
exit
pvsadm version
exit 1
fi

curl --progress-bar -LJ "https://github.com/ppc64le-cloud/pvsadm/releases/download/$LATEST_RELEASE/pvsadm-$DISTRO-$ARCH" --output /usr/local/bin/pvsadm
if [[ "${VERSION}" == "latest" ]]; then
DL_URL="https://github.com/ppc64le-cloud/pvsadm/releases/latest/download"
else
DL_URL="https://github.com/ppc64le-cloud/pvsadm/releases/download/${VERSION}"
fi

if ! curl --fail --progress-bar -LJ "${DL_URL}/pvsadm-$DISTRO-$ARCH" --output /usr/local/bin/pvsadm; then
echo "Failed to download the pvsadm with mentioned ${VERSION} version, please check the VERSION"
exit 1
fi

chmod +x /usr/local/bin/pvsadm
pvsadm version
}

function run (){

if [ -z $1 ]; then
if [[ "${FORCE}" -ne 1 ]]; then
echo
echo "To replace an old version of pvsadm, run this script as ./get.sh --force"
echo "To replace an old version of pvsadm, run this script with an environment variable: FORCE=1"
echo
fi

identify_os
check_connectivity
install_pvsadmin $1
install_pvsadm
}

run "$@"
run

0 comments on commit fe74948

Please sign in to comment.