From 7de345fd3bcaf144f6d43a7e5403d4fe8ce5cfc7 Mon Sep 17 00:00:00 2001 From: magikcypress Date: Wed, 31 May 2017 14:12:15 +0200 Subject: [PATCH] [fix] add helper ynh_normalize_url_path --- scripts/_common | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/scripts/_common b/scripts/_common index 734d05a..437774e 100644 --- a/scripts/_common +++ b/scripts/_common @@ -290,4 +290,28 @@ ynh_exit_properly () { ynh_abort_if_errors () { set -eu # Exit if a command fail, and if a variable is used unset. trap ynh_exit_properly EXIT # Capturing exit signals on shell script +} + +# Normalize the url path syntax +# Handle the slash at the beginning of path and its absence at ending +# Return a normalized url path +# +# example: url_path=$(ynh_normalize_url_path $url_path) +# ynh_normalize_url_path example -> /example +# ynh_normalize_url_path /example -> /example +# ynh_normalize_url_path /example/ -> /example +# ynh_normalize_url_path / -> / +# +# usage: ynh_normalize_url_path path_to_normalize +# | arg: url_path_to_normalize - URL path to normalize before using it +ynh_normalize_url_path () { + path_url=$1 + test -n "$path_url" || ynh_die "ynh_normalize_url_path expect a URL path as first argument and received nothing." + if [ "${path_url:0:1}" != "/" ]; then # If the first character is not a / + path_url="/$path_url" # Add / at begin of path variable + fi + if [ "${path_url:${#path_url}-1}" == "/" ] && [ ${#path_url} -gt 1 ]; then # If the last character is a / and that not the only character. + path_url="${path_url:0:${#path_url}-1}" # Delete the last character + fi + echo $path_url } \ No newline at end of file