process query-string paramenters in db urls

This commit is contained in:
2024-10-22 10:48:18 -03:00
parent 336278ddb3
commit 9382422ee7

View File

@@ -5,36 +5,56 @@
# #
DB_URL="$1"; DB_URL="$1";
DB_VAR_PREFIX="${DB_VAR_PREFIX:-DB_}";
DB_ARG_VAR_PREFIX="${DB_ARG_VAR_PREFIX:-db_arg_}";
SCHEMA="${DB_URL%//*}"; # get URL's query args
SCHEMALESS_URL=${DB_URL#*//}; query_args="${DB_URL#*\?}";
LEFT_HAND=${SCHEMALESS_URL%@*}; queryless_url="${DB_URL%%\?*}";
if test ! ${LEFT_HAND} = ${SCHEMALESS_URL}; then
USERNAME=$(echo ${LEFT_HAND} | cut -d ':' -f 1); # get URL's schema
if test ! ${USERNAME} = ${LEFT_HAND}; then schema="${queryless_url%//*}";
PASSWORD=$(echo ${LEFT_HAND} | cut -d ':' -f 2); schemaless_url=${queryless_url#*//};
# left hand is everything until the first @ (the user part)
# with no @ there's no difference from $schemaless_url
# so $username and $password will be blank
left_hand=${schemaless_url%@*};
if test ! ${left_hand} = ${schemaless_url}; then
username=$(echo ${left_hand} | cut -d ':' -f 1);
if test ! ${username} = ${left_hand}; then
password=$(echo ${left_hand} | cut -d ':' -f 2);
fi fi
fi fi
RIGHT_HAND=${SCHEMALESS_URL#*@};
HOST=$(echo ${RIGHT_HAND} | cut -d ':' -f 1 | cut -d '/' -f 1); # the right hand is the host part (after @) and will be always present
if test -n "$(echo ${RIGHT_HAND} | grep -o ':')"; then right_hand=${schemaless_url#*@};
PORT=$(echo ${RIGHT_HAND} | cut -d ':' -f 2 | cut -d '/' -f 1); host=$(echo ${right_hand} | cut -d ':' -f 1 | cut -d '/' -f 1);
if test -n "$(echo ${right_hand} | grep -o ':')"; then
port=$(echo ${right_hand} | cut -d ':' -f 2 | cut -d '/' -f 1);
fi fi
if test -n "$(echo ${RIGHT_HAND} | grep -o '/')"; then if test -n "$(echo ${right_hand} | grep -o '/')"; then
DATABASE=$(echo ${RIGHT_HAND} | cut -d ':' -f 2 | cut -d '/' -f 2); database=$(echo ${right_hand} | cut -d ':' -f 2 | cut -d '/' -f 2);
fi fi
# throw an error if DB_URL does not contains at least a HOST # throw an error if $DB_URL is empty and so is $host
if test -z "${HOST}"; then if test -z "${host}"; then
echo "database URL providen was invalid" && exit 1; echo "database URL providen was invalid" && exit 1;
fi fi
cat << HEREDOC cat << HEREDOC
DB_SCHEMA=${SCHEMA%:} ${DB_VAR_PREFIX}SCHEMA=${schema%:}
DB_USERNAME=${USERNAME} ${DB_VAR_PREFIX}USERNAME=${username}
DB_PASSWORD=${PASSWORD} ${DB_VAR_PREFIX}PASSWORD=${password}
DB_HOST=${HOST} ${DB_VAR_PREFIX}HOST=${host}
DB_PORT=${PORT} ${DB_VAR_PREFIX}PORT=${port}
DB_NAME=${DATABASE} ${DB_VAR_PREFIX}NAME=${database}
$(
echo ${query_args}\
| sed 's/[&;]/\n/g'\
| while read arg_item; do
echo "${DB_ARG_VAR_PREFIX}${arg_item}";
done
)
HEREDOC HEREDOC