1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/mautrix_signal_ynh.git synced 2024-09-03 19:46:07 +02:00
mautrix_signal_ynh/scripts/config
2023-01-06 01:26:44 +01:00

96 lines
3.3 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
ynh_abort_if_errors
#=================================================
# RETRIEVE ARGUMENTS
#=================================================
function get__encryption {
encryption=$(ynh_app_setting_get --app $app --key encryption)
echo "'${encryption}'"
}
#=================================================
# SPECIFIC GETTERS FOR TOML SHORT KEY
#=================================================
# SPECIFIC VALIDATORS FOR TOML SHORT KEYS
#=================================================
#=================================================
# SPECIFIC SETTERS FOR TOML SHORT KEYS
#=================================================
#=================================================
# GENERIC FINALIZATION
#=================================================
function get__listuser {
existingUsers=$(grep -- "\".*: user" "$final_path/config.yaml" | sed -r 's/: user//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr '\n' ',')
cat <<EOF
value: '$existingUsers'
EOF
}
function get__listrelay {
existingRelayUsers=$(grep -- "\".*: relay" "$final_path/config.yaml" | sed -r 's/: relay//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr '\n' ',')
cat <<EOF
value: '$existingRelayUsers'
EOF
}
function get__listadmin {
existingAdmins=$(grep -- "\".*: admin" "$final_path/config.yaml" | sed -r 's/: admin//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr '\n' ',')
cat <<EOF
value: '$existingAdmins'
EOF
}
function get__role {
cat <<EOF
choices: ["admin", "user", "relay"]
default: "user"
EOF
}
function set__role {
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
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"
}
ynh_app_config_run $1