1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/mautrix_whatsapp_ynh.git synced 2024-09-03 19:46:01 +02:00

config setters in common for upgrade

This commit is contained in:
Gredin67 2023-02-09 21:29:08 +01:00 committed by GitHub
parent 95c125fe76
commit 8e4713f642
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 67 deletions

View file

@ -8,13 +8,57 @@
pkg_dependencies="g++ postgresql ffmpeg"
#=================================================
# PERSONAL HELPERS
# CONFIG PANEL SETTERS
#=================================================
#=================================================
# EXPERIMENTAL HELPERS
#=================================================
apply_permissions() {
set -o noglob # Disable globbing to avoid expansions when passing * as value.
declare values="list$role"
newValues="${!values}" # Here we expand the dynamic variable we created in the previous line. ! Does the trick
newValues="${newValues//\"}"
usersArray=(${newValues//,/ }) # Split the values using comma (,) as separator.
#=================================================
# FUTURE OFFICIAL HELPERS
#=================================================
if [ -n "$newValues" ]
then
#ynh_systemd_action --service_name="$app" --action=stop
# Get all entries between "permissions:" and "relay:" keys, remove the role part, remove commented parts, format it with newlines and clean whitespaces and double quotes.
allDefinedEntries=$(awk '/permissions:/{flag=1; next} /relay:/{flag=0} flag' "$final_path/config.yaml" | sed "/: $role/d" | sed -r 's/: (admin|user|relay)//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr ',' '\n' )
# Delete everything from the corresponding role to insert the new defined values. This way we also handle deletion of users.
sed -i "/permissions:/,/relay:/{/: $role/d;}" "$final_path/config.yaml"
for user in "${usersArray[@]}"
do
if grep -q -x "${user}" <<< "$allDefinedEntries"
then
ynh_print_info "User $user already defined in another role."
else
sed -i "/permissions:/a \ \\\"$user\": $role" "$final_path/config.yaml" # Whitespaces are needed so that the file can be correctly parsed
fi
done
fi
set +o noglob
ynh_print_info "Users with role $role added in $final_path/config.yaml"
}
set__listuser() {
role="user"
ynh_app_setting_set --app=$app --key=listuser --value="$listuser"
apply_permissions
ynh_store_file_checksum --file="$final_path/config.yaml"
}
set__listrelay() {
role="relay"
ynh_app_setting_set --app=$app --key=listrelay --value="$listrelay"
apply_permissions
ynh_store_file_checksum --file="$final_path/config.yaml"
}
set__listadmin() {
role="admin"
ynh_app_setting_set --app=$app --key=listadmin --value="$listadmin"
apply_permissions
ynh_store_file_checksum --file="$final_path/config.yaml"
}

View file

@ -1,4 +1,5 @@
#!/bin/bash
source _common.sh
source /usr/share/yunohost/helpers
ynh_abort_if_errors
@ -45,35 +46,6 @@ EOF
# SPECIFIC SETTERS FOR TOML SHORT KEYS
#=================================================
apply_permissions() {
set -o noglob # Disable globbing to avoid expansions when passing * as value.
declare values="list$role"
newValues="${!values}" # Here we expand the dynamic variable we created in the previous line. ! Does the trick
newValues="${newValues//\"}"
usersArray=(${newValues//,/ }) # Split the values using comma (,) as separator.
if [ -n "$newValues" ]
then
#ynh_systemd_action --service_name="$app" --action=stop
# Get all entries between "permissions:" and "relay:" keys, remove the role part, remove commented parts, format it with newlines and clean whitespaces and double quotes.
allDefinedEntries=$(awk '/permissions:/{flag=1; next} /relay:/{flag=0} flag' "$final_path/config.yaml" | sed "/: $role/d" | sed -r 's/: (admin|user|relay)//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr ',' '\n' )
# Delete everything from the corresponding role to insert the new defined values. This way we also handle deletion of users.
sed -i "/permissions:/,/relay:/{/: $role/d;}" "$final_path/config.yaml"
for user in "${usersArray[@]}"
do
if grep -q -x "${user}" <<< "$allDefinedEntries"
then
ynh_print_info "User $user already defined in another role."
else
sed -i "/permissions:/a \ \\\"$user\": $role" "$final_path/config.yaml" # Whitespaces are needed so that the file can be correctly parsed
fi
done
fi
set +o noglob
ynh_print_info "Users with role $role added in $final_path/config.yaml"
}
set__botname() {
old_botname=$(ynh_app_setting_get --app $app --key botname)
if [ "$botname" -eq "$old_botname" ] # Check to avoid updating botname when it's not needed.
@ -92,25 +64,4 @@ set__botname() {
ynh_store_file_checksum --file="$final_path/config.yaml"
}
set__listuser() {
role="user"
ynh_app_setting_set --app=$app --key=listuser --value="$listuser"
apply_permissions
ynh_store_file_checksum --file="$final_path/config.yaml"
}
set__listrelay() {
role="relay"
ynh_app_setting_set --app=$app --key=listrelay --value="$listrelay"
apply_permissions
ynh_store_file_checksum --file="$final_path/config.yaml"
}
set__listadmin() {
role="admin"
ynh_app_setting_set --app=$app --key=listadmin --value="$listadmin"
apply_permissions
ynh_store_file_checksum --file="$final_path/config.yaml"
}
ynh_app_config_run $1

View file

@ -287,11 +287,11 @@ chown $app:$app "$final_path/config.yaml"
listrelay=$listrelay_
listuser=$listuser_
listadmin=$listadmin_
# MIGRATION from previous version without config panel -> config panel does not exist yet, so we upgrade the config after starting the systemd service
# apply_permissions to have correct syntax in config file
#yunohost app config set $app main.permissions.listrelay -v "$listrelay"
#yunohost app config set $app main.permissions.listuser -v "$listuser"
#yunohost app config set $app main.permissions.listadmin -v "$listadmin"
yunohost app config set $app main.permissions.listrelay -v "$listrelay"
yunohost app config set $app main.permissions.listuser -v "$listuser"
yunohost app config set $app main.permissions.listadmin -v "$listadmin"
#=================================================
# REGISTER SYNAPSE APP-SERVICE
@ -342,12 +342,6 @@ ynh_script_progression --message="Starting a systemd service..." --weight=1
# Start a systemd service
ynh_systemd_action --service_name=$app --action="start"
# MIGRATION from previous version without config panel -> config panel does not exist yet, so we upgrade the config after starting the systemd service
# apply_permissions to have correct syntax in config file
yunohost app config set $app main.permissions.listrelay -v "$listrelay"
yunohost app config set $app main.permissions.listuser -v "$listuser"
yunohost app config set $app main.permissions.listadmin -v "$listadmin"
#=================================================
# END OF SCRIPT
#=================================================