From 03201e2f201420626c61a8d8890e7079c1e35ac6 Mon Sep 17 00:00:00 2001 From: WaLLy3K Date: Wed, 10 May 2017 13:07:56 +1000 Subject: [PATCH] Display FTL version & version.sh rewrite While testing to make sure `pihole -v` would output `pihole-FTL version`, I noticed some options didn't work how I expected them to. For example, if I use `pihole -v -p`, I would expect to see the version output of Pi-hole Core. Instead, I'm informed that it's an invalid option. I've had the following things in mind while rewriting this: * I'm operating under the assumption that FTL is only installed if the Admin Console is (Line 113 exit 0) * I have modified the help text to only output with `pihole -v --help` * I have modified all output to be more similar to the output style of `grep` and `curl` (Ditching ":::") Testing output: ``` w3k@MCT:~$ pihole -v Pi-hole version is v3.0.1-14-ga928cd3 (Latest: v3.0.1) Admin Console version is v3.0-9-g3760482 (Latest: v3.0.1) FTL version is v2.6.2 (Latest: v2.6.2) w3k@MCT:~$ pihole -v -c Current Pi-hole version is v3.0.1-14-ga928cd3 Current Admin Console version is v3.0-9-g3760482 Current FTL version is v2.6.2 w3k@MCT:~$ pihole -v -l Latest Pi-hole version is v3.0.1 Latest Admin Console version is v3.0.1 Latest FTL version is v2.6.2 w3k@MCT:~$ pihole -v -p --hash Current Pi-hole hash is a928cd3 w3k@MCT:~$ pihole -v -a --hash Current Admin Console hash is 3760482 w3k@MCT:~$ pihole -v --help Usage: pihole -v [REPO | OPTION] [OPTION] Show Pi-hole, Web Admin & FTL versions w3k@MCT:~$ pihole -v -foo Invalid Option! ``` --- advanced/Scripts/version.sh | 127 +++++++++++++++++++++--------------- 1 file changed, 74 insertions(+), 53 deletions(-) diff --git a/advanced/Scripts/version.sh b/advanced/Scripts/version.sh index 7f96e29a..289e4d93 100755 --- a/advanced/Scripts/version.sh +++ b/advanced/Scripts/version.sh @@ -66,72 +66,93 @@ getRemoteVersion(){ return 0 } -#PHHASHLATEST=$(curl -s https://api.github.com/repos/pi-hole/pi-hole/commits/master | \ -# grep sha | \ -# head -n1 | \ -# awk -F ' ' '{ print $2 }' | \ -# tr -cd '[[:alnum:]]._-') - -#WEBHASHLATEST=$(curl -s https://api.github.com/repos/pi-hole/AdminLTE/commits/master | \ -# grep sha | \ -# head -n1 | \ -# awk -F ' ' '{ print $2 }' | \ -# tr -cd '[[:alnum:]]._-') - - -normalOutput() { - echo "::: Pi-hole version is $(getLocalVersion "${PHGITDIR}") (Latest version is $(getRemoteVersion pi-hole))" - if [ -d "${WEBGITDIR}" ]; then - echo "::: Web-Admin version is $(getLocalVersion "${WEBGITDIR}") (Latest version is $(getRemoteVersion AdminLTE))" - fi +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" = "--hash" ] && hash="$(getLocalHash ${PHGITDIR})" + + 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 + echo " Invalid Option! Try 'pihole -v --help' for more information." + exit 1 + fi + echo " $str" } webOutput() { - if [ -d "${WEBGITDIR}" ]; then - case "${1}" in - "-l" | "--latest" ) echo $(getRemoteVersion AdminLTE);; - "-c" | "--current" ) echo $(getLocalVersion "${WEBGITDIR}");; - "-h" | "--hash" ) echo $(getLocalHash "${WEBGITDIR}");; - * ) echo "::: Invalid Option!"; exit 1; - esac + [ "$1" = "-c" -o "$1" = "--current" -o -z "$1" ] && current="$(getLocalVersion ${WEBGITDIR})" + [ "$1" = "-l" -o "$1" = "--latest" -o -z "$1" ] && latest="$(getRemoteVersion AdminLTE)" + [ "$1" = "--hash" ] && hash="$(getLocalHash ${WEBGITDIR})" + [ ! -d "${WEBGITDIR}" ] && str="Web interface not installed!" + + if [ -n "$current" -a -n "$latest" ]; then + str="Admin Console version is $current (Latest: $latest)" + elif [ -n "$current" -a -z "$latest" ]; then + str="Current Admin Console version is $current" + elif [ -z "$current" -a -n "$latest" ]; then + str="Latest Admin Console version is $latest" + elif [ -n "$hash" ]; then + str="Current Admin Console hash is $hash" else - echo "::: Web interface not installed!"; exit 1; + echo " Invalid Option! Try 'pihole -v --help' for more information." + exit 1 fi + echo " $str" } -coreOutput() { - case "${1}" in - "-l" | "--latest" ) echo $(getRemoteVersion pi-hole);; - "-c" | "--current" ) echo $(getLocalVersion "${PHGITDIR}");; - "-h" | "--hash" ) echo $(getLocalHash "${PHGITDIR}");; - * ) echo "::: Invalid Option!"; exit 1; - esac +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 + + 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 + echo " Invalid Option! Try 'pihole -v --help' for more information." + exit 1 + fi + echo " $str" +} + +defaultOutput() { + coreOutput "$1" + webOutput "$1" + ftlOutput "$1" } helpFunc() { - cat << EOM -::: -::: Show Pi-hole/Web Admin versions -::: -::: Usage: pihole -v [ -a | -p ] [ -l | -c ] -::: -::: Options: -::: -a, --admin Show both current and latest versions of web admin -::: -p, --pihole Show both current and latest versions of Pi-hole core files -::: -l, --latest (Only after -a | -p) Return only latest version -::: -c, --current (Only after -a | -p) Return only current version -::: -h, --help Show this help dialog -::: -EOM + echo "Usage: pihole -v [REPO | OPTION] [OPTION] +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 + +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 + --help Show this help dialog +" exit 0 } -if [[ $# = 0 ]]; then - normalOutput -fi - case "${1}" in "-a" | "--admin" ) shift; webOutput "$@";; - "-p" | "--pihole" ) shift; coreOutput "$@" ;; - "-h" | "--help" ) helpFunc;; + "-p" | "--pihole" ) shift; coreOutput "$@";; + "-f" | "--ftl" ) shift; ftlOutput "$@";; + "--help" ) helpFunc;; + * ) defaultOutput "$@";; esac