[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 #!/bin/bash
# In simple cases, you don't need a config script.
# In most simple cases, you don't need a config script (or just to reload services).
# With a simple config_panel.toml, you can write in the app settings, in the # 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 # The config scripts allows you to go further, to handle specific cases
# (validation of several interdependent fields, specific getter/setter for a value, # (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 # 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 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 fi
} }
get__passphrase() { get__prices() {
if [ -s $final_path/keys/credentials ] 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 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 fi
} }
@ -33,42 +66,37 @@ get__passphrase() {
#================================================= #=================================================
# SPECIFIC VALIDATORS FOR TOML SHORT KEYS # SPECIFIC VALIDATORS FOR TOML SHORT KEYS
#================================================= #=================================================
validate__user() { validate__publishable_key() {
[[ -n "$passphrase" && -z "$user" ]] &&
echo 'A Username is needed when you suggest a Password'
}
validate__passphrase() { # We can imagine here we test if the key is really a publisheable key
[[ -n "$user" && -z "$passphrase" ]] && (is_secret_key $publishable_key) &&
echo 'A Password is needed when you suggest a Username' echo 'This key seems to be a secret key'
} }
#================================================= #=================================================
# SPECIFIC SETTERS FOR TOML SHORT KEYS # SPECIFIC SETTERS FOR TOML SHORT KEYS
#================================================= #=================================================
set__user() { set__prices() {
if [ -z "$user" ]
then
echo "$user\n$passphrase" > $final_path/keys/credentials
else
echo "" > $final_path/keys/credentials
fi
}
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"
echo "DONATION['$frequency']['$currency'] = '$price_id'" >> "$final_path/settings.py"
done
#---------------------------------------------
# IMPORTANT: to be able to upgrade properly, you have to saved the value in settings too
#---------------------------------------------
ynh_app_setting_set $app prices $prices
}
#================================================= #=================================================
# OVERWRITING APPLY STEP # GENERIC FINALIZATION
#================================================= #=================================================
ynh_panel_apply() { ynh_app_config_run $1
_ynh_panel_apply
# Reload app service
systemctl reload APP
}