Skip to content

Commit

Permalink
Improved error handling, dependency checks, and code readability (#136)
Browse files Browse the repository at this point in the history
* Improved error handling, dependency checks, and code readability

* error handling
  • Loading branch information
Adriel007 authored Apr 16, 2024
1 parent f201b63 commit 2fa5819
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions build_deb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,41 @@

set -e

# Function to display error message and exit
error_exit() {
echo "Error: $1" >&2
exit 1
}

# Sanitize package signing options
COUNT=0
for sign_param in DEB_SIGN_KEY DEB_SIGN_KEY_ID DEB_SIGN_KEY_PATH; do
if [[ -n "${!sign_param}" ]]; then ((COUNT+=1)); fi
done
if (( COUNT > 1 )); then
echo "Error: At most one of DEB_SIGN_KEY or DEB_SIGN_KEY_ID or DEB_SIGN_KEY_PATH vars must be defined " >&2
exit 1
error_exit "At most one of DEB_SIGN_KEY or DEB_SIGN_KEY_ID or DEB_SIGN_KEY_PATH vars must be defined"
fi

# Import GPG signing private key if it is provided
if [[ -n "${DEB_SIGN_KEY_ID}" ]]; then
# Check if gpg knows about this key id
if [[ $(gpg --list-keys ${DEB_SIGN_KEY_ID} 2>&1) =~ "No public key" ]]; then
echo "Error: No public key ${DEB_SIGN_KEY_ID}" >&2
exit 1
error_exit "No public key ${DEB_SIGN_KEY_ID}"
else
SIGN_ARGS="-k${DEB_SIGN_KEY_ID}"
fi
elif [[ -n "${DEB_SIGN_KEY}" ]]; then
echo "${DEB_SIGN_KEY}" | gpg --import
echo "${DEB_SIGN_KEY}" | gpg --import || error_exit "Unable to import signing key from var DEB_SIGN_KEY"
KEY_ID=$(gpg --list-keys --with-colon | awk -F: '/^fpr/ {print $10;exit}')
if [[ -z ${KEY_ID} ]]; then
echo "Error: Unable to import signing key from var DEB_SIGN_KEY" >&2
exit 1
error_exit "Unable to import signing key from var DEB_SIGN_KEY"
fi
SIGN_ARGS="-k${KEY_ID}"
elif [[ -n "${DEB_SIGN_KEY_PATH}" ]]; then
gpg --import --with-colons "${DEB_SIGN_KEY_PATH}"
gpg --import --with-colons "${DEB_SIGN_KEY_PATH}" || error_exit "Unable to import signing key from path: ${DEB_SIGN_KEY_PATH}"
KEY_ID=$(gpg --list-keys --with-colon | awk -F: '/^fpr/ {print $10;exit}')
if [[ -z ${KEY_ID} ]]; then
echo "Error: Unable to import signing key from path: ${DEB_SIGN_KEY_PATH}" >&2
exit 1
error_exit "Unable to import signing key from path: ${DEB_SIGN_KEY_PATH}"
fi
SIGN_ARGS="-k${KEY_ID}"
else
Expand Down

0 comments on commit 2fa5819

Please sign in to comment.