mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
config helpers: misc style + check if file exists
This commit is contained in:
parent
74714d0a62
commit
552db2e21d
2 changed files with 76 additions and 46 deletions
|
@ -19,7 +19,7 @@ for panel_name, panel in loaded_toml.items():
|
||||||
if not isinstance(param, dict):
|
if not isinstance(param, dict):
|
||||||
continue
|
continue
|
||||||
print(';'.join([
|
print(';'.join([
|
||||||
name,
|
name,
|
||||||
param.get('type', 'string'),
|
param.get('type', 'string'),
|
||||||
param.get('bind', 'settings' if param.get('type', 'string') != 'file' else '')
|
param.get('bind', 'settings' if param.get('type', 'string') != 'file' else '')
|
||||||
]))
|
]))
|
||||||
|
@ -35,45 +35,53 @@ EOL
|
||||||
file_hash[${short_setting}]=""
|
file_hash[${short_setting}]=""
|
||||||
formats[${short_setting}]=""
|
formats[${short_setting}]=""
|
||||||
# Get value from getter if exists
|
# Get value from getter if exists
|
||||||
if type -t $getter 2>/dev/null | grep -q '^function$' 2>/dev/null; then
|
if type -t $getter 2>/dev/null | grep -q '^function$' 2>/dev/null;
|
||||||
|
then
|
||||||
old[$short_setting]="$($getter)"
|
old[$short_setting]="$($getter)"
|
||||||
formats[${short_setting}]="yaml"
|
formats[${short_setting}]="yaml"
|
||||||
|
|
||||||
elif [[ "$bind" == "null" ]] ; then
|
elif [[ "$bind" == "null" ]];
|
||||||
|
then
|
||||||
old[$short_setting]="YNH_NULL"
|
old[$short_setting]="YNH_NULL"
|
||||||
|
|
||||||
# Get value from app settings or from another file
|
# Get value from app settings or from another file
|
||||||
elif [[ "$type" == "file" ]] ; then
|
elif [[ "$type" == "file" ]];
|
||||||
if [[ "$bind" == "settings" ]] ; then
|
then
|
||||||
|
if [[ "$bind" == "settings" ]];
|
||||||
|
then
|
||||||
ynh_die "File '${short_setting}' can't be stored in settings"
|
ynh_die "File '${short_setting}' can't be stored in settings"
|
||||||
fi
|
fi
|
||||||
old[$short_setting]="$(ls $(echo $bind | sed s@__FINALPATH__@$final_path@ | sed s/__APP__/$app/) 2> /dev/null || echo YNH_NULL)"
|
old[$short_setting]="$(ls $(echo $bind | sed s@__FINALPATH__@$final_path@ | sed s/__APP__/$app/) 2> /dev/null || echo YNH_NULL)"
|
||||||
file_hash[$short_setting]="true"
|
file_hash[$short_setting]="true"
|
||||||
|
|
||||||
# Get multiline text from settings or from a full file
|
# Get multiline text from settings or from a full file
|
||||||
elif [[ "$type" == "text" ]] ; then
|
elif [[ "$type" == "text" ]];
|
||||||
if [[ "$bind" == "settings" ]] ; then
|
then
|
||||||
|
if [[ "$bind" == "settings" ]];
|
||||||
|
then
|
||||||
old[$short_setting]="$(ynh_app_setting_get $app $short_setting)"
|
old[$short_setting]="$(ynh_app_setting_get $app $short_setting)"
|
||||||
elif [[ "$bind" == *":"* ]] ; then
|
elif [[ "$bind" == *":"* ]];
|
||||||
|
then
|
||||||
ynh_die "For technical reasons, multiline text '${short_setting}' can't be stored automatically in a variable file, you have to create custom getter/setter"
|
ynh_die "For technical reasons, multiline text '${short_setting}' can't be stored automatically in a variable file, you have to create custom getter/setter"
|
||||||
else
|
else
|
||||||
old[$short_setting]="$(cat $(echo $bind | sed s@__FINALPATH__@$final_path@ | sed s/__APP__/$app/) 2> /dev/null || echo YNH_NULL)"
|
old[$short_setting]="$(cat $(echo $bind | sed s@__FINALPATH__@$final_path@ | sed s/__APP__/$app/) 2> /dev/null || echo YNH_NULL)"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Get value from a kind of key/value file
|
# Get value from a kind of key/value file
|
||||||
else
|
else
|
||||||
if [[ "$bind" == "settings" ]] ; then
|
if [[ "$bind" == "settings" ]];
|
||||||
|
then
|
||||||
bind=":/etc/yunohost/apps/$app/settings.yml"
|
bind=":/etc/yunohost/apps/$app/settings.yml"
|
||||||
fi
|
fi
|
||||||
local bind_key="$(echo "$bind" | cut -d: -f1)"
|
local bind_key="$(echo "$bind" | cut -d: -f1)"
|
||||||
bind_key=${bind_key:-$short_setting}
|
bind_key=${bind_key:-$short_setting}
|
||||||
local bind_file="$(echo "$bind" | cut -d: -f2 | sed s@__FINALPATH__@$final_path@ | sed s/__APP__/$app/)"
|
local bind_file="$(echo "$bind" | cut -d: -f2 | sed s@__FINALPATH__@$final_path@ | sed s/__APP__/$app/)"
|
||||||
old[$short_setting]="$(ynh_read_var_in_file --file="${bind_file}" --key="${bind_key}")"
|
old[$short_setting]="$(ynh_read_var_in_file --file="${bind_file}" --key="${bind_key}")"
|
||||||
|
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_ynh_app_config_apply() {
|
_ynh_app_config_apply() {
|
||||||
|
@ -82,21 +90,27 @@ _ynh_app_config_apply() {
|
||||||
local setter="set__${short_setting}"
|
local setter="set__${short_setting}"
|
||||||
local bind="${binds[$short_setting]}"
|
local bind="${binds[$short_setting]}"
|
||||||
local type="${types[$short_setting]}"
|
local type="${types[$short_setting]}"
|
||||||
if [ "${changed[$short_setting]}" == "true" ] ; then
|
if [ "${changed[$short_setting]}" == "true" ];
|
||||||
|
then
|
||||||
# Apply setter if exists
|
# Apply setter if exists
|
||||||
if type -t $setter 2>/dev/null | grep -q '^function$' 2>/dev/null; then
|
if type -t $setter 2>/dev/null | grep -q '^function$' 2>/dev/null;
|
||||||
|
then
|
||||||
$setter
|
$setter
|
||||||
|
|
||||||
elif [[ "$bind" == "null" ]] ; then
|
elif [[ "$bind" == "null" ]];
|
||||||
|
then
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Save in a file
|
# Save in a file
|
||||||
elif [[ "$type" == "file" ]] ; then
|
elif [[ "$type" == "file" ]];
|
||||||
if [[ "$bind" == "settings" ]] ; then
|
then
|
||||||
|
if [[ "$bind" == "settings" ]];
|
||||||
|
then
|
||||||
ynh_die "File '${short_setting}' can't be stored in settings"
|
ynh_die "File '${short_setting}' can't be stored in settings"
|
||||||
fi
|
fi
|
||||||
local bind_file="$(echo "$bind" | sed s@__FINALPATH__@$final_path@ | sed s/__APP__/$app/)"
|
local bind_file="$(echo "$bind" | sed s@__FINALPATH__@$final_path@ | sed s/__APP__/$app/)"
|
||||||
if [[ "${!short_setting}" == "" ]] ; then
|
if [[ "${!short_setting}" == "" ]];
|
||||||
|
then
|
||||||
ynh_backup_if_checksum_is_different --file="$bind_file"
|
ynh_backup_if_checksum_is_different --file="$bind_file"
|
||||||
rm -f "$bind_file"
|
rm -f "$bind_file"
|
||||||
ynh_delete_file_checksum --file="$bind_file" --update_only
|
ynh_delete_file_checksum --file="$bind_file" --update_only
|
||||||
|
@ -107,15 +121,18 @@ _ynh_app_config_apply() {
|
||||||
ynh_store_file_checksum --file="$bind_file" --update_only
|
ynh_store_file_checksum --file="$bind_file" --update_only
|
||||||
ynh_print_info "File '$bind_file' overwrited with ${!short_setting}"
|
ynh_print_info "File '$bind_file' overwrited with ${!short_setting}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Save value in app settings
|
# Save value in app settings
|
||||||
elif [[ "$bind" == "settings" ]] ; then
|
elif [[ "$bind" == "settings" ]];
|
||||||
|
i then
|
||||||
ynh_app_setting_set $app $short_setting "${!short_setting}"
|
ynh_app_setting_set $app $short_setting "${!short_setting}"
|
||||||
ynh_print_info "Configuration key '$short_setting' edited in app settings"
|
ynh_print_info "Configuration key '$short_setting' edited in app settings"
|
||||||
|
|
||||||
# Save multiline text in a file
|
# Save multiline text in a file
|
||||||
elif [[ "$type" == "text" ]] ; then
|
elif [[ "$type" == "text" ]];
|
||||||
if [[ "$bind" == *":"* ]] ; then
|
then
|
||||||
|
if [[ "$bind" == *":"* ]];
|
||||||
|
then
|
||||||
ynh_die "For technical reasons, multiline text '${short_setting}' can't be stored automatically in a variable file, you have to create custom getter/setter"
|
ynh_die "For technical reasons, multiline text '${short_setting}' can't be stored automatically in a variable file, you have to create custom getter/setter"
|
||||||
fi
|
fi
|
||||||
local bind_file="$(echo "$bind" | sed s@__FINALPATH__@$final_path@ | sed s/__APP__/$app/)"
|
local bind_file="$(echo "$bind" | sed s@__FINALPATH__@$final_path@ | sed s/__APP__/$app/)"
|
||||||
|
@ -133,7 +150,7 @@ _ynh_app_config_apply() {
|
||||||
ynh_backup_if_checksum_is_different --file="$bind_file"
|
ynh_backup_if_checksum_is_different --file="$bind_file"
|
||||||
ynh_write_var_in_file --file="${bind_file}" --key="${bind_key}" --value="${!short_setting}"
|
ynh_write_var_in_file --file="${bind_file}" --key="${bind_key}" --value="${!short_setting}"
|
||||||
ynh_store_file_checksum --file="$bind_file" --update_only
|
ynh_store_file_checksum --file="$bind_file" --update_only
|
||||||
|
|
||||||
# We stored the info in settings in order to be able to upgrade the app
|
# We stored the info in settings in order to be able to upgrade the app
|
||||||
ynh_app_setting_set $app $short_setting "${!short_setting}"
|
ynh_app_setting_set $app $short_setting "${!short_setting}"
|
||||||
ynh_print_info "Configuration key '$bind_key' edited into $bind_file"
|
ynh_print_info "Configuration key '$bind_key' edited into $bind_file"
|
||||||
|
@ -146,8 +163,10 @@ _ynh_app_config_apply() {
|
||||||
_ynh_app_config_show() {
|
_ynh_app_config_show() {
|
||||||
for short_setting in "${!old[@]}"
|
for short_setting in "${!old[@]}"
|
||||||
do
|
do
|
||||||
if [[ "${old[$short_setting]}" != YNH_NULL ]] ; then
|
if [[ "${old[$short_setting]}" != YNH_NULL ]];
|
||||||
if [[ "${formats[$short_setting]}" == "yaml" ]] ; then
|
then
|
||||||
|
if [[ "${formats[$short_setting]}" == "yaml" ]];
|
||||||
|
then
|
||||||
ynh_return "${short_setting}:"
|
ynh_return "${short_setting}:"
|
||||||
ynh_return "$(echo "${old[$short_setting]}" | sed 's/^/ /g')"
|
ynh_return "$(echo "${old[$short_setting]}" | sed 's/^/ /g')"
|
||||||
else
|
else
|
||||||
|
@ -167,23 +186,28 @@ _ynh_app_config_validate() {
|
||||||
for short_setting in "${!old[@]}"
|
for short_setting in "${!old[@]}"
|
||||||
do
|
do
|
||||||
changed[$short_setting]=false
|
changed[$short_setting]=false
|
||||||
if [ -z ${!short_setting+x} ]; then
|
if [ -z ${!short_setting+x} ];
|
||||||
|
then
|
||||||
# Assign the var with the old value in order to allows multiple
|
# Assign the var with the old value in order to allows multiple
|
||||||
# args validation
|
# args validation
|
||||||
declare "$short_setting"="${old[$short_setting]}"
|
declare "$short_setting"="${old[$short_setting]}"
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
if [ ! -z "${file_hash[${short_setting}]}" ] ; then
|
if [ ! -z "${file_hash[${short_setting}]}" ];
|
||||||
|
then
|
||||||
file_hash[old__$short_setting]=""
|
file_hash[old__$short_setting]=""
|
||||||
file_hash[new__$short_setting]=""
|
file_hash[new__$short_setting]=""
|
||||||
if [ -f "${old[$short_setting]}" ] ; then
|
if [ -f "${old[$short_setting]}" ];
|
||||||
|
then
|
||||||
file_hash[old__$short_setting]=$(sha256sum "${old[$short_setting]}" | cut -d' ' -f1)
|
file_hash[old__$short_setting]=$(sha256sum "${old[$short_setting]}" | cut -d' ' -f1)
|
||||||
if [ -z "${!short_setting}" ] ; then
|
if [ -z "${!short_setting}" ];
|
||||||
|
then
|
||||||
changed[$short_setting]=true
|
changed[$short_setting]=true
|
||||||
nothing_changed=false
|
nothing_changed=false
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
if [ -f "${!short_setting}" ] ; then
|
if [ -f "${!short_setting}" ];
|
||||||
|
then
|
||||||
file_hash[new__$short_setting]=$(sha256sum "${!short_setting}" | cut -d' ' -f1)
|
file_hash[new__$short_setting]=$(sha256sum "${!short_setting}" | cut -d' ' -f1)
|
||||||
if [[ "${file_hash[old__$short_setting]}" != "${file_hash[new__$short_setting]}" ]]
|
if [[ "${file_hash[old__$short_setting]}" != "${file_hash[new__$short_setting]}" ]]
|
||||||
then
|
then
|
||||||
|
@ -203,8 +227,8 @@ _ynh_app_config_validate() {
|
||||||
then
|
then
|
||||||
ynh_print_info "Nothing has changed"
|
ynh_print_info "Nothing has changed"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Run validation if something is changed
|
# Run validation if something is changed
|
||||||
ynh_script_progression --message="Validating the new configuration..." --weight=1
|
ynh_script_progression --message="Validating the new configuration..." --weight=1
|
||||||
|
|
||||||
|
@ -212,7 +236,8 @@ _ynh_app_config_validate() {
|
||||||
do
|
do
|
||||||
[[ "${changed[$short_setting]}" == "false" ]] && continue
|
[[ "${changed[$short_setting]}" == "false" ]] && continue
|
||||||
local result=""
|
local result=""
|
||||||
if type -t validate__$short_setting | grep -q '^function$' 2>/dev/null; then
|
if type -t validate__$short_setting | grep -q '^function$' 2>/dev/null;
|
||||||
|
then
|
||||||
result="$(validate__$short_setting)"
|
result="$(validate__$short_setting)"
|
||||||
fi
|
fi
|
||||||
if [ -n "$result" ]
|
if [ -n "$result" ]
|
||||||
|
@ -228,8 +253,8 @@ _ynh_app_config_validate() {
|
||||||
if [[ "$changes_validated" == "false" ]]
|
if [[ "$changes_validated" == "false" ]]
|
||||||
then
|
then
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ynh_app_config_get() {
|
ynh_app_config_get() {
|
||||||
|
@ -255,19 +280,19 @@ ynh_app_config_run() {
|
||||||
declare -Ag binds=()
|
declare -Ag binds=()
|
||||||
declare -Ag types=()
|
declare -Ag types=()
|
||||||
declare -Ag formats=()
|
declare -Ag formats=()
|
||||||
|
|
||||||
case $1 in
|
case $1 in
|
||||||
show)
|
show)
|
||||||
ynh_app_config_get
|
ynh_app_config_get
|
||||||
ynh_app_config_show
|
ynh_app_config_show
|
||||||
;;
|
;;
|
||||||
apply)
|
apply)
|
||||||
max_progression=4
|
max_progression=4
|
||||||
ynh_script_progression --message="Reading config panel description and current configuration..."
|
ynh_script_progression --message="Reading config panel description and current configuration..."
|
||||||
ynh_app_config_get
|
ynh_app_config_get
|
||||||
|
|
||||||
ynh_app_config_validate
|
ynh_app_config_validate
|
||||||
|
|
||||||
ynh_script_progression --message="Applying the new configuration..."
|
ynh_script_progression --message="Applying the new configuration..."
|
||||||
ynh_app_config_apply
|
ynh_app_config_apply
|
||||||
ynh_script_progression --message="Configuration of $app completed" --last
|
ynh_script_progression --message="Configuration of $app completed" --last
|
||||||
|
|
|
@ -481,9 +481,9 @@ ynh_replace_vars () {
|
||||||
#
|
#
|
||||||
# This helpers match several var affectation use case in several languages
|
# This helpers match several var affectation use case in several languages
|
||||||
# We don't use jq or equivalent to keep comments and blank space in files
|
# We don't use jq or equivalent to keep comments and blank space in files
|
||||||
# This helpers work line by line, it is not able to work correctly
|
# This helpers work line by line, it is not able to work correctly
|
||||||
# if you have several identical keys in your files
|
# if you have several identical keys in your files
|
||||||
#
|
#
|
||||||
# Example of line this helpers can managed correctly
|
# Example of line this helpers can managed correctly
|
||||||
# .yml
|
# .yml
|
||||||
# title: YunoHost documentation
|
# title: YunoHost documentation
|
||||||
|
@ -517,6 +517,9 @@ ynh_read_var_in_file() {
|
||||||
ynh_handle_getopts_args "$@"
|
ynh_handle_getopts_args "$@"
|
||||||
set +o xtrace
|
set +o xtrace
|
||||||
local filename="$(basename -- "$file")"
|
local filename="$(basename -- "$file")"
|
||||||
|
|
||||||
|
[[ -f $file ]] || ynh_die "File $file does not exists"
|
||||||
|
|
||||||
local ext="${filename##*.}"
|
local ext="${filename##*.}"
|
||||||
local endline=',;'
|
local endline=',;'
|
||||||
local assign="=>|:|="
|
local assign="=>|:|="
|
||||||
|
@ -576,6 +579,8 @@ ynh_write_var_in_file() {
|
||||||
set +o xtrace
|
set +o xtrace
|
||||||
local var_part='\s*\$?([\w.]*\[)?\s*["'"']?${key}['"'"]?\s*\]?\s*[:=]>?\s*'
|
local var_part='\s*\$?([\w.]*\[)?\s*["'"']?${key}['"'"]?\s*\]?\s*[:=]>?\s*'
|
||||||
|
|
||||||
|
[[ -f $file ]] || ynh_die "File $file does not exists"
|
||||||
|
|
||||||
local crazy_value="$(grep -i -o -P '^\s*\$?([\w.]*\[)?\s*["'"']?${key}['"'"]?\s*\]?\s*[:=]>?\s*\K.*(?=[\s,;]*$)' ${file} | head -n1)"
|
local crazy_value="$(grep -i -o -P '^\s*\$?([\w.]*\[)?\s*["'"']?${key}['"'"]?\s*\]?\s*[:=]>?\s*\K.*(?=[\s,;]*$)' ${file} | head -n1)"
|
||||||
# local crazy_value="$(grep -i -o -P "^${var_part}\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}"
|
local first_char="${crazy_value:0:1}"
|
||||||
|
|
Loading…
Add table
Reference in a new issue