1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/fider_ynh.git synced 2024-09-03 18:36:11 +02:00

[enh] A small config script

This commit is contained in:
ljf 2021-09-29 02:41:30 +02:00
parent 8afe35a53a
commit 8e21cb4df3

View file

@ -1,31 +1,64 @@
#!/bin/bash
# In most simple cases, you don't need a config script (or just to reload services).
# In simple cases, you don't need a config script.
# With a simple config_panel.toml, you can write in the app settings, in the
# upstream config file or replace complete files (logo ...).
# upstream config file or replace complete files (logo ...) and restart services.
# The config scripts allows you to go further, to handle specific cases
# (validation of several interdependent fields, specific getter/setter for a value,
# pre-loading of config type .cube ).
# display dynamic informations or choices, pre-loading of config type .cube... ).
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source /usr/share/yunohost/helpers
ynh_abort_if_errors
#=================================================
# RETRIEVE ARGUMENTS
#=================================================
final_path=$(ynh_app_setting_get $app final_path)
#=================================================
# SPECIFIC GETTERS FOR TOML SHORT KEY
#=================================================
get__user() {
if [ -s $final_path/keys/credentials ]
get__amount() {
# Here we can imagine to have an API call to stripe to know the amount of donation during a month
local amount = 200
# It's possible to change some properties of the question by overriding it:
if [ $amount -gt 100 ]
then
sed -n 1p $final_path/keys/credentials
cat << EOF
style: success
value: $amount
ask:
en: A lot of donation this month: **$amount €**
EOF
else
cat << EOF
style: danger
value: $amount
ask:
en: Not so much donation this month: $amount €
EOF
fi
}
get__passphrase() {
if [ -s $final_path/keys/credentials ]
get__prices() {
local prices = "$(grep "DONATION\['" "$final_path/settings.py" | sed -r "s@^DONATION\['([^']*)'\]\['([^']*)'\] = '([^']*)'@\1/\2/\3@g" | sed -z 's/\n/,/g;s/,$/\n/')"
if [ "$prices" == "," ];
then
sed -n 2p $final_path/keys/credentials
# Return YNH_NULL if you prefer to not return a value at all.
echo YNH_NULL
else
echo $prices
fi
}
@ -33,42 +66,37 @@ get__passphrase() {
#=================================================
# SPECIFIC VALIDATORS FOR TOML SHORT KEYS
#=================================================
validate__user() {
[[ -n "$passphrase" && -z "$user" ]] &&
echo 'A Username is needed when you suggest a Password'
}
validate__publishable_key() {
validate__passphrase() {
[[ -n "$user" && -z "$passphrase" ]] &&
echo 'A Password is needed when you suggest a Username'
# We can imagine here we test if the key is really a publisheable key
(is_secret_key $publishable_key) &&
echo 'This key seems to be a secret key'
}
#=================================================
# SPECIFIC SETTERS FOR TOML SHORT KEYS
#=================================================
set__user() {
if [ -z "$user" ]
then
echo "$user\n$passphrase" > $final_path/keys/credentials
else
echo "" > $final_path/keys/credentials
fi
}
set__prices() {
set__passphrase() {
:
}
#---------------------------------------------
# IMPORTANT: setter are trigger only if a change is detected
#---------------------------------------------
for price in $(echo $prices | sed "s/,/ /"); do
frequency=$(echo $price | cut -d/ -f1)
currency=$(echo $price | cut -d/ -f2)
price_id=$(echo $price | cut -d/ -f3)
sed "d/DONATION\['$frequency'\]\['$currency'\]" "$final_path/settings.py"
#=================================================
# OVERWRITING APPLY STEP
#=================================================
ynh_panel_apply() {
echo "DONATION['$frequency']['$currency'] = '$price_id'" >> "$final_path/settings.py"
done
_ynh_panel_apply
# Reload app service
systemctl reload APP
#---------------------------------------------
# IMPORTANT: to be able to upgrade properly, you have to saved the value in settings too
#---------------------------------------------
ynh_app_setting_set $app prices $prices
}
#=================================================
# GENERIC FINALIZATION
#=================================================
ynh_app_config_run $1