mirror of
https://github.com/pi-hole/pi-hole
synced 2025-01-03 12:40:56 +00:00
Simplify version.sh
Signed-off-by: Christian König <ckoenig@posteo.de>
This commit is contained in:
parent
d7f7ef9965
commit
2dc599f266
@ -8,140 +8,63 @@
|
|||||||
# This file is copyright under the latest version of the EUPL.
|
# This file is copyright under the latest version of the EUPL.
|
||||||
# Please see LICENSE file for your rights under this license.
|
# Please see LICENSE file for your rights under this license.
|
||||||
|
|
||||||
# Variables
|
|
||||||
DEFAULT="-1"
|
|
||||||
COREGITDIR="/etc/.pihole/"
|
|
||||||
WEBGITDIR="/var/www/html/admin/"
|
|
||||||
|
|
||||||
# Source the setupvars config file
|
# Source the setupvars config file
|
||||||
# shellcheck disable=SC1091
|
# shellcheck disable=SC1091
|
||||||
source /etc/pihole/setupVars.conf
|
source /etc/pihole/setupVars.conf
|
||||||
|
|
||||||
getLocalVersion() {
|
# Sourece the versions file poupulated by updatechecker.sh
|
||||||
# FTL requires a different method
|
cachedVersions="/etc/pihole/versions"
|
||||||
if [[ "$1" == "FTL" ]]; then
|
|
||||||
pihole-FTL version
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Get the tagged version of the local repository
|
if [ -f ${cachedVersions} ]; then
|
||||||
local directory="${1}"
|
# shellcheck disable=SC1090
|
||||||
local version
|
. "$cachedVersions"
|
||||||
|
|
||||||
cd "${directory}" 2> /dev/null || { echo "${DEFAULT}"; return 1; }
|
|
||||||
version=$(git describe --tags --always || echo "$DEFAULT")
|
|
||||||
if [[ "${version}" =~ ^v ]]; then
|
|
||||||
echo "${version}"
|
|
||||||
elif [[ "${version}" == "${DEFAULT}" ]]; then
|
|
||||||
echo "ERROR"
|
|
||||||
return 1
|
|
||||||
else
|
else
|
||||||
echo "Untagged"
|
echo "Could not find /etc/pihole/versons. Exiting."
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
getLocalVersion() {
|
||||||
|
case ${1} in
|
||||||
|
"pi-hole" ) echo "${CORE_VERSION}";;
|
||||||
|
"AdminLTE" ) [[ "${INSTALL_WEB_INTERFACE}" == true ]] && echo "${WEB_VERSION}";;
|
||||||
|
"FTL" ) echo "${FTL_VERSION}";;
|
||||||
|
esac
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
getLocalHash() {
|
getLocalHash() {
|
||||||
# Local FTL hash does not exist on filesystem
|
case ${1} in
|
||||||
if [[ "$1" == "FTL" ]]; then
|
"pi-hole" ) echo "${CORE_HASH}";;
|
||||||
echo "N/A"
|
"AdminLTE" ) [[ "${INSTALL_WEB_INTERFACE}" == true ]] && echo "${WEB_HASH}";;
|
||||||
return 0
|
"FTL" ) echo "${FTL_HASH}";;
|
||||||
fi
|
esac
|
||||||
|
|
||||||
# Get the short hash of the local repository
|
|
||||||
local directory="${1}"
|
|
||||||
local hash
|
|
||||||
|
|
||||||
cd "${directory}" 2> /dev/null || { echo "${DEFAULT}"; return 1; }
|
|
||||||
hash=$(git rev-parse --short HEAD || echo "$DEFAULT")
|
|
||||||
if [[ "${hash}" == "${DEFAULT}" ]]; then
|
|
||||||
echo "ERROR"
|
|
||||||
return 1
|
|
||||||
else
|
|
||||||
echo "${hash}"
|
|
||||||
fi
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
getRemoteHash(){
|
getRemoteHash(){
|
||||||
# Remote FTL hash is not applicable
|
case ${1} in
|
||||||
if [[ "$1" == "FTL" ]]; then
|
"pi-hole" ) echo "${GITHUB_CORE_HASH}";;
|
||||||
echo "N/A"
|
"AdminLTE" ) [[ "${INSTALL_WEB_INTERFACE}" == true ]] && echo "${GITHUB_WEB_HASH}";;
|
||||||
return 0
|
"FTL" ) echo "${GITHUB_FTL_HASH}";;
|
||||||
fi
|
esac
|
||||||
|
|
||||||
local daemon="${1}"
|
|
||||||
local branch="${2}"
|
|
||||||
|
|
||||||
hash=$(git ls-remote --heads "https://github.com/pi-hole/${daemon}" | \
|
|
||||||
awk -v bra="$branch" '$0~bra {print substr($0,0,8);exit}')
|
|
||||||
if [[ -n "$hash" ]]; then
|
|
||||||
echo "$hash"
|
|
||||||
else
|
|
||||||
echo "ERROR"
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
getRemoteVersion(){
|
getRemoteVersion(){
|
||||||
# Get the version from the remote origin
|
case ${1} in
|
||||||
local daemon="${1}"
|
|
||||||
local version
|
|
||||||
local cachedVersions
|
|
||||||
cachedVersions="/etc/pihole/versions"
|
|
||||||
|
|
||||||
#If the above file exists, then we can read from that. Prevents overuse of GitHub API
|
|
||||||
if [[ -f "$cachedVersions" ]]; then
|
|
||||||
|
|
||||||
# shellcheck disable=SC1090
|
|
||||||
. "$cachedVersions"
|
|
||||||
|
|
||||||
case $daemon in
|
|
||||||
"pi-hole" ) echo "${GITHUB_CORE_VERSION}";;
|
"pi-hole" ) echo "${GITHUB_CORE_VERSION}";;
|
||||||
"AdminLTE" ) [[ "${INSTALL_WEB_INTERFACE}" == true ]] && echo "${GITHUB_WEB_VERSION}";;
|
"AdminLTE" ) [[ "${INSTALL_WEB_INTERFACE}" == true ]] && echo "${GITHUB_WEB_VERSION}";;
|
||||||
"FTL" ) echo "${GITHUB_FTL_VERSION}";;
|
"FTL" ) echo "${GITHUB_FTL_VERSION}";;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
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
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
getLocalBranch(){
|
getLocalBranch(){
|
||||||
# Get the checked out branch of the local directory
|
case ${1} in
|
||||||
local directory="${1}"
|
"pi-hole" ) echo "${CORE_BRANCH}";;
|
||||||
local branch
|
"AdminLTE" ) [[ "${INSTALL_WEB_INTERFACE}" == true ]] && echo "${WEB_BRANCH}";;
|
||||||
|
"FTL" ) echo "${FTL_BRANCH}";;
|
||||||
# Local FTL btranch is stored in /etc/pihole/ftlbranch
|
esac
|
||||||
if [[ "$1" == "FTL" ]]; then
|
|
||||||
branch="$(pihole-FTL branch)"
|
|
||||||
else
|
|
||||||
cd "${directory}" 2> /dev/null || { echo "${DEFAULT}"; return 1; }
|
|
||||||
branch=$(git rev-parse --abbrev-ref HEAD || echo "$DEFAULT")
|
|
||||||
fi
|
|
||||||
if [[ ! "${branch}" =~ ^v ]]; then
|
|
||||||
if [[ "${branch}" == "master" ]]; then
|
|
||||||
echo ""
|
|
||||||
elif [[ "${branch}" == "HEAD" ]]; then
|
|
||||||
echo "in detached HEAD state at "
|
|
||||||
else
|
|
||||||
echo "${branch} "
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
# Branch started in "v"
|
|
||||||
echo "release "
|
|
||||||
fi
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,30 +74,26 @@ versionOutput() {
|
|||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[[ "$1" == "pi-hole" ]] && GITDIR=$COREGITDIR
|
[[ "$2" == "-c" ]] || [[ "$2" == "--current" ]] || [[ -z "$2" ]] && current=$(getLocalVersion "${1}") && branch=$(getLocalBranch "${1}")
|
||||||
[[ "$1" == "AdminLTE" ]] && GITDIR=$WEBGITDIR
|
[[ "$2" == "-l" ]] || [[ "$2" == "--latest" ]] || [[ -z "$2" ]] && latest=$(getRemoteVersion "${1}")
|
||||||
[[ "$1" == "FTL" ]] && GITDIR="FTL"
|
|
||||||
|
|
||||||
[[ "$2" == "-c" ]] || [[ "$2" == "--current" ]] || [[ -z "$2" ]] && current=$(getLocalVersion $GITDIR) && branch=$(getLocalBranch $GITDIR)
|
|
||||||
[[ "$2" == "-l" ]] || [[ "$2" == "--latest" ]] || [[ -z "$2" ]] && latest=$(getRemoteVersion "$1")
|
|
||||||
if [[ "$2" == "-h" ]] || [[ "$2" == "--hash" ]]; then
|
if [[ "$2" == "-h" ]] || [[ "$2" == "--hash" ]]; then
|
||||||
[[ "$3" == "-c" ]] || [[ "$3" == "--current" ]] || [[ -z "$3" ]] && curHash=$(getLocalHash "$GITDIR") && branch=$(getLocalBranch $GITDIR)
|
[[ "$3" == "-c" ]] || [[ "$3" == "--current" ]] || [[ -z "$3" ]] && curHash=$(getLocalHash "${1}") && branch=$(getLocalBranch "${1}")
|
||||||
[[ "$3" == "-l" ]] || [[ "$3" == "--latest" ]] || [[ -z "$3" ]] && latHash=$(getRemoteHash "$1" "$(cd "$GITDIR" 2> /dev/null && git rev-parse --abbrev-ref HEAD)")
|
[[ "$3" == "-l" ]] || [[ "$3" == "--latest" ]] || [[ -z "$3" ]] && latHash=$(getRemoteHash "${1}")
|
||||||
fi
|
fi
|
||||||
if [[ -n "$current" ]] && [[ -n "$latest" ]]; then
|
if [[ -n "$current" ]] && [[ -n "$latest" ]]; then
|
||||||
output="${1^} version is $branch $current (Latest: $latest)"
|
output="${1^} version is $branch $current (Latest: $latest)"
|
||||||
elif [[ -n "$current" ]] && [[ -z "$latest" ]]; then
|
elif [[ -n "$current" ]] && [[ -z "$latest" ]]; then
|
||||||
output="Current ${1^} version is $branch$current"
|
output="Current ${1^} version is $branch $current (Latest: N/A)"
|
||||||
elif [[ -z "$current" ]] && [[ -n "$latest" ]]; then
|
elif [[ -z "$current" ]] && [[ -n "$latest" ]]; then
|
||||||
output="Latest ${1^} version is $latest"
|
output="Latest ${1^} version is $latest (Current: N/A)"
|
||||||
elif [[ "$curHash" == "N/A" ]] || [[ "$latHash" == "N/A" ]]; then
|
elif [[ -z "$curHash" ]] && [[ -z "$latHash" ]]; then
|
||||||
output="${1^} hash is not applicable"
|
output="No hash info available"
|
||||||
elif [[ -n "$curHash" ]] && [[ -n "$latHash" ]]; then
|
elif [[ -n "$curHash" ]] && [[ -n "$latHash" ]]; then
|
||||||
output="${1^} hash is $curHash (Latest: $latHash)"
|
output="Local ${1^} hash of branch $branch is $curHash (Remote: $latHash)"
|
||||||
elif [[ -n "$curHash" ]] && [[ -z "$latHash" ]]; then
|
elif [[ -n "$curHash" ]] && [[ -z "$latHash" ]]; then
|
||||||
output="Current ${1^} hash is $curHash"
|
output="Current local ${1^} hash of branch $branch is $curHash (Remote: N/A)"
|
||||||
elif [[ -z "$curHash" ]] && [[ -n "$latHash" ]]; then
|
elif [[ -z "$curHash" ]] && [[ -n "$latHash" ]]; then
|
||||||
output="Latest ${1^} hash is $latHash"
|
output="Latest remote ${1^} hash of branch $branch is $latHash (Local: N/A)"
|
||||||
else
|
else
|
||||||
errorOutput
|
errorOutput
|
||||||
return 1
|
return 1
|
||||||
|
Loading…
Reference in New Issue
Block a user