config helpers: get_var / set_var -> read/write_var_in_file

This commit is contained in:
Alexandre Aubin 2021-09-03 17:13:31 +02:00
parent b28cf8cbce
commit 3163179464
2 changed files with 11 additions and 11 deletions

View file

@ -67,7 +67,7 @@ EOL
local source_key="$(echo "$source" | cut -d: -f1)"
source_key=${source_key:-$short_setting}
local source_file="$(echo "$source" | cut -d: -f2 | sed s@__FINALPATH__@$final_path@ | sed s/__APP__/$app/)"
old[$short_setting]="$(ynh_get_var --file="${source_file}" --key="${source_key}")"
old[$short_setting]="$(ynh_read_var_in_file --file="${source_file}" --key="${source_key}")"
fi
done
@ -130,7 +130,7 @@ _ynh_app_config_apply() {
local source_file="$(echo "$source" | cut -d: -f2 | sed s@__FINALPATH__@$final_path@ | sed s/__APP__/$app/)"
ynh_backup_if_checksum_is_different --file="$source_file"
ynh_set_var --file="${source_file}" --key="${source_key}" --value="${!short_setting}"
ynh_write_var_in_file --file="${source_file}" --key="${source_key}" --value="${!short_setting}"
ynh_store_file_checksum --file="$source_file" --update_only
# We stored the info in settings in order to be able to upgrade the app

View file

@ -475,7 +475,7 @@ ynh_replace_vars () {
# Get a value from heterogeneous file (yaml, json, php, python...)
#
# usage: ynh_get_var --file=PATH --key=KEY
# usage: ynh_read_var_in_file --file=PATH --key=KEY
# | arg: -f, --file= - the path to the file
# | arg: -k, --key= - the key to get
#
@ -504,8 +504,9 @@ ynh_replace_vars () {
# USER = 8102
# user = 'https://donate.local'
# CUSTOM['user'] = 'YunoHost'
#
# Requires YunoHost version 4.3 or higher.
ynh_get_var() {
ynh_read_var_in_file() {
# Declare an array to define the options of this helper.
local legacy_args=fk
local -A args_array=( [f]=file= [k]=key= )
@ -515,10 +516,9 @@ ynh_get_var() {
ynh_handle_getopts_args "$@"
local var_part='^[ \t]*\$?(\w*\[)?[ \t]*["'"']?${key}['"'"]?[ \t]*\]?[ \t]*[:=]>?[ \t]*'
local crazy_value="$((grep -i -o -P '^[ \t]*\$?(\w*\[)?[ \t]*["'"']?${key}['"'"]?[ \t]*\]?[ \t]*[:=]>?[ \t]*\K.*(?=[ \t,\n;]*$)' ${file} || echo YNH_NULL) | head -n1)"
#"
local crazy_value="$(grep -i -o -P '^[ \t]*\$?(\w*\[)?[ \t]*["'"']?${key}['"'"]?[ \t]*\]?[ \t]*[:=]>?[ \t]*\K.*(?=[ \t,\n;]*$)' ${file} || echo YNH_NULL | head -n1)"
local first_char="${crazy_value:0:1}"
if [[ "$first_char" == '"' ]] ; then
echo "$crazy_value" | grep -m1 -o -P '"\K([^"](\\")?)*[^\\](?=")' | head -n1 | sed 's/\\"/"/g'
@ -531,13 +531,13 @@ ynh_get_var() {
# Set a value into heterogeneous file (yaml, json, php, python...)
#
# usage: ynh_set_var --file=PATH --key=KEY --value=VALUE
# usage: ynh_write_var_in_file --file=PATH --key=KEY --value=VALUE
# | arg: -f, --file= - the path to the file
# | arg: -k, --key= - the key to set
# | arg: -v, --value= - the value to set
#
# Requires YunoHost version 4.3 or higher.
ynh_set_var() {
ynh_write_var_in_file() {
# Declare an array to define the options of this helper.
local legacy_args=fkv
local -A args_array=( [f]=file= [k]=key= [v]=value=)
@ -547,7 +547,7 @@ ynh_set_var() {
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
local var_part='[ \t]*\$?(\w*\[)?[ \t]*["'"']?${key}['"'"]?[ \t]*\]?[ \t]*[:=]>?[ \t]*'
local crazy_value="$(grep -i -o -P '^[ \t]*\$?(\w*\[)?[ \t]*["'"']?${key}['"'"]?[ \t]*\]?[ \t]*[:=]>?[ \t]*\K.*(?=[ \t,\n;]*$)' ${file} | head -n1)"
# local crazy_value="$(grep -i -o -P "^${var_part}\K.*(?=[ \t,\n;]*\$)" ${file} | head -n1)"
local first_char="${crazy_value:0:1}"