From 2863308090296a43ae69c7ecc4f1ef176b756090 Mon Sep 17 00:00:00 2001 From: WaLLy3K Date: Thu, 11 May 2017 13:34:58 +1000 Subject: [PATCH] Minimise string duplication & other minor changes Instead of duplicating output strings, rewrite core/web/ftlOutput() into one neat versionOutput(). --- advanced/Scripts/version.sh | 123 +++++++++++++----------------------- 1 file changed, 44 insertions(+), 79 deletions(-) diff --git a/advanced/Scripts/version.sh b/advanced/Scripts/version.sh index 62106342..8ab95367 100755 --- a/advanced/Scripts/version.sh +++ b/advanced/Scripts/version.sh @@ -10,10 +10,16 @@ # Variables DEFAULT="-1" -PHGITDIR="/etc/.pihole/" +COREGITDIR="/etc/.pihole/" WEBGITDIR="/var/www/html/admin/" getLocalVersion() { + # FTL requires a different method + if [ "$1" == "FTL" ]; then + echo $(pihole-FTL version) + return 0 + fi + # Get the tagged version of the local repository local directory="${1}" local version @@ -33,6 +39,12 @@ getLocalVersion() { } getLocalHash() { + # FTL hash is not applicable + if [ "$1" == "FTL" ]; then + echo "N/A" + return 0 + fi + # Get the short hash of the local repository local directory="${1}" local hash @@ -66,88 +78,41 @@ getRemoteVersion(){ return 0 } -coreOutput() { - [ "$1" = "-c" -o "$1" = "--current" -o -z "$1" ] && current="$(getLocalVersion ${PHGITDIR})" - [ "$1" = "-l" -o "$1" = "--latest" -o -z "$1" ] && latest="$(getRemoteVersion pi-hole)" - [ "$1" = "-h" -o "$1" = "--hash" ] && hash="$(getLocalHash ${PHGITDIR})" - [ -n "$2" ] && error="true" - - if [ -n "$current" -a -n "$latest" ]; then - str="Pi-hole version is $current (Latest: $latest)" - elif [ -n "$current" -a -z "$latest" ]; then - str="Current Pi-hole version is $current" - elif [ -z "$current" -a -n "$latest" ]; then - str="Latest Pi-hole version is $latest" - elif [ -n "$hash" ]; then - str="Current Pi-hole hash is $hash" - else - error="true" - fi - - if [ "$error" = "true" ]; then - echo " Invalid Option! Try 'pihole -v --help' for more information." - exit 1 - fi - - echo " $str" -} - -webOutput() { - [ "$1" = "-c" -o "$1" = "--current" -o -z "$1" ] && current="$(getLocalVersion ${WEBGITDIR})" - [ "$1" = "-l" -o "$1" = "--latest" -o -z "$1" ] && latest="$(getRemoteVersion AdminLTE)" - [ "$1" = "-h" -o "$1" = "--hash" ] && hash="$(getLocalHash ${WEBGITDIR})" - [ ! -d "${WEBGITDIR}" ] && str="Web interface not installed!" - [ -n "$2" ] && error="true" - +versionOutput() { + [ "$1" == "pi-hole" ] && GITDIR=${COREGITDIR} + [ "$1" == "AdminLTE" ] && GITDIR=${WEBGITDIR} + [ "$1" == "FTL" ] && GITDIR="FTL" + [ "$2" == "-c" -o "$2" == "--current" -o -z "$2" ] && current=$(getLocalVersion $GITDIR) + [ "$2" == "-l" -o "$2" == "--latest" -o -z "$2" ] && latest=$(getRemoteVersion $1) + [ "$2" == "-h" -o "$2" == "--hash" ] && hash=$(getLocalHash $GITDIR) + if [ -n "$current" -a -n "$latest" ]; then - str="Admin Console version is $current (Latest: $latest)" + output="${1^} version is $current (Latest: $latest)" elif [ -n "$current" -a -z "$latest" ]; then - str="Current Admin Console version is $current" + output="Current ${1^} version is $current" elif [ -z "$current" -a -n "$latest" ]; then - str="Latest Admin Console version is $latest" + output="Latest ${1^} version is $latest" + elif [ "$hash" == "N/A" ]; then + output="" elif [ -n "$hash" ]; then - str="Current Admin Console hash is $hash" + output="Current ${1^} hash is $hash" else - error="true" + errorOutput fi - if [ "$error" = "true" ]; then - echo " Invalid Option! Try 'pihole -v --help' for more information." - exit 1 - fi - - echo " $str" + [ -n "$output" ] && echo " $output" } -ftlOutput() { - [ "$1" = "-c" -o "$1" = "--current" -o -z "$1" ] && current="$(pihole-FTL version)" - [ "$1" = "-l" -o "$1" = "--latest" -o -z "$1" ] && latest="$(getRemoteVersion FTL)" - [ ! -d "${WEBGITDIR}" ] && exit 0 - [ -n "$2" ] && error="true" - - if [ -n "$current" -a -n "$latest" ]; then - str="FTL version is $current (Latest: $latest)" - elif [ -n "$current" -a -z "$latest" ]; then - str="Current FTL version is $current" - elif [ -z "$current" -a -n "$latest" ]; then - str="Latest FTL version is $latest" - else - error="true" - fi - - if [ "$error" = "true" ]; then - echo " Invalid Option! Try 'pihole -v --help' for more information." - exit 1 - fi - - echo " $str" +errorOutput() { + echo " Invalid Option! Try 'pihole -v --help' for more information." + exit 1 } defaultOutput() { - coreOutput "$1" "$2" - webOutput "$1" "$2" - ftlOutput "$1" "$2" + versionOutput "pi-hole" "$@" + versionOutput "AdminLTE" "$@" + versionOutput "FTL" "$@" } helpFunc() { @@ -155,23 +120,23 @@ helpFunc() { Show Pi-hole, Web Admin & FTL versions Repositories: - -a, --admin Show both current and latest versions of Web Admin - -f, --ftl Show both current and latest versions of FTL - -p, --pihole Show both current and latest versions of Pi-hole Core + -p, --pihole Only retrieve info regarding Pi-hole repository + -a, --admin Only retrieve info regarding AdminLTE repository + -f, --ftl Only retrieve info regarding FTL repository Options: - -c, --current (Only after -a | -p | -f) Return the current version - -l, --latest (Only after -a | -p | -f) Return the latest version - -h, --hash (Only after -a | -p) Return the current Github hash + -c, --current Return the current version + -l, --latest Return the latest version + -h, --hash Return the Github hash from your local repositories --help Show this help dialog " exit 0 } case "${1}" in - "-a" | "--admin" ) shift; webOutput "$@";; - "-p" | "--pihole" ) shift; coreOutput "$@";; - "-f" | "--ftl" ) shift; ftlOutput "$@";; + "-p" | "--pihole" ) shift; versionOutput "pi-hole" "$@";; + "-a" | "--admin" ) shift; versionOutput "AdminLTE" "$@";; + "-f" | "--ftl" ) shift; versionOutput "FTL" "$@";; "--help" ) helpFunc;; * ) defaultOutput "$@";; esac