Skip to content

Commit

Permalink
Enhanced software release retrieval
Browse files Browse the repository at this point in the history
Updated:
- Web::upgradeRelease
- Web::getReleases
- Github::installRelease

Changes:
- support for gh
- allows to override version place holder using env variable VERSION_PLACEHOLDER
  • Loading branch information
fchastanet committed Jan 20, 2025
1 parent f27f9f9 commit 6e2670e
Show file tree
Hide file tree
Showing 14 changed files with 377 additions and 334 deletions.
46 changes: 33 additions & 13 deletions bin/doc
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ Github::upgradeRelease() {
}
FILTER_LAST_VERSION_CALLBACK=${FILTER_LAST_VERSION_CALLBACK:-extractVersion} \
SOFT_VERSION_CALLBACK="${softVersionCallback}" \
INSTALL_CALLBACK="${installCallback}" \
Web::upgradeRelease \
"${targetFile}" \
"${releasesUrl}" \
Expand Down Expand Up @@ -1544,12 +1545,27 @@ Version::parse() {
Web::getReleases() {
local releaseListUrl="$1"
# Get latest release from GitHub api
Retry::parameterized "${RETRY_MAX_RETRY:-5}" "${RETRY_DELAY_BETWEEN_RETRIES:-15}" "Retrieving release versions list ..." curl \
-L \
--connect-timeout "${CURL_CONNECT_TIMEOUT:-5}" \
--fail \
--silent \
"${releaseListUrl}"
if command -v gh &>/dev/null && [[ -n "${GH_TOKEN}" ]]; then
Log::displayDebug "Using gh to retrieve release versions list"
Retry::parameterized "${RETRY_MAX_RETRY:-5}" \
"${RETRY_DELAY_BETWEEN_RETRIES:-15}" \
"Retrieving release versions list ..." \
gh api "${releaseListUrl#https://github.com}"
else
if command -v gh &>/dev/null && [[ "${GH_WARNING_DISPLAYED:-0}" = "0" ]]; then
Log::displayWarning "GH_TOKEN is not set, cannot use gh, using curl to retrieve release versions list"
GH_WARNING_DISPLAYED=1
fi
Retry::parameterized "${RETRY_MAX_RETRY:-5}" \
"${RETRY_DELAY_BETWEEN_RETRIES:-15}" \
"Retrieving release versions list ..." \
curl \
-L \
--connect-timeout "${CURL_CONNECT_TIMEOUT:-5}" \
--fail \
--silent \
"${releaseListUrl}"
fi
}


Expand All @@ -1567,6 +1583,7 @@ Web::getReleases() {
# @env PARSE_VERSION_CALLBACK a callback to parse the version of the existing command
# @env INSTALL_CALLBACK a callback to install the software downloaded
# @env CURL_CONNECT_TIMEOUT number of seconds before giving up host connection
# @env VERSION_PLACEHOLDER a placeholder to replace in downloadReleaseUrl (default: @latestVersion@)
Web::upgradeRelease() {
local targetFile="$1"
local releasesUrl="$2"
Expand All @@ -1577,25 +1594,28 @@ Web::upgradeRelease() {
local filterLastVersionCallback="${FILTER_LAST_VERSION_CALLBACK:-Version::parse}"
local softVersionCallback="${SOFT_VERSION_CALLBACK:-Version::getCommandVersionFromPlainText}"
local installCallback="${INSTALL_CALLBACK:-}"
local latestVersion
latestVersion="$(Web::getReleases "${releasesUrl}" | ${filterLastVersionCallback})" || {
Log::displayError "latest version not found on ${releasesUrl}"
return 1
}
Log::displayInfo "Latest version found is ${latestVersion}"

local currentVersion="not existing"
if [[ -f "${targetFile}" ]]; then
currentVersion="$(${softVersionCallback} "${targetFile}" "${softVersionArg}" 2>&1 || true)"
fi
if [[ -z "${exactVersion}" ]]; then
local latestVersion
latestVersion="$(Web::getReleases "${releasesUrl}" | ${filterLastVersionCallback})" || {
Log::displayError "latest version not found on ${releasesUrl}"
return 1
}
Log::displayInfo "Latest version found is ${latestVersion}"

exactVersion="${latestVersion}"
fi
local url="${downloadReleaseUrl//@latestVersion@/${exactVersion}}"
local url="${downloadReleaseUrl//${VERSION_PLACEHOLDER:-@latestVersion@}/${exactVersion}}"
if [[ -n "${exactVersion}" ]] && ! Github::isReleaseVersionExist "${url}"; then
Log::displayError "${targetFile} version ${exactVersion} doesn't exist on github"
return 2
fi
Log::displayDebug "currentVersion: '${currentVersion}'"
Log::displayDebug "exactVersion: '${exactVersion}'"
if [[ "${currentVersion}" = "${exactVersion}" ]]; then
Log::displayInfo "${targetFile} version ${exactVersion} already installed"
else
Expand Down
68 changes: 55 additions & 13 deletions bin/dockerLint
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ Github::upgradeRelease() {
}
FILTER_LAST_VERSION_CALLBACK=${FILTER_LAST_VERSION_CALLBACK:-extractVersion} \
SOFT_VERSION_CALLBACK="${softVersionCallback}" \
INSTALL_CALLBACK="${installCallback}" \
Web::upgradeRelease \
"${targetFile}" \
"${releasesUrl}" \
Expand Down Expand Up @@ -612,6 +613,19 @@ Log::displaySuccess() {
}


# @description Display message using warning color (yellow)
# @arg $1 message:String the message to display
# @env DISPLAY_DURATION int (default 0) if 1 display elapsed time information between 2 info logs
# @env LOG_CONTEXT String allows to contextualize the log
Log::displayWarning() {
if ((BASH_FRAMEWORK_DISPLAY_LEVEL >= __LEVEL_WARNING)); then
Log::computeDuration
echo -e "${__WARNING_COLOR}WARN - ${LOG_CONTEXT:-}${LOG_LAST_DURATION_STR:-}${1}${__RESET_COLOR}" >&2
fi
Log::logWarning "$1"
}


# @description Display message using error color (red) and exit immediately with error status 1
# @arg $1 message:String the message to display
# @env DISPLAY_DURATION int (default 0) if 1 display elapsed time information between 2 info logs
Expand Down Expand Up @@ -704,6 +718,15 @@ Log::logSuccess() {
}


# @description log message to file
# @arg $1 message:String the message to display
Log::logWarning() {
if ((BASH_FRAMEWORK_LOG_LEVEL >= __LEVEL_WARNING)); then
Log::logMessage "${2:-WARNING}" "$1"
fi
}


# @description activate or not Log::display* and Log::log* functions
# based on BASH_FRAMEWORK_DISPLAY_LEVEL and BASH_FRAMEWORK_LOG_LEVEL
# environment variables loaded by Env::requireLoad
Expand Down Expand Up @@ -1074,12 +1097,27 @@ Version::parse() {
Web::getReleases() {
local releaseListUrl="$1"
# Get latest release from GitHub api
Retry::parameterized "${RETRY_MAX_RETRY:-5}" "${RETRY_DELAY_BETWEEN_RETRIES:-15}" "Retrieving release versions list ..." curl \
-L \
--connect-timeout "${CURL_CONNECT_TIMEOUT:-5}" \
--fail \
--silent \
"${releaseListUrl}"
if command -v gh &>/dev/null && [[ -n "${GH_TOKEN}" ]]; then
Log::displayDebug "Using gh to retrieve release versions list"
Retry::parameterized "${RETRY_MAX_RETRY:-5}" \
"${RETRY_DELAY_BETWEEN_RETRIES:-15}" \
"Retrieving release versions list ..." \
gh api "${releaseListUrl#https://github.com}"
else
if command -v gh &>/dev/null && [[ "${GH_WARNING_DISPLAYED:-0}" = "0" ]]; then
Log::displayWarning "GH_TOKEN is not set, cannot use gh, using curl to retrieve release versions list"
GH_WARNING_DISPLAYED=1
fi
Retry::parameterized "${RETRY_MAX_RETRY:-5}" \
"${RETRY_DELAY_BETWEEN_RETRIES:-15}" \
"Retrieving release versions list ..." \
curl \
-L \
--connect-timeout "${CURL_CONNECT_TIMEOUT:-5}" \
--fail \
--silent \
"${releaseListUrl}"
fi
}


Expand All @@ -1097,6 +1135,7 @@ Web::getReleases() {
# @env PARSE_VERSION_CALLBACK a callback to parse the version of the existing command
# @env INSTALL_CALLBACK a callback to install the software downloaded
# @env CURL_CONNECT_TIMEOUT number of seconds before giving up host connection
# @env VERSION_PLACEHOLDER a placeholder to replace in downloadReleaseUrl (default: @latestVersion@)
Web::upgradeRelease() {
local targetFile="$1"
local releasesUrl="$2"
Expand All @@ -1107,25 +1146,28 @@ Web::upgradeRelease() {
local filterLastVersionCallback="${FILTER_LAST_VERSION_CALLBACK:-Version::parse}"
local softVersionCallback="${SOFT_VERSION_CALLBACK:-Version::getCommandVersionFromPlainText}"
local installCallback="${INSTALL_CALLBACK:-}"
local latestVersion
latestVersion="$(Web::getReleases "${releasesUrl}" | ${filterLastVersionCallback})" || {
Log::displayError "latest version not found on ${releasesUrl}"
return 1
}
Log::displayInfo "Latest version found is ${latestVersion}"

local currentVersion="not existing"
if [[ -f "${targetFile}" ]]; then
currentVersion="$(${softVersionCallback} "${targetFile}" "${softVersionArg}" 2>&1 || true)"
fi
if [[ -z "${exactVersion}" ]]; then
local latestVersion
latestVersion="$(Web::getReleases "${releasesUrl}" | ${filterLastVersionCallback})" || {
Log::displayError "latest version not found on ${releasesUrl}"
return 1
}
Log::displayInfo "Latest version found is ${latestVersion}"

exactVersion="${latestVersion}"
fi
local url="${downloadReleaseUrl//@latestVersion@/${exactVersion}}"
local url="${downloadReleaseUrl//${VERSION_PLACEHOLDER:-@latestVersion@}/${exactVersion}}"
if [[ -n "${exactVersion}" ]] && ! Github::isReleaseVersionExist "${url}"; then
Log::displayError "${targetFile} version ${exactVersion} doesn't exist on github"
return 2
fi
Log::displayDebug "currentVersion: '${currentVersion}'"
Log::displayDebug "exactVersion: '${exactVersion}'"
if [[ "${currentVersion}" = "${exactVersion}" ]]; then
Log::displayInfo "${targetFile} version ${exactVersion} already installed"
else
Expand Down
66 changes: 33 additions & 33 deletions bin/installFacadeExample
Original file line number Diff line number Diff line change
Expand Up @@ -516,39 +516,39 @@ Log::logFatal() {
# FUNCTIONS

facade_main_installFacadeExamplesh() {
FRAMEWORK_ROOT_DIR="$(cd "${CURRENT_DIR}/.." && pwd -P)"
FRAMEWORK_SRC_DIR="${FRAMEWORK_ROOT_DIR}/src"
FRAMEWORK_BIN_DIR="${FRAMEWORK_ROOT_DIR}/bin"
FRAMEWORK_VENDOR_DIR="${FRAMEWORK_ROOT_DIR}/vendor"
FRAMEWORK_VENDOR_BIN_DIR="${FRAMEWORK_ROOT_DIR}/vendor/bin"
# REQUIRES
Env::requireLoad
Log::requireLoad
UI::requireTheme
Compiler::Facade::requireCommandBinDir

# @require Compiler::Facade::requireCommandBinDir

install() {
echo "installation in progress"
}

local action=$1
shift || true
case ${action} in
install)
install "$@"
;;
*)
if Assert::functionExists defaultFacadeAction; then
defaultFacadeAction "$1" "$@"
else
Log::displayError "invalid action requested: ${action}"
exit 1
fi
;;
esac
exit 0
FRAMEWORK_ROOT_DIR="$(cd "${CURRENT_DIR}/.." && pwd -P)"
FRAMEWORK_SRC_DIR="${FRAMEWORK_ROOT_DIR}/src"
FRAMEWORK_BIN_DIR="${FRAMEWORK_ROOT_DIR}/bin"
FRAMEWORK_VENDOR_DIR="${FRAMEWORK_ROOT_DIR}/vendor"
FRAMEWORK_VENDOR_BIN_DIR="${FRAMEWORK_ROOT_DIR}/vendor/bin"
# REQUIRES
Env::requireLoad
Log::requireLoad
UI::requireTheme
Compiler::Facade::requireCommandBinDir

# @require Compiler::Facade::requireCommandBinDir

install() {
echo "installation in progress"
}

local action=$1
shift || true
case ${action} in
install)
install "$@"
;;
*)
if Assert::functionExists defaultFacadeAction; then
defaultFacadeAction "$1" "$@"
else
Log::displayError "invalid action requested: ${action}"
exit 1
fi
;;
esac
exit 0
}

# if file is sourced avoid calling main function
Expand Down
46 changes: 33 additions & 13 deletions bin/installRequirements
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ Github::upgradeRelease() {
}
FILTER_LAST_VERSION_CALLBACK=${FILTER_LAST_VERSION_CALLBACK:-extractVersion} \
SOFT_VERSION_CALLBACK="${softVersionCallback}" \
INSTALL_CALLBACK="${installCallback}" \
Web::upgradeRelease \
"${targetFile}" \
"${releasesUrl}" \
Expand Down Expand Up @@ -1172,12 +1173,27 @@ Version::parse() {
Web::getReleases() {
local releaseListUrl="$1"
# Get latest release from GitHub api
Retry::parameterized "${RETRY_MAX_RETRY:-5}" "${RETRY_DELAY_BETWEEN_RETRIES:-15}" "Retrieving release versions list ..." curl \
-L \
--connect-timeout "${CURL_CONNECT_TIMEOUT:-5}" \
--fail \
--silent \
"${releaseListUrl}"
if command -v gh &>/dev/null && [[ -n "${GH_TOKEN}" ]]; then
Log::displayDebug "Using gh to retrieve release versions list"
Retry::parameterized "${RETRY_MAX_RETRY:-5}" \
"${RETRY_DELAY_BETWEEN_RETRIES:-15}" \
"Retrieving release versions list ..." \
gh api "${releaseListUrl#https://github.com}"
else
if command -v gh &>/dev/null && [[ "${GH_WARNING_DISPLAYED:-0}" = "0" ]]; then
Log::displayWarning "GH_TOKEN is not set, cannot use gh, using curl to retrieve release versions list"
GH_WARNING_DISPLAYED=1
fi
Retry::parameterized "${RETRY_MAX_RETRY:-5}" \
"${RETRY_DELAY_BETWEEN_RETRIES:-15}" \
"Retrieving release versions list ..." \
curl \
-L \
--connect-timeout "${CURL_CONNECT_TIMEOUT:-5}" \
--fail \
--silent \
"${releaseListUrl}"
fi
}


Expand All @@ -1195,6 +1211,7 @@ Web::getReleases() {
# @env PARSE_VERSION_CALLBACK a callback to parse the version of the existing command
# @env INSTALL_CALLBACK a callback to install the software downloaded
# @env CURL_CONNECT_TIMEOUT number of seconds before giving up host connection
# @env VERSION_PLACEHOLDER a placeholder to replace in downloadReleaseUrl (default: @latestVersion@)
Web::upgradeRelease() {
local targetFile="$1"
local releasesUrl="$2"
Expand All @@ -1205,25 +1222,28 @@ Web::upgradeRelease() {
local filterLastVersionCallback="${FILTER_LAST_VERSION_CALLBACK:-Version::parse}"
local softVersionCallback="${SOFT_VERSION_CALLBACK:-Version::getCommandVersionFromPlainText}"
local installCallback="${INSTALL_CALLBACK:-}"
local latestVersion
latestVersion="$(Web::getReleases "${releasesUrl}" | ${filterLastVersionCallback})" || {
Log::displayError "latest version not found on ${releasesUrl}"
return 1
}
Log::displayInfo "Latest version found is ${latestVersion}"

local currentVersion="not existing"
if [[ -f "${targetFile}" ]]; then
currentVersion="$(${softVersionCallback} "${targetFile}" "${softVersionArg}" 2>&1 || true)"
fi
if [[ -z "${exactVersion}" ]]; then
local latestVersion
latestVersion="$(Web::getReleases "${releasesUrl}" | ${filterLastVersionCallback})" || {
Log::displayError "latest version not found on ${releasesUrl}"
return 1
}
Log::displayInfo "Latest version found is ${latestVersion}"

exactVersion="${latestVersion}"
fi
local url="${downloadReleaseUrl//@latestVersion@/${exactVersion}}"
local url="${downloadReleaseUrl//${VERSION_PLACEHOLDER:-@latestVersion@}/${exactVersion}}"
if [[ -n "${exactVersion}" ]] && ! Github::isReleaseVersionExist "${url}"; then
Log::displayError "${targetFile} version ${exactVersion} doesn't exist on github"
return 2
fi
Log::displayDebug "currentVersion: '${currentVersion}'"
Log::displayDebug "exactVersion: '${exactVersion}'"
if [[ "${currentVersion}" = "${exactVersion}" ]]; then
Log::displayInfo "${targetFile} version ${exactVersion} already installed"
else
Expand Down
Loading

0 comments on commit 6e2670e

Please sign in to comment.