Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 33 additions & 35 deletions scripts/binary-validation
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
#!/bin/bash

# binary-validator.sh v1.1.0
# binary-validator v1.2.0
#
# Authored by:
# github.com/cmckee786
# github.com/Kolkhis
#
# Targeting mdBook version v0.4.52 as of Aug, 2025
# Only requests 5 latest release records, remove ?per_page=5 to
# fetch all from logic to request all records
# Only requests 5 latest release records, remove ?per_page=5
# from API request (json-setup()) to fetch all records
#
# Requires mdBook semantic version input from command line
# For example 'binary-validation.sh v0.1.15'

set -eo pipefail
# For example 'binary-validation 0.1.15'

declare MDBOOK_VERSION
declare SKIP_DIGEST=0
Expand All @@ -35,19 +33,19 @@ usage() {


USAGE
./binary-validation.sh \$version [options] [-s][-i][-h]
./binary-validation \$version [options] [-s][-i][-h]

OPTIONS
-s Skips GitHub API sha256 digest check logic, usually used with a non-interactive call of
the script when it is known the version does not support the digest check. Practical for
testing or debug purposes.

-i Should be used when the mdBook version has an unknown/null digest check value. This option will
prompt the user whether to continue the download if it does not detect the sha digest. Practical
for local dev purposes.
-i Should be used to test whether the mdBook version has an unknown/null digest check value.
This option will prompt the user whether to continue the download. Practical for debug or
local dev purposes.

-h Display this help message.
"
\n"
}

while getopts "sih" option; do
Expand All @@ -56,37 +54,38 @@ while getopts "sih" option; do
i) INTERACTIVE=1 ;;
h)
usage
exit;;
exit
;;
\?)
printf >&2 "Unknown argument!\n./binary-validation.sh \$version [options] [-s][-i][-h]"
printf >&2 "Unknown argument!\n./binary-validation \$version [options] [-s][-i][-h]\n"
exit 1
;;
esac
done

if [ "$OPTIND" -gt "$#" ]; then
echo "Missing mdBook version!"
printf "Missing mdBook version!\n"
exit 1
fi

shift $(($OPTIND - 1))
shift $((OPTIND - 1))
if [[ -n "$1" && "$1" =~ $reg_pattern ]]; then
MDBOOK_VERSION="$1"
elif [[ ! "$1" =~ $reg_pattern ]]; then
printf >&2 "Unrecognized mdBook version pattern, script accepts semantic version for example: 0.4.52"
printf >&2 "Unrecognized mdBook version pattern, script accepts semantic version for example: 0.4.52\n"
exit 1
fi

# If the mdbook version is pre-digest version (<0.4.52), ask for user validation
interactive-download() {
# If the mdbook version is pre-digest API version (<0.4.52), will ask for user validation
interactive() {
if [[ $INTERACTIVE -gt 0 ]]; then
local -l choice
until [[ $choice =~ ^[Yy|nN]$ ]]; do
read -r -p "Do you want to continue [y/N]? " choice
choice=${choice:-N}
case $choice in
y*)
printf "Proceeding with download.\n"
printf "Proceeding with extraction...\n"
EXTRACT=1
;;
n*)
Expand All @@ -97,9 +96,6 @@ interactive-download() {
;;
esac
done
else
printf "Non-interactive download, proceeding...\n"
return 0
fi
}

Expand All @@ -123,7 +119,7 @@ json_setup() {
jq "$jquery")" ||
{
printf "Something went wrong with the GH API request. Consider adding set -x flag.\n" 1>&2
return 1
exit 1
}

# TODO Implement GH TOKEN in the future?
Expand All @@ -144,10 +140,11 @@ binary_fetch() {
printf "\n"
else
printf "\nThe processed JSON record appears to be empty, queried version may not exist...\nExiting..."
return 1
exit 1
fi
}

# Designed such that any cases that function return 1 will still remove tar.gz if it was downloaded
validation-decision() {
local api_digest
local zip_digest
Expand All @@ -160,18 +157,18 @@ validation-decision() {
printf "%2s %s\n" "ZIP:" "$zip" "API_DIGEST:" "$api_digest"
printf "ZIP_DIGEST: %s\n\n" "$zip_digest"

if [[ $SKIP_DIGEST -eq 1 ]]; then
if [[ $SKIP_DIGEST == 1 ]]; then
EXTRACT=1
printf "Skipping digest check, fetching binary...\n"
elif [[ "$api_digest" == 'null' && $INTERACTIVE -eq 1 ]]; then
printf "Skipping digest check, extracting binary...\n"
elif [[ "$api_digest" == 'null' && $INTERACTIVE == 1 ]]; then
printf "This version of mdBook (%s) pre-dates the SHA digest feature of the GitHub API and cannot be validated.\n" "$MDBOOK_VERSION"
interactive-download || {
printf >&2 "Could not proceed with download.\n"
interactive || {
printf >&2 "Interactive function failed, could not proceed with extraction.\n"
}
elif [[ "$api_digest" == "$zip_digest" && $INTERACTIVE -eq 1 ]]; then
elif [[ "$api_digest" == "$zip_digest" && $INTERACTIVE == 1 ]]; then
printf "The mdBook binary has been successfully validated.\n"
interactive-download || {
printf >&2 "Could not proceed with download.\n"
interactive || {
printf >&2 "Interactive function failed, could not proceed with extraction.\n"
}
elif [[ "$api_digest" == 'null' ]]; then
printf "Digest could not be gathered from the API, likely an older version (<0.4.52).\nExiting..."
Expand All @@ -185,9 +182,11 @@ validation-decision() {
echo "$json" | jq '.'
fi

if [[ $EXTRACT -eq 1 ]]; then
if [[ $EXTRACT == 1 && -e "$zip" ]]; then
tar xfz "$zip"
printf "Extracting binary for use...\nBinary is ready for use!"
printf "Extracting binary for use...\nBinary is ready for use!\n"
else
printf "mdBook tar not found...\nCleaning up...\nExiting...\n"
fi
[[ -e $zip ]] && rm -f "$zip"
}
Expand All @@ -201,4 +200,3 @@ if [[ -n "$MDBOOK_VERSION" ]]; then
else
printf >&2 "MDBOOK_VERSION appears to be empty! Script requires a semantic version, for example: 0.4.52\n"
fi