#!/usr/bin/env bash # # Copyright (c) 2024 YunoHost Contributors # # This file is part of YunoHost (see https://yunohost.org) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # # Generate a random string # # usage: ynh_string_random [--length=string_length] # | arg: --length= - the string length to generate (default: 24) # | arg: --filter= - the kind of characters accepted in the output (default: 'A-Za-z0-9') # | ret: the generated string # # example: pwd=$(ynh_string_random --length=8) ynh_string_random() { # ============ Argument parsing ============= local -A args_array=([l]=length= [f]=filter=) local length local filter ynh_handle_getopts_args "$@" length=${length:-24} filter=${filter:-'A-Za-z0-9'} # =========================================== tr --complement --delete "$filter" < /dev/urandom | head -c "$length" } # Substitute/replace a string (or expression) by another in a file # # usage: ynh_replace --match=match --replace=replace --file=file # | arg: --match= - String to be searched and replaced in the file # | arg: --replace= - String that will replace matches # | arg: --file= - File in which the string will be replaced. # # As this helper is based on sed command, regular expressions and references to # sub-expressions can be used (see sed manual page for more information). In particular # for back-references, as in sed without specific options, you will need to escape # the parentheses of the capture group. ynh_replace() { # ============ Argument parsing ============= local -A args_array=([m]=match= [r]=replace= [f]=file=) local match local replace local file ynh_handle_getopts_args "$@" # =========================================== set +o xtrace # set +x local delimit=$'\001' # Escape the delimiter if it's in the string. match=${match//${delimit}/"\\${delimit}"} replace=${replace//${delimit}/"\\${delimit}"} set -o xtrace # set -x sed --in-place "s${delimit}${match}${delimit}${replace}${delimit}g" "$file" } # Substitute/replace a regex in a file # # usage: ynh_replace_regex --match=match --replace=replace --file=file # | arg: --match= - String to be searched and replaced in the file # | arg: --replace= - String that will replace matches # | arg: --file= - File in which the string will be replaced. # # This helper will use ynh_replace, but as you can use special # characters, you can't use some regular expressions and sub-expressions. ynh_replace_regex() { # ============ Argument parsing ============= local -A args_array=([m]=match= [r]=replace= [f]=file=) local match local replace local file ynh_handle_getopts_args "$@" # =========================================== # Escape any backslash to preserve them as simple backslash. match=${match//\\/"\\\\"} replace=${replace//\\/"\\\\"} # Escape the & character, who has a special function in sed. match=${match//&/"\&"} replace=${replace//&/"\&"} ynh_replace --match="$match" --replace="$replace" --file="$file" } # Sanitize a string intended to be the name of a database # # [packagingv1] # # usage: ynh_sanitize_dbid --db_name=name # | arg: --db_name= - name to correct/sanitize # | ret: the corrected name # # example: dbname=$(ynh_sanitize_dbid $app) # # Underscorify the string (replace - and . by _) ynh_sanitize_dbid() { # ============ Argument parsing ============= local -A args_array=([n]=db_name=) local db_name 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_to_normalize ynh_normalize_url_path() { local 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" }