process query-string paramenters in db urls
This commit is contained in:
@@ -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);
|
|
||||||
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
|
# the right hand is the host part (after @) and will be always present
|
||||||
if test -z "${HOST}"; then
|
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;
|
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
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user