Minimise string duplication & other minor changes

Instead of duplicating output strings, rewrite core/web/ftlOutput() into one neat versionOutput().
This commit is contained in:
WaLLy3K 2017-05-11 13:34:58 +10:00 committed by GitHub
parent 7fef1fdc83
commit 2863308090
1 changed files with 44 additions and 79 deletions

View File

@ -10,10 +10,16 @@
# Variables # Variables
DEFAULT="-1" DEFAULT="-1"
PHGITDIR="/etc/.pihole/" COREGITDIR="/etc/.pihole/"
WEBGITDIR="/var/www/html/admin/" WEBGITDIR="/var/www/html/admin/"
getLocalVersion() { 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 # Get the tagged version of the local repository
local directory="${1}" local directory="${1}"
local version local version
@ -33,6 +39,12 @@ getLocalVersion() {
} }
getLocalHash() { getLocalHash() {
# FTL hash is not applicable
if [ "$1" == "FTL" ]; then
echo "N/A"
return 0
fi
# Get the short hash of the local repository # Get the short hash of the local repository
local directory="${1}" local directory="${1}"
local hash local hash
@ -66,88 +78,41 @@ getRemoteVersion(){
return 0 return 0
} }
coreOutput() { versionOutput() {
[ "$1" = "-c" -o "$1" = "--current" -o -z "$1" ] && current="$(getLocalVersion ${PHGITDIR})" [ "$1" == "pi-hole" ] && GITDIR=${COREGITDIR}
[ "$1" = "-l" -o "$1" = "--latest" -o -z "$1" ] && latest="$(getRemoteVersion pi-hole)" [ "$1" == "AdminLTE" ] && GITDIR=${WEBGITDIR}
[ "$1" = "-h" -o "$1" = "--hash" ] && hash="$(getLocalHash ${PHGITDIR})" [ "$1" == "FTL" ] && GITDIR="FTL"
[ -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"
[ "$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 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 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 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 elif [ -n "$hash" ]; then
str="Current Admin Console hash is $hash" output="Current ${1^} hash is $hash"
else else
error="true" errorOutput
fi fi
if [ "$error" = "true" ]; then [ -n "$output" ] && echo " $output"
echo " Invalid Option! Try 'pihole -v --help' for more information."
exit 1
fi
echo " $str"
} }
ftlOutput() { errorOutput() {
[ "$1" = "-c" -o "$1" = "--current" -o -z "$1" ] && current="$(pihole-FTL version)" echo " Invalid Option! Try 'pihole -v --help' for more information."
[ "$1" = "-l" -o "$1" = "--latest" -o -z "$1" ] && latest="$(getRemoteVersion FTL)" exit 1
[ ! -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"
} }
defaultOutput() { defaultOutput() {
coreOutput "$1" "$2" versionOutput "pi-hole" "$@"
webOutput "$1" "$2" versionOutput "AdminLTE" "$@"
ftlOutput "$1" "$2" versionOutput "FTL" "$@"
} }
helpFunc() { helpFunc() {
@ -155,23 +120,23 @@ helpFunc() {
Show Pi-hole, Web Admin & FTL versions Show Pi-hole, Web Admin & FTL versions
Repositories: Repositories:
-a, --admin Show both current and latest versions of Web Admin -p, --pihole Only retrieve info regarding Pi-hole repository
-f, --ftl Show both current and latest versions of FTL -a, --admin Only retrieve info regarding AdminLTE repository
-p, --pihole Show both current and latest versions of Pi-hole Core -f, --ftl Only retrieve info regarding FTL repository
Options: Options:
-c, --current (Only after -a | -p | -f) Return the current version -c, --current Return the current version
-l, --latest (Only after -a | -p | -f) Return the latest version -l, --latest Return the latest version
-h, --hash (Only after -a | -p) Return the current Github hash -h, --hash Return the Github hash from your local repositories
--help Show this help dialog --help Show this help dialog
" "
exit 0 exit 0
} }
case "${1}" in case "${1}" in
"-a" | "--admin" ) shift; webOutput "$@";; "-p" | "--pihole" ) shift; versionOutput "pi-hole" "$@";;
"-p" | "--pihole" ) shift; coreOutput "$@";; "-a" | "--admin" ) shift; versionOutput "AdminLTE" "$@";;
"-f" | "--ftl" ) shift; ftlOutput "$@";; "-f" | "--ftl" ) shift; versionOutput "FTL" "$@";;
"--help" ) helpFunc;; "--help" ) helpFunc;;
* ) defaultOutput "$@";; * ) defaultOutput "$@";;
esac esac