Skip to content

Commit

Permalink
apt-utils.sh: get base-files package version from GHA
Browse files Browse the repository at this point in the history
  • Loading branch information
amazingfate committed Nov 14, 2024
1 parent 45a1a09 commit 346d625
Showing 1 changed file with 19 additions and 26 deletions.
45 changes: 19 additions & 26 deletions lib/functions/general/apt-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@
function apt_find_upstream_package_version_and_download_url() {
declare sought_package_name="${1}"

declare -a package_info_download_urls=()
declare first_letter_of_sought_package_name="${sought_package_name:0:1}"
declare mirror_with_slash="undetermined/"

case "${DISTRIBUTION}" in
Ubuntu)
# Only LTS releases have an "-updates" repo that is worth looking into
if [[ "${RELEASE}" == "focal" || "${RELEASE}" == "jammy" ]]; then # @TODO: release info information, is_ubuntu_release_lts() or similar
package_info_download_urls+=("https://packages.ubuntu.com/${RELEASE}-updates/${ARCH}/${sought_package_name}/download")
package_download_release=${RELEASE}-updates
else
package_info_download_urls+=("https://packages.ubuntu.com/${RELEASE}/${ARCH}/${sought_package_name}/download")
package_download_release=${RELEASE}
fi
mirror_with_slash="${UBUNTU_MIRROR}"
;;

Debian)
package_info_download_urls+=("https://packages.debian.org/${RELEASE}/${ARCH}/${sought_package_name}/download")
package_download_release=${RELEASE}
mirror_with_slash="${DEBIAN_MIRROR}"
;;

Expand All @@ -41,35 +40,29 @@ function apt_find_upstream_package_version_and_download_url() {

declare base_down_url="http://${mirror_with_slash}pool/main/${first_letter_of_sought_package_name}/${sought_package_name}"

declare index package_info_download_url
# loop over the package_info_download_urls with index and value
for index in "${!package_info_download_urls[@]}"; do
package_info_download_url="${package_info_download_urls[$index]}"
display_alert "Testing URL" "${package_info_download_url}" "debug"

declare package_info_download_url_file
package_info_download_url_file="$(mktemp)"
curl --silent --show-error --max-time 10 "${package_info_download_url}" > "${package_info_download_url_file}" || true # don't fail
declare package_info_download_url_file_package_name # grep the file for the package name. parse "<kbd>name</kbd>"
package_info_download_url_file_package_name="$(grep -oP '(?<=<kbd>)[^<]+' "${package_info_download_url_file}" | grep "^${sought_package_name}_" | head -n 1)"
rm -f "${package_info_download_url_file}"

display_alert "Package name parsed" "${package_info_download_url_file_package_name}" "debug"
if [[ "${package_info_download_url_file_package_name}" == "${sought_package_name}_"* ]]; then
found_package_filename="${package_info_download_url_file_package_name}"
found_package_down_url="${base_down_url}/${found_package_filename}"
display_alert "Found package filename" "${found_package_filename} in url ${package_info_download_url}" "debug"
break
fi
done
# get json with latest pacakge info generated by GHA
case "${GITHUB_MIRROR}" in
"ghproxy")
package_info_download_url="https://ghp.ci/https://raw.githubusercontent.com/amazingfate/deb-files-parser/refs/heads/master/${sought_package_name}.json"
;;
*)
package_info_download_url="https://raw.githubusercontent.com/amazingfate/deb-files-parser/refs/heads/master/${sought_package_name}.json"
;;
esac
package_info_download_url_file="$(mktemp)"
curl --silent --show-error --max-time 10 $package_info_download_url -o $package_info_download_url_file
found_package_filename=$(jq -r .[\"${package_download_release}\"].${ARCH} $package_info_download_url_file)

if [[ "${found_package_filename}" == "${sought_package_name}_"* ]]; then
display_alert "Found upstream base-files package filename" "${found_package_filename}" "info"
else
display_alert "Could not find package filename for '${sought_package_name}' in '${package_info_download_urls[*]}'" "looking for ${sought_package_name}" "warn"
display_alert "Could not find package filename for '${sought_package_name}' in distro repo" "looking for ${sought_package_name}, found_package_filename is ${found_package_filename}" "warn"
return 1
fi

found_package_down_url="${base_down_url}/${found_package_filename}"
display_alert "Found package filename" "${found_package_filename} in url ${found_package_down_url}" "debug"

# Now we have the package name, lets parse out the version.
found_package_version="$(echo "${found_package_filename}" | grep -oP '(?<=_)[^_]+(?=_)')"
display_alert "Found base-files upstream package version" "${found_package_version}" "info"
Expand Down

0 comments on commit 346d625

Please sign in to comment.