Skip to content

Instantly share code, notes, and snippets.

@kebyn
Created September 20, 2019 02:31
Show Gist options
  • Save kebyn/68305419b0de5379bc559cf095ae6a96 to your computer and use it in GitHub Desktop.
Save kebyn/68305419b0de5379bc559cf095ae6a96 to your computer and use it in GitHub Desktop.
#!/bin/sh
set -o pipefail
set +o xtrace
red=$(tput setaf 1)
green=$(tput setaf 2)
reset=$(tput sgr0)
function os_type() {
OS=${OSTYPE//[0-9.-]*/}
case "$OS" in
darwin) machine=Mac ;;
linux) machine=Linux ;;
*)
echo "Operating System $OSTYPE not supported, supported types are darwin, linux"
exit 1
;;
esac
}
## Find OS type
os_type
function echo_fail() {
printf "\e[31m✘ ${1}"
printf "\033\e[0m\n"
}
function echo_pass() {
printf "\e[32m✔ ${1}"
printf "\033\e[0m\n"
}
function die {
echo "${red}$@${reset}"
exit 1
}
# retry ${retries} "ls"
function retry() {
local retries=$1
shift
local count=0
until "$@"; do
exit=$?
wait=$((2 ** $count))
count=$(($count + 1 ))
if [[ ${count} -lt ${retries} ]]; then
echo "Retry $count/$retries exited ${exit}, retrying in ${wait} seconds..."
sleep ${wait}
else
echo "Retry $count/$retries exited ${exit}, no more retries left."
exit 1
return 1
fi
done
return 0
}
# run_cmd "ls -l" "${verbose}"
function run_cmd() {
enable_debug=$2
echo "Run Command:\n"
echo "\t${green}$1${reset}\n"
if [[ ${enable_debug} = "true" ]]; then
eval $1
else
val=$(eval $1 2>&1)
fi
if [[ $? != 0 ]]; then
die "${red}Unable to execute ${1} ${val} ${reset}\n"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment