2
0
Fork 0
mirror of https://github.com/alexanderepstein/Bash-Snippets synced 2018-11-08 02:59:35 +01:00

Adding httpGet support and getting rid of redundant code

This commit is contained in:
Alex Epstein 2017-06-29 20:31:37 -04:00
parent 6b7a9a254f
commit b7a5e6e0dd

View file

@ -5,12 +5,47 @@ base=""
exchangeTo="" exchangeTo=""
currentVersion="1.2.1" currentVersion="1.2.1"
checkCurl() configuredClient=""
## This function determines which http get tool the system has installed and returns an error if there isnt one
getConfiguredClient()
{ {
if ! command -v curl &>/dev/null ; then if command -v curl &>/dev/null ; then
echo "Error: this tool requires 'curl', please install it." configuredClient="curl"
return 1 elif command -v wget &>/dev/null ; then
fi configuredClient="wget"
elif command -v fetch &>/dev/null ; then
configuredClient="fetch"
else
echo "Error: This tool reqires either curl, wget, or fetch to be installed."
return 1
fi
}
## Allows to call the users configured client without if statements everywhere
httpGet()
{
case "$configuredClient" in
curl) curl -A curl -s "$@";;
wget) wget -qO- "$@";;
fetch) fetch -o "...";;
esac
}
checkValidCurrency()
{
if [[ $1 != "AUD" && $1 != "BGN" && $1 != "BRL" \
&& $1 != "CAD" && $1 != "CHF" && $1 != "CNY" && $1 != "CZK" && $1 != "DKK" \
&& $1 != "EUR" && $1 != "GBP" && $1 != "HKD" && $1 != "HRK" && $1 != "HUF" \
&& $1 != "IDR" && $1 != "ILS" && $1 != "INR" && $1 != "JPY" && $1 != "KRW" \
&& $1 != "MXN" && $1 != "MYR" && $1 != "NOK" && $1 != "NZD" && $1 != "PHP" \
&& $1 != "PLN" && $1 != "RON" && $1 != "RUB" && $1 != "SEK" && $1 != "SGD" \
&& $1 != "THB" && $1 != "TRY" && $1 != "USD" && $1 != "ZAR" ]];then
echo "1"
else
echo "0"
fi
} }
## Grabs the base currency from the user and validates it with all the possible currency ## Grabs the base currency from the user and validates it with all the possible currency
@ -20,13 +55,7 @@ getBase()
echo -n "What is the base currency: " echo -n "What is the base currency: "
read -r base read -r base
base=$(echo $base | tr /a-z/ /A-Z/) base=$(echo $base | tr /a-z/ /A-Z/)
if [[ $base != "AUD" && $base != "BGN" && $base != "BRL" \ if [[ $(checkValidCurrency $base) == "1" ]];then
&& $base != "CAD" && $base != "CHF" && $base != "CNY" && $base != "CZK" && $base != "DKK" \
&& $base != "EUR" && $base != "GBP" && $base != "HKD" && $base != "HRK" && $base != "HUF" \
&& $base != "IDR" && $base != "ILS" && $base != "INR" && $base != "JPY" && $base != "KRW" \
&& $base != "MXN" && $base != "MYR" && $base != "NOK" && $base != "NZD" && $base != "PHP" \
&& $base != "PLN" && $base != "RON" && $base != "RUB" && $base != "SEK" && $base != "SGD" \
&& $base != "THB" && $base != "TRY" && $base != "USD" && $base != "ZAR" ]];then
unset base unset base
echo "Invalid base currency" echo "Invalid base currency"
getBase getBase
@ -38,13 +67,7 @@ checkBase()
{ {
base=$1 base=$1
base=$(echo $base | tr /a-z/ /A-Z/) base=$(echo $base | tr /a-z/ /A-Z/)
if [[ $base != "AUD" && $base != "BGN" && $base != "BRL" \ if [[ $(checkValidCurrency $base) == "1" ]];then
&& $base != "CAD" && $base != "CHF" && $base != "CNY" && $base != "CZK" && $base != "DKK" \
&& $base != "EUR" && $base != "GBP" && $base != "HKD" && $base != "HRK" && $base != "HUF" \
&& $base != "IDR" && $base != "ILS" && $base != "INR" && $base != "JPY" && $base != "KRW" \
&& $base != "MXN" && $base != "MYR" && $base != "NOK" && $base != "NZD" && $base != "PHP" \
&& $base != "PLN" && $base != "RON" && $base != "RUB" && $base != "SEK" && $base != "SGD" \
&& $base != "THB" && $base != "TRY" && $base != "USD" && $base != "ZAR" ]];then
unset base unset base
echo "Invalid base currency" echo "Invalid base currency"
exit 1 exit 1
@ -58,13 +81,7 @@ getExchangeTo()
echo -n "What currency to exchange to: " echo -n "What currency to exchange to: "
read -r exchangeTo read -r exchangeTo
exchangeTo=$(echo $exchangeTo | tr /a-z/ /A-Z/) exchangeTo=$(echo $exchangeTo | tr /a-z/ /A-Z/)
if [[ $exchangeTo != "AUD" && $exchangeTo != "BGN" && $exchangeTo != "BRL" \ if [[ $(checkValidCurrency $exchangeTo) == "1" ]];then
&& $exchangeTo != "CAD" && $exchangeTo != "CHF" && $exchangeTo != "CNY" && $exchangeTo != "CZK" && $exchangeTo != "DKK" \
&& $exchangeTo != "EUR" && $exchangeTo != "GBP" && $exchangeTo != "HKD" && $exchangeTo != "HRK" && $exchangeTo != "HUF" \
&& $exchangeTo != "IDR" && $exchangeTo != "ILS" && $exchangeTo != "INR" && $exchangeTo != "JPY" && $exchangeTo != "KRW" \
&& $exchangeTo != "MXN" && $exchangeTo != "MYR" && $exchangeTo != "NOK" && $exchangeTo != "NZD" && $exchangeTo != "PHP" \
&& $exchangeTo != "PLN" && $exchangeTo != "RON" && $exchangeTo != "RUB" && $exchangeTo != "SEK" && $exchangeTo != "SGD" \
&& $exchangeTo != "THB" && $exchangeTo != "TRY" && $exchangeTo != "USD" && $exchangeTo != "ZAR" ]];then
echo "Invalid exchange currency" echo "Invalid exchange currency"
unset getExchangeTo unset getExchangeTo
getExchangeTo getExchangeTo
@ -77,13 +94,7 @@ checkExchangeTo()
{ {
exchangeTo=$1 exchangeTo=$1
exchangeTo=$(echo $exchangeTo | tr /a-z/ /A-Z/) exchangeTo=$(echo $exchangeTo | tr /a-z/ /A-Z/)
if [[ $exchangeTo != "AUD" && $exchangeTo != "BGN" && $exchangeTo != "BRL" \ if [[ $(checkValidCurrency $exchangeTo) == "1" ]];then
&& $exchangeTo != "CAD" && $exchangeTo != "CHF" && $exchangeTo != "CNY" && $exchangeTo != "CZK" && $exchangeTo != "DKK" \
&& $exchangeTo != "EUR" && $exchangeTo != "GBP" && $exchangeTo != "HKD" && $exchangeTo != "HRK" && $exchangeTo != "HUF" \
&& $exchangeTo != "IDR" && $exchangeTo != "ILS" && $exchangeTo != "INR" && $exchangeTo != "JPY" && $exchangeTo != "KRW" \
&& $exchangeTo != "MXN" && $exchangeTo != "MYR" && $exchangeTo != "NOK" && $exchangeTo != "NZD" && $exchangeTo != "PHP" \
&& $exchangeTo != "PLN" && $exchangeTo != "RON" && $exchangeTo != "RUB" && $exchangeTo != "SEK" && $exchangeTo != "SGD" \
&& $exchangeTo != "THB" && $exchangeTo != "TRY" && $exchangeTo != "USD" && $exchangeTo != "ZAR" ]];then
echo "Invalid exchange currency" echo "Invalid exchange currency"
unset getExchangeTo unset getExchangeTo
exit 1 exit 1
@ -131,7 +142,7 @@ checkInternet()
## Grabs the exchange rate and does the math for converting the currency ## Grabs the exchange rate and does the math for converting the currency
convertCurrency() convertCurrency()
{ {
exchangeRate=$(curl -Acurl -s "http://api.fixer.io/latest?base=$base&symbols=$exchangeTo" | grep -Eo "[0-9]*[.][0-9]*") > /dev/null exchangeRate=$(httpGet "http://api.fixer.io/latest?base=$base&symbols=$exchangeTo" | grep -Eo "[0-9]*[.][0-9]*") > /dev/null
exchangeAmount=$(echo "$exchangeRate * $amount" | bc ) exchangeAmount=$(echo "$exchangeRate * $amount" | bc )
echo echo
echo "=========================" echo "========================="
@ -150,7 +161,7 @@ update()
repositoryName="Bash-Snippets" #Name of repostiory to be updated ex. Sandman-Lite repositoryName="Bash-Snippets" #Name of repostiory to be updated ex. Sandman-Lite
githubUserName="alexanderepstein" #username that hosts the repostiory ex. alexanderepstein githubUserName="alexanderepstein" #username that hosts the repostiory ex. alexanderepstein
nameOfInstallFile="install.sh" # change this if the installer file has a different name be sure to include file extension if there is one nameOfInstallFile="install.sh" # change this if the installer file has a different name be sure to include file extension if there is one
latestVersion=$(curl -Acurl -s https://api.github.com/repos/$githubUserName/$repositoryName/tags | grep -Eo '"name":.*?[^\\]",'| head -1 | grep -Eo "[0-9.]+" ) #always grabs the tag without the v option latestVersion=$(httpGet https://api.github.com/repos/$githubUserName/$repositoryName/tags | grep -Eo '"name":.*?[^\\]",'| head -1 | grep -Eo "[0-9.]+" ) #always grabs the tag without the v option
if [[ $currentVersion == "" || $repositoryName == "" || $githubUserName == "" || $nameOfInstallFile == "" ]];then if [[ $currentVersion == "" || $repositoryName == "" || $githubUserName == "" || $nameOfInstallFile == "" ]];then
echo "Error: update utility has not been configured correctly." >&2 echo "Error: update utility has not been configured correctly." >&2
@ -193,7 +204,7 @@ usage()
echo " -v Get the tool version" echo " -v Get the tool version"
} }
checkCurl || exit 1 getConfiguredClient || exit 1
checkInternet || exit 1 # check if we have a valid internet connection if this isnt true the rest of the script will not work so stop here checkInternet || exit 1 # check if we have a valid internet connection if this isnt true the rest of the script will not work so stop here
while getopts "uvh" opt; do while getopts "uvh" opt; do
@ -234,9 +245,14 @@ elif [[ $# == "1" ]]; then
usage usage
else else
echo "Not a valid argument" echo "Not a valid argument"
usage
exit 1 exit 1
fi fi
elif [[ $# == 3 ]];then elif [[ $# == "2" ]]; then
echo "Not a valid argument"
usage
exit 1
elif [[ $# == "3" ]];then
checkBase $1 checkBase $1
checkExchangeTo $2 checkExchangeTo $2
checkAmount $3 checkAmount $3