From 81939ab265f9b85516f673df3b3b13467371ded7 Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Fri, 3 Mar 2017 19:14:30 -0800 Subject: [PATCH 01/11] Get short hash of local and remote Signed-off-by: Dan Schaper --- advanced/Scripts/version.sh | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/advanced/Scripts/version.sh b/advanced/Scripts/version.sh index 11a7af00..27166507 100755 --- a/advanced/Scripts/version.sh +++ b/advanced/Scripts/version.sh @@ -17,11 +17,34 @@ current=false DEFAULT="-1" normalOutput() { - piholeVersion=$(cd /etc/.pihole/ && git describe --tags --abbrev=0) - webVersion=$(cd /var/www/html/admin/ && git describe --tags --abbrev=0) + piholeVersion=$(cd /etc/.pihole/ \ + && git describe --tags --abbrev=0) + webVersion=$(cd /var/www/html/admin/ \ + && git describe --tags --abbrev=0) - piholeVersionLatest=$(curl -s https://api.github.com/repos/pi-hole/pi-hole/releases/latest | grep -Po '"tag_name":.*?[^\\]",' | perl -pe 's/"tag_name": "//; s/^"//; s/",$//') - webVersionLatest=$(curl -s https://api.github.com/repos/pi-hole/AdminLTE/releases/latest | grep -Po '"tag_name":.*?[^\\]",' | perl -pe 's/"tag_name": "//; s/^"//; s/",$//') + piholeHash=$(cd /etc/.pihole/ \ + && git rev-parse --short HEAD) + webHash=$(cd /var/www/html/admin/ \ + && git rev-parse --short HEAD) + + piholeVersionLatest=$(curl -s https://api.github.com/repos/pi-hole/pi-hole/releases/latest | \ + grep -Po '"tag_name":.*?[^\\]",' | \ + perl -pe 's/"tag_name": "//; s/^"//; s/",$//') + webVersionLatest=$(curl -s https://api.github.com/repos/pi-hole/AdminLTE/releases/latest | \ + grep -Po '"tag_name":.*?[^\\]",' | \ + perl -pe 's/"tag_name": "//; s/^"//; s/",$//') + + piholeHashLatest=$(curl -s https://api.github.com/repos/pi-hole/pi-hole/commits/master | \ + grep sha | \ + head -n1 | \ + awk -F ' ' '{ print $2}' | \ + tr -cd '[[:alnum:]]._-') + + piholeHashLatest=$(curl -s https://api.github.com/repos/pi-hole/AdminLTE/commits/master | \ + grep sha | \ + head -n1 | \ + awk -F ' ' '{ print $2}' | \ + tr -cd '[[:alnum:]]._-') echo "::: Pi-hole version is ${piholeVersion} (Latest version is ${piholeVersionLatest:-${DEFAULT}})" echo "::: Web-Admin version is ${webVersion} (Latest version is ${webVersionLatest:-${DEFAULT}})" From ebfffea5dce3befd2ea6a1067228c5069a96cc64 Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Fri, 3 Mar 2017 20:04:55 -0800 Subject: [PATCH 02/11] Always show something, even if it's just the short hash of the latest commit. Signed-off-by: Dan Schaper --- advanced/Scripts/version.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/advanced/Scripts/version.sh b/advanced/Scripts/version.sh index 27166507..3a04d2ec 100755 --- a/advanced/Scripts/version.sh +++ b/advanced/Scripts/version.sh @@ -8,8 +8,6 @@ # This file is copyright under the latest version of the EUPL. # Please see LICENSE file for your rights under this license. - - # Flags: latest=false current=false @@ -18,9 +16,9 @@ DEFAULT="-1" normalOutput() { piholeVersion=$(cd /etc/.pihole/ \ - && git describe --tags --abbrev=0) + && git describe --tags --always) webVersion=$(cd /var/www/html/admin/ \ - && git describe --tags --abbrev=0) + && git describe --tags --always) piholeHash=$(cd /etc/.pihole/ \ && git rev-parse --short HEAD) From 9bb4a5fb253957a2969decc48f80ee115d315e4e Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Fri, 3 Mar 2017 20:16:53 -0800 Subject: [PATCH 03/11] Factor out variables from display. Signed-off-by: Dan Schaper --- advanced/Scripts/version.sh | 81 +++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 45 deletions(-) diff --git a/advanced/Scripts/version.sh b/advanced/Scripts/version.sh index 3a04d2ec..a094ba16 100755 --- a/advanced/Scripts/version.sh +++ b/advanced/Scripts/version.sh @@ -12,40 +12,39 @@ latest=false current=false +# Variables DEFAULT="-1" +PHVERSION=$(cd /etc/.pihole/ \ + && git describe --tags --always) +WEBVERSION=$(cd /var/www/html/admin/ \ + && git describe --tags --always) +PHHASH=$(cd /etc/.pihole/ \ + && git rev-parse --short HEAD) +WEBHASH=$(cd /var/www/html/admin/ \ + && git rev-parse --short HEAD) + +PHVERSIONLATEST=$(curl -s https://api.github.com/repos/pi-hole/pi-hole/releases/latest | \ + grep -Po '"tag_name":.*?[^\\]",' | \ + perl -pe 's/"tag_name": "//; s/^"//; s/",$//') +WEBVERSIONLATEST=$(curl -s https://api.github.com/repos/pi-hole/AdminLTE/releases/latest | \ + grep -Po '"tag_name":.*?[^\\]",' | \ + perl -pe 's/"tag_name": "//; s/^"//; s/",$//') + +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() { - piholeVersion=$(cd /etc/.pihole/ \ - && git describe --tags --always) - webVersion=$(cd /var/www/html/admin/ \ - && git describe --tags --always) - - piholeHash=$(cd /etc/.pihole/ \ - && git rev-parse --short HEAD) - webHash=$(cd /var/www/html/admin/ \ - && git rev-parse --short HEAD) - - piholeVersionLatest=$(curl -s https://api.github.com/repos/pi-hole/pi-hole/releases/latest | \ - grep -Po '"tag_name":.*?[^\\]",' | \ - perl -pe 's/"tag_name": "//; s/^"//; s/",$//') - webVersionLatest=$(curl -s https://api.github.com/repos/pi-hole/AdminLTE/releases/latest | \ - grep -Po '"tag_name":.*?[^\\]",' | \ - perl -pe 's/"tag_name": "//; s/^"//; s/",$//') - - piholeHashLatest=$(curl -s https://api.github.com/repos/pi-hole/pi-hole/commits/master | \ - grep sha | \ - head -n1 | \ - awk -F ' ' '{ print $2}' | \ - tr -cd '[[:alnum:]]._-') - - piholeHashLatest=$(curl -s https://api.github.com/repos/pi-hole/AdminLTE/commits/master | \ - grep sha | \ - head -n1 | \ - awk -F ' ' '{ print $2}' | \ - tr -cd '[[:alnum:]]._-') - - echo "::: Pi-hole version is ${piholeVersion} (Latest version is ${piholeVersionLatest:-${DEFAULT}})" - echo "::: Web-Admin version is ${webVersion} (Latest version is ${webVersionLatest:-${DEFAULT}})" + echo "::: Pi-hole version is ${PHVERSION} (Latest version is ${PHVERSIONLATEST:-${DEFAULT}})" + echo "::: Web-Admin version is ${WEBVERSION} (Latest version is ${WEBVERSIONLATEST:-${DEFAULT}})" } webOutput() { @@ -58,15 +57,11 @@ webOutput() { done if [[ "${latest}" == true && "${current}" == false ]]; then - webVersionLatest=$(curl -s https://api.github.com/repos/pi-hole/AdminLTE/releases/latest | grep -Po '"tag_name":.*?[^\\]",' | perl -pe 's/"tag_name": "//; s/^"//; s/",$//') - echo "${webVersionLatest:--1}" + echo "${WEBVERSIONLATEST:--1}" elif [[ "${latest}" == false && "${current}" == true ]]; then - webVersion=$(cd /var/www/html/admin/ && git describe --tags --abbrev=0) - echo "${webVersion}" + echo "${WEBVERSION}" else - webVersion=$(cd /var/www/html/admin/ && git describe --tags --abbrev=0) - webVersionLatest=$(curl -s https://api.github.com/repos/pi-hole/AdminLTE/releases/latest | grep -Po '"tag_name":.*?[^\\]",' | perl -pe 's/"tag_name": "//; s/^"//; s/",$//') - echo "::: Web-Admin version is ${webVersion} (Latest version is ${webVersionLatest:-${DEFAULT}})" + echo "::: Web-Admin version is ${WEBVERSION} (Latest version is ${WEBVERSIONLATEST:-${DEFAULT}})" fi } @@ -80,15 +75,11 @@ coreOutput() { done if [[ "${latest}" == true && "${current}" == false ]]; then - piholeVersionLatest=$(curl -s https://api.github.com/repos/pi-hole/pi-hole/releases/latest | grep -Po '"tag_name":.*?[^\\]",' | perl -pe 's/"tag_name": "//; s/^"//; s/",$//') - echo "${piholeVersionLatest:--1}" + echo "${PHVERSIONLATEST:--1}" elif [[ "${latest}" == false && "${current}" == true ]]; then - piholeVersion=$(cd /etc/.pihole/ && git describe --tags --abbrev=0) - echo "${piholeVersion}" + echo "${PHVERSION}" else - piholeVersion=$(cd /etc/.pihole/ && git describe --tags --abbrev=0) - piholeVersionLatest=$(curl -s https://api.github.com/repos/pi-hole/pi-hole/releases/latest | grep -Po '"tag_name":.*?[^\\]",' | perl -pe 's/"tag_name": "//; s/^"//; s/",$//') - echo "::: Pi-hole version is ${piholeVersion} (Latest version is ${piholeVersionLatest:-${DEFAULT}})" + echo "::: Pi-hole version is ${PHVERSION} (Latest version is ${PHVERSIONLATEST:-${DEFAULT}})" fi } From 0341bd175871f2e6bdc6dce5c3ca8c0aa500f2b6 Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Fri, 3 Mar 2017 20:32:26 -0800 Subject: [PATCH 04/11] Optimize code, undoc flag for current hash. Signed-off-by: Dan Schaper --- advanced/Scripts/version.sh | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/advanced/Scripts/version.sh b/advanced/Scripts/version.sh index a094ba16..b8876fc8 100755 --- a/advanced/Scripts/version.sh +++ b/advanced/Scripts/version.sh @@ -50,37 +50,23 @@ normalOutput() { webOutput() { for var in "$@"; do case "${var}" in - "-l" | "--latest" ) latest=true;; - "-c" | "--current" ) current=true;; + "-l" | "--latest" ) echo "${WEBVERSIONLATEST:--1}";; + "-c" | "--current" ) echo "${WEBVERSION}";; + "-h" | "--hash" ) echo "${WEBHASH}";; * ) echo "::: Invalid Option!"; exit 1; esac done - - if [[ "${latest}" == true && "${current}" == false ]]; then - echo "${WEBVERSIONLATEST:--1}" - elif [[ "${latest}" == false && "${current}" == true ]]; then - echo "${WEBVERSION}" - else - echo "::: Web-Admin version is ${WEBVERSION} (Latest version is ${WEBVERSIONLATEST:-${DEFAULT}})" - fi } coreOutput() { for var in "$@"; do case "${var}" in - "-l" | "--latest" ) latest=true;; - "-c" | "--current" ) current=true;; + "-l" | "--latest" ) echo "${PHVERSIONLATEST:--1}";; + "-c" | "--current" ) echo "${PHVERSION}";; + "-h" | "--hash" ) echo "${PHHASH}";; * ) echo "::: Invalid Option!"; exit 1; esac done - - if [[ "${latest}" == true && "${current}" == false ]]; then - echo "${PHVERSIONLATEST:--1}" - elif [[ "${latest}" == false && "${current}" == true ]]; then - echo "${PHVERSION}" - else - echo "::: Pi-hole version is ${PHVERSION} (Latest version is ${PHVERSIONLATEST:-${DEFAULT}})" - fi } helpFunc() { From 8831b22fc89a0aba2cf75c5cd86644ac760a301f Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Fri, 3 Mar 2017 21:36:38 -0800 Subject: [PATCH 05/11] Prototype function for getting version of local repo Signed-off-by: Dan Schaper --- advanced/Scripts/version.sh | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/advanced/Scripts/version.sh b/advanced/Scripts/version.sh index b8876fc8..3bd9d4d2 100755 --- a/advanced/Scripts/version.sh +++ b/advanced/Scripts/version.sh @@ -14,8 +14,26 @@ current=false # Variables DEFAULT="-1" -PHVERSION=$(cd /etc/.pihole/ \ - && git describe --tags --always) +PHGITDIR="/etc/.pihole/" +WEBGITDIR="/var/www/html/admin/" + +getLocalPHVersion(){ + # Get the tagged version of the local Pi-hole repository + local version + + cd "${PHGITDIR}" || { PHVERSION="${DEFAULT}"; return -1; } + version=$(git describe --tags --always || \ + echo "${DEFAULT}") + if [[ "${version}" =~ ^v ]]; then + PHVERSION="${version}" + elif [[ "${version}" == "-1" ]]; then + PHVERSION="ERROR" + else + PHVERSION="Untagged" + fi + return 0 +} + WEBVERSION=$(cd /var/www/html/admin/ \ && git describe --tags --always) @@ -42,16 +60,18 @@ WEBHASHLATEST=$(curl -s https://api.github.com/repos/pi-hole/AdminLTE/commits/ma head -n1 | \ awk -F ' ' '{ print $2}' | \ tr -cd '[[:alnum:]]._-') + + normalOutput() { echo "::: Pi-hole version is ${PHVERSION} (Latest version is ${PHVERSIONLATEST:-${DEFAULT}})" - echo "::: Web-Admin version is ${WEBVERSION} (Latest version is ${WEBVERSIONLATEST:-${DEFAULT}})" + echo "::: Web-Admin version is ${WEBVERSION:-Untagged} (Latest version is ${WEBVERSIONLATEST:-${DEFAULT}})" } webOutput() { for var in "$@"; do case "${var}" in - "-l" | "--latest" ) echo "${WEBVERSIONLATEST:--1}";; - "-c" | "--current" ) echo "${WEBVERSION}";; + "-l" | "--latest" ) echo "${WEBVERSIONLATEST:-${DEFAULT}}";; + "-c" | "--current" ) echo "${WEBVERSION:-Untagged}";; "-h" | "--hash" ) echo "${WEBHASH}";; * ) echo "::: Invalid Option!"; exit 1; esac @@ -61,7 +81,7 @@ webOutput() { coreOutput() { for var in "$@"; do case "${var}" in - "-l" | "--latest" ) echo "${PHVERSIONLATEST:--1}";; + "-l" | "--latest" ) echo "${PHVERSIONLATEST:-${DEFAULT}}";; "-c" | "--current" ) echo "${PHVERSION}";; "-h" | "--hash" ) echo "${PHHASH}";; * ) echo "::: Invalid Option!"; exit 1; @@ -87,6 +107,8 @@ EOM exit 0 } +getLocalPHVersion + if [[ $# = 0 ]]; then normalOutput fi From ed8088f20377be8eb9918f7c1b0f732415486499 Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Fri, 3 Mar 2017 22:10:32 -0800 Subject: [PATCH 06/11] Functions for Core/Web version and hash. And only check first argument in each section. Signed-off-by: Dan Schaper --- advanced/Scripts/version.sh | 58 ++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/advanced/Scripts/version.sh b/advanced/Scripts/version.sh index 3bd9d4d2..edcb8958 100755 --- a/advanced/Scripts/version.sh +++ b/advanced/Scripts/version.sh @@ -8,10 +8,6 @@ # This file is copyright under the latest version of the EUPL. # Please see LICENSE file for your rights under this license. -# Flags: -latest=false -current=false - # Variables DEFAULT="-1" PHGITDIR="/etc/.pihole/" @@ -20,27 +16,54 @@ WEBGITDIR="/var/www/html/admin/" getLocalPHVersion(){ # Get the tagged version of the local Pi-hole repository local version + local hash - cd "${PHGITDIR}" || { PHVERSION="${DEFAULT}"; return -1; } + cd "${PHGITDIR}" || { PHVERSION="${DEFAULT}"; return 1; } version=$(git describe --tags --always || \ echo "${DEFAULT}") if [[ "${version}" =~ ^v ]]; then PHVERSION="${version}" - elif [[ "${version}" == "-1" ]]; then + elif [[ "${version}" == "${DEFAULT}" ]]; then PHVERSION="ERROR" else PHVERSION="Untagged" fi + + hash=$(git rev-parse --short HEAD || \ + echo "${DEFAULT}") + if [[ "${hash}" == "${DEFAULT}" ]]; then + PHHASH="ERROR" + else + PHHASH="${hash}" + fi return 0 } -WEBVERSION=$(cd /var/www/html/admin/ \ - && git describe --tags --always) +getLocalWebVersion(){ + # Get the tagged version of the local Pi-hole repository + local version + local hash -PHHASH=$(cd /etc/.pihole/ \ - && git rev-parse --short HEAD) -WEBHASH=$(cd /var/www/html/admin/ \ - && git rev-parse --short HEAD) + cd "${WEBGITDIR}" || { WEBVERSION="${DEFAULT}"; return 1; } + version=$(git describe --tags --always || \ + echo "${DEFAULT}") + if [[ "${version}" =~ ^v ]]; then + WEBVERSION="${version}" + elif [[ "${version}" == "${DEFAULT}" ]]; then + WEBVERSION="ERROR" + else + WEBVERSION="Untagged" + fi + + hash=$(git rev-parse --short HEAD || \ + echo "${DEFAULT}") + if [[ "${hash}" == "${DEFAULT}" ]]; then + WEBHASH="ERROR" + else + WEBHASH="${hash}" + fi + return 0 +} PHVERSIONLATEST=$(curl -s https://api.github.com/repos/pi-hole/pi-hole/releases/latest | \ grep -Po '"tag_name":.*?[^\\]",' | \ @@ -64,14 +87,14 @@ WEBHASHLATEST=$(curl -s https://api.github.com/repos/pi-hole/AdminLTE/commits/ma normalOutput() { echo "::: Pi-hole version is ${PHVERSION} (Latest version is ${PHVERSIONLATEST:-${DEFAULT}})" - echo "::: Web-Admin version is ${WEBVERSION:-Untagged} (Latest version is ${WEBVERSIONLATEST:-${DEFAULT}})" + echo "::: Web-Admin version is ${WEBVERSION} (Latest version is ${WEBVERSIONLATEST:-${DEFAULT}})" } webOutput() { - for var in "$@"; do + for var in "$1"; do case "${var}" in "-l" | "--latest" ) echo "${WEBVERSIONLATEST:-${DEFAULT}}";; - "-c" | "--current" ) echo "${WEBVERSION:-Untagged}";; + "-c" | "--current" ) echo "${WEBVERSION}";; "-h" | "--hash" ) echo "${WEBHASH}";; * ) echo "::: Invalid Option!"; exit 1; esac @@ -79,7 +102,7 @@ webOutput() { } coreOutput() { - for var in "$@"; do + for var in "$1"; do case "${var}" in "-l" | "--latest" ) echo "${PHVERSIONLATEST:-${DEFAULT}}";; "-c" | "--current" ) echo "${PHVERSION}";; @@ -108,12 +131,13 @@ EOM } getLocalPHVersion +getLocalWebVersion if [[ $# = 0 ]]; then normalOutput fi -for var in "$@"; do +for var in "$1"; do case "${var}" in "-a" | "--admin" ) shift; webOutput "$@";; "-p" | "--pihole" ) shift; coreOutput "$@" ;; From 77ab47a9849bb0491e30bd437cf112791098836b Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Fri, 3 Mar 2017 22:34:55 -0800 Subject: [PATCH 07/11] Awk for perl Signed-off-by: Dan Schaper --- advanced/Scripts/version.sh | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/advanced/Scripts/version.sh b/advanced/Scripts/version.sh index edcb8958..d930b8a2 100755 --- a/advanced/Scripts/version.sh +++ b/advanced/Scripts/version.sh @@ -66,23 +66,23 @@ getLocalWebVersion(){ } PHVERSIONLATEST=$(curl -s https://api.github.com/repos/pi-hole/pi-hole/releases/latest | \ - grep -Po '"tag_name":.*?[^\\]",' | \ - perl -pe 's/"tag_name": "//; s/^"//; s/",$//') + awk -F: '$1 ~/tag_name/ { print $2 }' | \ + tr -cd '[[:alnum:]]._-') WEBVERSIONLATEST=$(curl -s https://api.github.com/repos/pi-hole/AdminLTE/releases/latest | \ - grep -Po '"tag_name":.*?[^\\]",' | \ - perl -pe 's/"tag_name": "//; s/^"//; s/",$//') + awk -F: '$1 ~/tag_name/ { print $2 }' | \ + tr -cd '[[:alnum:]]._-') -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:]]._-') +#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:]]._-') +#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() { From 945e22874e260d1be8d6fd453c8142ff68d30dd5 Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Fri, 3 Mar 2017 23:12:28 -0800 Subject: [PATCH 08/11] Collapse duplicate functions, and take out argument loops. Signed-off-by: Dan Schaper --- advanced/Scripts/version.sh | 93 +++++++++++++++---------------------- 1 file changed, 37 insertions(+), 56 deletions(-) diff --git a/advanced/Scripts/version.sh b/advanced/Scripts/version.sh index d930b8a2..3089e00c 100755 --- a/advanced/Scripts/version.sh +++ b/advanced/Scripts/version.sh @@ -13,54 +13,38 @@ DEFAULT="-1" PHGITDIR="/etc/.pihole/" WEBGITDIR="/var/www/html/admin/" -getLocalPHVersion(){ - # Get the tagged version of the local Pi-hole repository +getLocalVersion() { + # Get the tagged version of the local repository + local directory="${1}" local version - local hash - cd "${PHGITDIR}" || { PHVERSION="${DEFAULT}"; return 1; } + cd "${directory}" || { echo "${DEFAULT}"; return 1; } version=$(git describe --tags --always || \ echo "${DEFAULT}") if [[ "${version}" =~ ^v ]]; then - PHVERSION="${version}" + echo "${version}" elif [[ "${version}" == "${DEFAULT}" ]]; then - PHVERSION="ERROR" + echo "ERROR" + return 1 else - PHVERSION="Untagged" - fi - - hash=$(git rev-parse --short HEAD || \ - echo "${DEFAULT}") - if [[ "${hash}" == "${DEFAULT}" ]]; then - PHHASH="ERROR" - else - PHHASH="${hash}" + echo "Untagged" fi return 0 } -getLocalWebVersion(){ - # Get the tagged version of the local Pi-hole repository - local version +getLocalHash() { + # Get the short hash of the local repository + local directory="${1}" local hash - cd "${WEBGITDIR}" || { WEBVERSION="${DEFAULT}"; return 1; } - version=$(git describe --tags --always || \ - echo "${DEFAULT}") - if [[ "${version}" =~ ^v ]]; then - WEBVERSION="${version}" - elif [[ "${version}" == "${DEFAULT}" ]]; then - WEBVERSION="ERROR" - else - WEBVERSION="Untagged" - fi - + cd "${directory}" || { echo "${DEFAULT}"; return 1; } hash=$(git rev-parse --short HEAD || \ echo "${DEFAULT}") if [[ "${hash}" == "${DEFAULT}" ]]; then - WEBHASH="ERROR" + echo "ERROR" + return 1 else - WEBHASH="${hash}" + echo "${hash}" fi return 0 } @@ -91,25 +75,21 @@ normalOutput() { } webOutput() { - for var in "$1"; do - case "${var}" in - "-l" | "--latest" ) echo "${WEBVERSIONLATEST:-${DEFAULT}}";; - "-c" | "--current" ) echo "${WEBVERSION}";; - "-h" | "--hash" ) echo "${WEBHASH}";; - * ) echo "::: Invalid Option!"; exit 1; - esac - done + case "${1}" in + "-l" | "--latest" ) echo "${WEBVERSIONLATEST:-${DEFAULT}}";; + "-c" | "--current" ) echo "${WEBVERSION}";; + "-h" | "--hash" ) echo "${WEBHASH}";; + * ) echo "::: Invalid Option!"; exit 1; + esac } coreOutput() { - for var in "$1"; do - case "${var}" in - "-l" | "--latest" ) echo "${PHVERSIONLATEST:-${DEFAULT}}";; - "-c" | "--current" ) echo "${PHVERSION}";; - "-h" | "--hash" ) echo "${PHHASH}";; - * ) echo "::: Invalid Option!"; exit 1; - esac - done + case "${1}" in + "-l" | "--latest" ) echo "${PHVERSIONLATEST:-${DEFAULT}}";; + "-c" | "--current" ) echo "${PHVERSION}";; + "-h" | "--hash" ) echo "${PHHASH}";; + * ) echo "::: Invalid Option!"; exit 1; + esac } helpFunc() { @@ -130,17 +110,18 @@ EOM exit 0 } -getLocalPHVersion -getLocalWebVersion +PHVERSION=$(getLocalVersion "${PHGITDIR}") +PHHASH=$(getLocalHash "${PHGITDIR}") +WEBVERSION=$(getLocalVersion "${WEBGITDIR}") +WEBHASH=$(getLocalHash "${WEBGITDIR}") + if [[ $# = 0 ]]; then normalOutput fi -for var in "$1"; do - case "${var}" in - "-a" | "--admin" ) shift; webOutput "$@";; - "-p" | "--pihole" ) shift; coreOutput "$@" ;; - "-h" | "--help" ) helpFunc;; - esac -done +case "${1}" in + "-a" | "--admin" ) shift; webOutput "$@";; + "-p" | "--pihole" ) shift; coreOutput "$@" ;; + "-h" | "--help" ) helpFunc;; +esac From 0e8fd49669e5b5018439597585da424b516f9f57 Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Fri, 3 Mar 2017 23:30:57 -0800 Subject: [PATCH 09/11] Function... Signed-off-by: Dan Schaper --- advanced/Scripts/version.sh | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/advanced/Scripts/version.sh b/advanced/Scripts/version.sh index 3089e00c..68e84900 100755 --- a/advanced/Scripts/version.sh +++ b/advanced/Scripts/version.sh @@ -49,12 +49,22 @@ getLocalHash() { return 0 } -PHVERSIONLATEST=$(curl -s https://api.github.com/repos/pi-hole/pi-hole/releases/latest | \ - awk -F: '$1 ~/tag_name/ { print $2 }' | \ - tr -cd '[[:alnum:]]._-') -WEBVERSIONLATEST=$(curl -s https://api.github.com/repos/pi-hole/AdminLTE/releases/latest | \ - awk -F: '$1 ~/tag_name/ { print $2 }' | \ - tr -cd '[[:alnum:]]._-') +getRemoteVersion(){ + # Get the version from the remote origin + local daemon="${1}" + local version + + version=$(curl --silent --fail https://api.github.com/repos/pi-hole/${daemon}/releases/latest | \ + awk -F: '$1 ~/tag_name/ { print $2 }' | \ + tr -cd '[[:alnum:]]._-') + if [[ "${version}" =~ ^v ]]; then + echo "${version}" + else + echo "ERROR" + return 1 + fi + return 0 +} #PHHASHLATEST=$(curl -s https://api.github.com/repos/pi-hole/pi-hole/commits/master | \ # grep sha | \ @@ -70,13 +80,13 @@ WEBVERSIONLATEST=$(curl -s https://api.github.com/repos/pi-hole/AdminLTE/release normalOutput() { - echo "::: Pi-hole version is ${PHVERSION} (Latest version is ${PHVERSIONLATEST:-${DEFAULT}})" - echo "::: Web-Admin version is ${WEBVERSION} (Latest version is ${WEBVERSIONLATEST:-${DEFAULT}})" + echo "::: Pi-hole version is ${PHVERSION} (Latest version is ${PHVERSIONLATEST})" + echo "::: Web-Admin version is ${WEBVERSION} (Latest version is ${WEBVERSIONLATEST})" } webOutput() { case "${1}" in - "-l" | "--latest" ) echo "${WEBVERSIONLATEST:-${DEFAULT}}";; + "-l" | "--latest" ) echo "${WEBVERSIONLATEST}";; "-c" | "--current" ) echo "${WEBVERSION}";; "-h" | "--hash" ) echo "${WEBHASH}";; * ) echo "::: Invalid Option!"; exit 1; @@ -85,7 +95,7 @@ webOutput() { coreOutput() { case "${1}" in - "-l" | "--latest" ) echo "${PHVERSIONLATEST:-${DEFAULT}}";; + "-l" | "--latest" ) echo "${PHVERSIONLATEST}";; "-c" | "--current" ) echo "${PHVERSION}";; "-h" | "--hash" ) echo "${PHHASH}";; * ) echo "::: Invalid Option!"; exit 1; @@ -112,9 +122,10 @@ EOM PHVERSION=$(getLocalVersion "${PHGITDIR}") PHHASH=$(getLocalHash "${PHGITDIR}") +PHVERSIONLATEST=$(getRemoteVersion pi-hole) WEBVERSION=$(getLocalVersion "${WEBGITDIR}") WEBHASH=$(getLocalHash "${WEBGITDIR}") - +WEBVERSIONLATEST=$(getRemoteVersion AdminLTE) if [[ $# = 0 ]]; then normalOutput From b85af50d14381dc7eab4e0688af44baba8fd7f3d Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 4 Mar 2017 14:08:11 +0100 Subject: [PATCH 10/11] Generate only the information that is actually asked for --- advanced/Scripts/version.sh | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/advanced/Scripts/version.sh b/advanced/Scripts/version.sh index 68e84900..5674bce1 100755 --- a/advanced/Scripts/version.sh +++ b/advanced/Scripts/version.sh @@ -86,18 +86,18 @@ normalOutput() { webOutput() { case "${1}" in - "-l" | "--latest" ) echo "${WEBVERSIONLATEST}";; - "-c" | "--current" ) echo "${WEBVERSION}";; - "-h" | "--hash" ) echo "${WEBHASH}";; + "-l" | "--latest" ) echo $(getRemoteVersion AdminLTE);; + "-c" | "--current" ) echo $(getLocalVersion "${WEBGITDIR}");; + "-h" | "--hash" ) echo $(getLocalHash "${WEBGITDIR}");; * ) echo "::: Invalid Option!"; exit 1; esac } coreOutput() { case "${1}" in - "-l" | "--latest" ) echo "${PHVERSIONLATEST}";; - "-c" | "--current" ) echo "${PHVERSION}";; - "-h" | "--hash" ) echo "${PHHASH}";; + "-l" | "--latest" ) echo $(getRemoteVersion pi-hole);; + "-c" | "--current" ) echo $(getLocalVersion "${PHGITDIR}");; + "-h" | "--hash" ) echo $(getLocalHash "${PHGITDIR}");; * ) echo "::: Invalid Option!"; exit 1; esac } @@ -120,13 +120,6 @@ EOM exit 0 } -PHVERSION=$(getLocalVersion "${PHGITDIR}") -PHHASH=$(getLocalHash "${PHGITDIR}") -PHVERSIONLATEST=$(getRemoteVersion pi-hole) -WEBVERSION=$(getLocalVersion "${WEBGITDIR}") -WEBHASH=$(getLocalHash "${WEBGITDIR}") -WEBVERSIONLATEST=$(getRemoteVersion AdminLTE) - if [[ $# = 0 ]]; then normalOutput fi From abd3e828de63f28f7853839298093c20ff0ca2e5 Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Sat, 4 Mar 2017 07:46:00 -0800 Subject: [PATCH 11/11] Optimize normalOutput Signed-off-by: Dan Schaper --- advanced/Scripts/version.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/advanced/Scripts/version.sh b/advanced/Scripts/version.sh index 5674bce1..fd74c2de 100755 --- a/advanced/Scripts/version.sh +++ b/advanced/Scripts/version.sh @@ -80,8 +80,8 @@ getRemoteVersion(){ normalOutput() { - echo "::: Pi-hole version is ${PHVERSION} (Latest version is ${PHVERSIONLATEST})" - echo "::: Web-Admin version is ${WEBVERSION} (Latest version is ${WEBVERSIONLATEST})" + echo "::: Pi-hole version is $(getLocalVersion "${PHGITDIR}") (Latest version is $(getRemoteVersion pi-hole))" + echo "::: Web-Admin version is $(getLocalVersion "${WEBGITDIR}") (Latest version is $(getRemoteVersion AdminLTE))" } webOutput() {