process query-string paramenters in db urls
This commit is contained in:
@@ -5,36 +5,56 @@
|
||||
#
|
||||
|
||||
DB_URL="$1";
|
||||
DB_VAR_PREFIX="${DB_VAR_PREFIX:-DB_}";
|
||||
DB_ARG_VAR_PREFIX="${DB_ARG_VAR_PREFIX:-db_arg_}";
|
||||
|
||||
SCHEMA="${DB_URL%//*}";
|
||||
SCHEMALESS_URL=${DB_URL#*//};
|
||||
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);
|
||||
# get URL's query args
|
||||
query_args="${DB_URL#*\?}";
|
||||
queryless_url="${DB_URL%%\?*}";
|
||||
|
||||
# get URL's schema
|
||||
schema="${queryless_url%//*}";
|
||||
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
|
||||
RIGHT_HAND=${SCHEMALESS_URL#*@};
|
||||
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
|
||||
if test -n "$(echo ${RIGHT_HAND} | grep -o '/')"; then
|
||||
DATABASE=$(echo ${RIGHT_HAND} | cut -d ':' -f 2 | cut -d '/' -f 2);
|
||||
fi
|
||||
|
||||
# throw an error if DB_URL does not contains at least a HOST
|
||||
if test -z "${HOST}"; then
|
||||
# the right hand is the host part (after @) and will be always present
|
||||
right_hand=${schemaless_url#*@};
|
||||
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
|
||||
if test -n "$(echo ${right_hand} | grep -o '/')"; then
|
||||
database=$(echo ${right_hand} | cut -d ':' -f 2 | cut -d '/' -f 2);
|
||||
fi
|
||||
|
||||
# throw an error if $DB_URL is empty and so is $host
|
||||
if test -z "${host}"; then
|
||||
echo "database URL providen was invalid" && exit 1;
|
||||
fi
|
||||
|
||||
cat << HEREDOC
|
||||
DB_SCHEMA=${SCHEMA%:}
|
||||
DB_USERNAME=${USERNAME}
|
||||
DB_PASSWORD=${PASSWORD}
|
||||
DB_HOST=${HOST}
|
||||
DB_PORT=${PORT}
|
||||
DB_NAME=${DATABASE}
|
||||
${DB_VAR_PREFIX}SCHEMA=${schema%:}
|
||||
${DB_VAR_PREFIX}USERNAME=${username}
|
||||
${DB_VAR_PREFIX}PASSWORD=${password}
|
||||
${DB_VAR_PREFIX}HOST=${host}
|
||||
${DB_VAR_PREFIX}PORT=${port}
|
||||
${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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user