Compare commits

...

1 Commits

Author SHA1 Message Date
wpetit f8fd8e102b feat(gitea): add script to facilitate package upload on gitea 2023-08-22 18:46:48 -06:00
1 changed files with 133 additions and 0 deletions

View File

@ -0,0 +1,133 @@
#!/bin/bash
set -eo pipefail
GITEA_PACKAGE_BASE_URL=${GITEA_BASE_URL:-https://forge.cadoles.com}
GITEA_PACKAGE_USERNAME=${GITEA_PACKAGE_USERNAME}
GITEA_PACKAGE_PASSWORD=${GITEA_PACKAGE_PASSWORD}
GITEA_PACKAGE_OWNER=${GITEA_PACKAGE_OWNER}
GITEA_PACKAGE_FILES=${GITEA_PACKAGE_FILES}
GITEA_PACKAGE_CURL_MAX_RETRY=${GITEA_PACKAGE_CURL_MAX_RETRY:-3}
GITEA_PACKAGE_TYPE=${GITEA_PACKAGE_TYPE:-generic}
function check_dependencies {
assert_command_available 'curl'
assert_command_available 'jq'
}
function assert_command_available {
local command=$1
local command_path=$(which $command)
if [ -z "$command_path" ]; then
echo "The '$command' command could not be found. Please install it before using this script." 1>&2
exit 1
fi
}
function check_environment {
assert_environment GITEA_PACKAGE_BASE_URL
assert_environment GITEA_PACKAGE_FILES
assert_environment GITEA_PACKAGE_OWNER
}
function source_env_file {
if [ ! -f '.env' ]; then
return 0
fi
set -o allexport
source .env
set +o allexport
}
function assert_environment {
local name=$1
local value=${!name}
if [ -z "$value" ]; then
echo "The $"$name" environment variable is empty." 1>&2
exit 1
fi
}
function ask_credentials {
if [ -z "$GITEA_PACKAGE_USERNAME" ]; then
echo -n "Username: "
read GITEA_PACKAGE_USERNAME
fi
if [ -z "$GITEA_PACKAGE_PASSWORD" ]; then
echo -n "Password: "
stty -echo
read GITEA_PACKAGE_PASSWORD
stty echo
echo
fi
}
function upload_package {
echo "Uploading package(s)..."
case "$GITEA_PACKAGE_TYPE" in
generic)
upload_generic_package
;;
alpine)
upload_alpine_package
;;
*)
echo "Unknown package type '$GITEA_PACKAGE_TYPE'. Please check $"$GITEA_PACKAGE_TYPE" environment variable value." 1>&2
exit 1
esac
echo "Done"
}
function upload_generic_package {
assert_environment GITEA_PACKAGE_GENERIC_PACKAGE_NAME
assert_environment GITEA_PACKAGE_GENERIC_PACKAGE_VERSION
for f in $GITEA_PACKAGE_FILES; do
local filename=$(basename $f)
echo "Uploading file '$f' as generic package $GITEA_PACKAGE_OWNER/$GITEA_PACKAGE_GENERIC_PACKAGE_NAME/$GITEA_PACKAGE_GENERIC_PACKAGE_VERSION..."
gitea_api "/api/packages/$GITEA_PACKAGE_OWNER/generic/$GITEA_PACKAGE_GENERIC_PACKAGE_NAME/$GITEA_PACKAGE_GENERIC_PACKAGE_VERSION/$filename" \
-X PUT \
--upload-file "$f"
done
}
function upload_alpine_package {
assert_environment GITEA_PACKAGE_ALPINE_PACKAGE_BRANCH
assert_environment GITEA_PACKAGE_ALPINE_PACKAGE_REPOSITORY
for f in $GITEA_PACKAGE_FILES; do
local filename=$(basename $f)
echo "Uploading file '$f' as alpine package $GITEA_PACKAGE_OWNER/$GITEA_PACKAGE_ALPINE_PACKAGE_BRANCH/$GITEA_PACKAGE_ALPINE_PACKAGE_REPOSITORY..."
gitea_api "/api/packages/$GITEA_PACKAGE_OWNER/alpine/$GITEA_PACKAGE_ALPINE_PACKAGE_BRANCH/$GITEA_PACKAGE_ALPINE_PACKAGE_REPOSITORY" \
-X PUT \
--upload-file "$f"
done
}
function gitea_api {
local path=$1
local args=${@:2}
curl -L \
--fail \
--ipv4 \
--progress-bar \
--retry "$GITEA_PACKAGE_CURL_MAX_RETRY" \
-u "$GITEA_PACKAGE_USERNAME:$GITEA_PACKAGE_PASSWORD" \
$GITEA_PACKAGE_CURL_ARGS \
${args} \
"$GITEA_PACKAGE_BASE_URL$path"
}
function main {
check_dependencies
source_env_file
check_environment
ask_credentials
upload_package
}
main