helpers2.1: simplify ynh_replace_string, just write ynh_replace --match=foo --replace=bar --file=/some/path

This commit is contained in:
Alexandre Aubin 2024-06-10 18:18:42 +02:00
parent 14ba98a232
commit 1b2d13f96a
4 changed files with 33 additions and 33 deletions

View file

@ -23,9 +23,9 @@ ynh_add_nginx_config() {
ynh_add_config --template="nginx.conf" --destination="$finalnginxconf"
if [ "${path:-}" != "/" ]; then
ynh_replace_string --match_string="^#sub_path_only" --replace_string="" --target_file="$finalnginxconf"
ynh_replace --match="^#sub_path_only" --replace="" --file="$finalnginxconf"
else
ynh_replace_string --match_string="^#root_path_only" --replace_string="" --target_file="$finalnginxconf"
ynh_replace --match="^#root_path_only" --replace="" --file="$finalnginxconf"
fi
ynh_store_file_checksum --file="$finalnginxconf"

View file

@ -111,7 +111,7 @@ ynh_install_nodejs() {
mkdir -p $n_install_dir/bin/
cp "$YNH_HELPERS_DIR/vendor/n/n" $n_install_dir/bin/n
# Tweak for n to understand it's installed in $N_PREFIX
ynh_replace_string --match_string="^N_PREFIX=\${N_PREFIX-.*}$" --replace_string="N_PREFIX=\${N_PREFIX-$N_PREFIX}" --target_file="$n_install_dir/bin/n"
ynh_replace --match="^N_PREFIX=\${N_PREFIX-.*}$" --replace="N_PREFIX=\${N_PREFIX-$N_PREFIX}" --file="$n_install_dir/bin/n"
# Restore /usr/local/bin in PATH
PATH=$CLEAR_PATH

View file

@ -27,63 +27,63 @@ ynh_string_random() {
# Substitute/replace a string (or expression) by another in a file
#
# usage: ynh_replace_string --match_string=match_string --replace_string=replace_string --target_file=target_file
# | arg: -m, --match_string= - String to be searched and replaced in the file
# | arg: -r, --replace_string= - String that will replace matches
# | arg: -f, --target_file= - File in which the string will be replaced.
# usage: ynh_replace --match=match --replace=replace --file=file
# | arg: -m, --match= - String to be searched and replaced in the file
# | arg: -r, --replace= - String that will replace matches
# | arg: -f, --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)
#
# Requires YunoHost version 2.6.4 or higher.
ynh_replace_string() {
ynh_replace() {
# ============ Argument parsing =============
local -A args_array=([m]=match_string= [r]=replace_string= [f]=target_file=)
local match_string
local replace_string
local target_file
local -A args_array=([m]=match= [r]=replaceg= [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_string=${match_string//${delimit}/"\\${delimit}"}
replace_string=${replace_string//${delimit}/"\\${delimit}"}
match=${match//${delimit}/"\\${delimit}"}
replace=${replace//${delimit}/"\\${delimit}"}
set -o xtrace # set -x
sed --in-place "s${delimit}${match_string}${delimit}${replace_string}${delimit}g" "$target_file"
sed --in-place "s${delimit}${match}${delimit}${replace}${delimit}g" "$file"
}
# Substitute/replace a special string by another in a file
#
# usage: ynh_replace_special_string --match_string=match_string --replace_string=replace_string --target_file=target_file
# | arg: -m, --match_string= - String to be searched and replaced in the file
# | arg: -r, --replace_string= - String that will replace matches
# | arg: -t, --target_file= - File in which the string will be replaced.
# usage: ynh_replace_special_string --match=match --replace=replace --file=file
# | arg: -m, --match= - String to be searched and replaced in the file
# | arg: -r, --replace= - String that will replace matches
# | arg: -f, --file= - File in which the string will be replaced.
#
# This helper will use ynh_replace_string, but as you can use special
# This helper will use ynh_replace, but as you can use special
# characters, you can't use some regular expressions and sub-expressions.
#
# Requires YunoHost version 2.7.7 or higher.
ynh_replace_special_string() {
# ============ Argument parsing =============
local -A args_array=([m]=match_string= [r]=replace_string= [f]=target_file=)
local match_string
local replace_string
local target_file
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_string=${match_string//\\/"\\\\"}
replace_string=${replace_string//\\/"\\\\"}
match=${match//\\/"\\\\"}
replace=${replace//\\/"\\\\"}
# Escape the & character, who has a special function in sed.
match_string=${match_string//&/"\&"}
replace_string=${replace_string//&/"\&"}
match=${match//&/"\&"}
replace=${replace//&/"\&"}
ynh_replace_string --match_string="$match_string" --replace_string="$replace_string" --target_file="$target_file"
ynh_replace --match="$match" --replace="$replace" --file="$file"
}
# Sanitize a string intended to be the name of a database

View file

@ -148,14 +148,14 @@ ynh_replace_vars() {
if test -n "${path:-}"; then
# path_slash_less is path, or a blank value if path is only '/'
local path_slash_less=${path%/}
ynh_replace_string --match_string="__PATH__/" --replace_string="$path_slash_less/" --target_file="$file"
ynh_replace_string --match_string="__PATH__" --replace_string="$path" --target_file="$file"
ynh_replace --match="__PATH__/" --replace="$path_slash_less/" --file="$file"
ynh_replace --match="__PATH__" --replace="$path" --file="$file"
fi
if test -n "${app:-}"; then
ynh_replace_string --match_string="__USER__" --replace_string="$app" --target_file="$file"
ynh_replace --match="__USER__" --replace="$app" --file="$file"
fi
if test -n "${ynh_node_load_PATH:-}"; then
ynh_replace_string --match_string="__YNH_NODE_LOAD_PATH__" --replace_string="$ynh_node_load_PATH" --target_file="$file"
ynh_replace --match="__YNH_NODE_LOAD_PATH__" --replace="$ynh_node_load_PATH" --file="$file"
fi
# Replace others variables