config 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/bash
  2. # In simple cases, you don't need a config script.
  3. # With a simple config_panel.toml, you can write in the app settings, in the
  4. # upstream config file or replace complete files (logo ...) and restart services.
  5. # The config scripts allows you to go further, to handle specific cases
  6. # (validation of several interdependent fields, specific getter/setter for a value,
  7. # display dynamic informations or choices, pre-loading of config type .cube... ).
  8. #=================================================
  9. # IMPORT GENERIC HELPERS
  10. #=================================================
  11. source /usr/share/yunohost/helpers
  12. ynh_abort_if_errors
  13. #=================================================
  14. # RETRIEVE ARGUMENTS
  15. #=================================================
  16. install_dir=$(ynh_app_setting_get --key=install_dir)
  17. #=================================================
  18. # SPECIFIC GETTERS FOR TOML SHORT KEY
  19. #=================================================
  20. get__amount() {
  21. # Here we can imagine to have an API call to stripe to know the amount of donation during a month
  22. local amount=200
  23. # It's possible to change some properties of the question by overriding it:
  24. if [ "$amount" -gt 100 ]; then
  25. cat << EOF
  26. style: success
  27. value: $amount
  28. ask:
  29. en: A lot of donation this month: **$amount €**
  30. EOF
  31. else
  32. cat << EOF
  33. style: danger
  34. value: $amount
  35. ask:
  36. en: Not so much donation this month: $amount €
  37. EOF
  38. fi
  39. }
  40. get__prices() {
  41. local prices
  42. prices="$(grep "DONATION\['" "$install_dir/settings.py" | sed -r "s@^DONATION\['([^']*)'\]\['([^']*)'\] = '([^']*)'@\1/\2/\3@g" | sed -z 's/\n/,/g;s/,$/\n/')"
  43. if [ "$prices" == "," ]; then
  44. # Return YNH_NULL if you prefer to not return a value at all.
  45. echo YNH_NULL
  46. else
  47. echo "$prices"
  48. fi
  49. }
  50. #=================================================
  51. # SPECIFIC VALIDATORS FOR TOML SHORT KEYS
  52. #=================================================
  53. validate__publishable_key() {
  54. # We can imagine here we test if the key is really a publishable key
  55. (is_secret_key "$publishable_key") &&
  56. echo 'This key seems to be a secret key'
  57. }
  58. #=================================================
  59. # SPECIFIC SETTERS FOR TOML SHORT KEYS
  60. #=================================================
  61. set__prices() {
  62. #---------------------------------------------
  63. # IMPORTANT: setters are triggered only if a change is detected
  64. #---------------------------------------------
  65. for price in $(echo "$prices" | sed "s/,/ /"); do
  66. frequency=$(echo "$price" | cut -d/ -f1)
  67. currency=$(echo "$price" | cut -d/ -f2)
  68. price_id=$(echo "$price" | cut -d/ -f3)
  69. sed "d/DONATION\['$frequency'\]\['$currency'\]" "$install_dir/settings.py"
  70. echo "DONATION['$frequency']['$currency'] = '$price_id'" >> "$install_dir/settings.py"
  71. done
  72. #---------------------------------------------
  73. # IMPORTANT: to be able to upgrade properly, you have to save the value in settings too
  74. #---------------------------------------------
  75. ynh_app_setting_set --key=prices --value="$prices"
  76. }
  77. #=================================================
  78. ynh_app_config_run "$1"