diff --git a/data/helpers.d/mysql b/data/helpers.d/mysql index 1dad94e13..39f93891c 100644 --- a/data/helpers.d/mysql +++ b/data/helpers.d/mysql @@ -237,24 +237,3 @@ ynh_mysql_remove_db () { fi } -# Sanitize a string intended to be the name of a database -# (More specifically : replace - and . by _) -# -# example: dbname=$(ynh_sanitize_dbid $app) -# -# usage: ynh_sanitize_dbid --db_name=name -# | arg: -n, --db_name - name to correct/sanitize -# | ret: the corrected name -# -# Requires YunoHost version 2.2.4 or higher. -ynh_sanitize_dbid () { - # Declare an array to define the options of this helper. - local legacy_args=n - declare -Ar args_array=( [n]=db_name= ) - local db_name - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - # We should avoid having - and . in the name of databases. They are replaced by _ - echo ${db_name//[-.]/_} -} diff --git a/data/helpers.d/network b/data/helpers.d/network index d2a34f8d7..0f75cb165 100644 --- a/data/helpers.d/network +++ b/data/helpers.d/network @@ -1,39 +1,5 @@ #!/bin/bash -# Normalize the url path syntax -# -# Handle the slash at the beginning of path and its absence at ending -# Return a normalized url path -# -# examples: -# 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_url=path_to_normalize -# | arg: -p, --path_url - URL path to normalize before using it -# -# Requires YunoHost version 2.6.4 or higher. -ynh_normalize_url_path () { - # Declare an array to define the options of this helper. - local legacy_args=p - declare -Ar args_array=( [p]=path_url= ) - local path_url - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - test -n "$path_url" || ynh_die --message="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 -} - # Find a free port and return it # # example: port=$(ynh_find_port --port=8080) diff --git a/data/helpers.d/string b/data/helpers.d/string index 52eede872..fcbc5190d 100644 --- a/data/helpers.d/string +++ b/data/helpers.d/string @@ -83,3 +83,59 @@ ynh_replace_special_string () { ynh_replace_string --match_string="$match_string" --replace_string="$replace_string" --target_file="$target_file" } + +# Sanitize a string intended to be the name of a database +# (More specifically : replace - and . by _) +# +# example: dbname=$(ynh_sanitize_dbid $app) +# +# usage: ynh_sanitize_dbid --db_name=name +# | arg: -n, --db_name - name to correct/sanitize +# | ret: the corrected name +# +# Requires YunoHost version 2.2.4 or higher. +ynh_sanitize_dbid () { + # Declare an array to define the options of this helper. + local legacy_args=n + declare -Ar args_array=( [n]=db_name= ) + local db_name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + # We should avoid having - and . in the name of databases. They are replaced by _ + echo ${db_name//[-.]/_} +} + +# Normalize the url path syntax +# +# Handle the slash at the beginning of path and its absence at ending +# Return a normalized url path +# +# examples: +# 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_url=path_to_normalize +# | arg: -p, --path_url - URL path to normalize before using it +# +# Requires YunoHost version 2.6.4 or higher. +ynh_normalize_url_path () { + # Declare an array to define the options of this helper. + local legacy_args=p + declare -Ar args_array=( [p]=path_url= ) + local path_url + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + test -n "$path_url" || ynh_die --message="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 +}