config 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. # GENERIC STARTING
  10. #=================================================
  11. # IMPORT GENERIC HELPERS
  12. #=================================================
  13. source /usr/share/yunohost/helpers
  14. ynh_abort_if_errors
  15. #=================================================
  16. # RETRIEVE ARGUMENTS
  17. #=================================================
  18. final_path=$(ynh_app_setting_get $app final_path)
  19. #=================================================
  20. # SPECIFIC GETTERS FOR TOML SHORT KEY
  21. #=================================================
  22. get__amount() {
  23. # Here we can imagine to have an API call to stripe to know the amount of donation during a month
  24. local amount = 200
  25. # It's possible to change some properties of the question by overriding it:
  26. if [ $amount -gt 100 ]
  27. then
  28. cat << EOF
  29. style: success
  30. value: $amount
  31. ask:
  32. en: A lot of donation this month: **$amount €**
  33. EOF
  34. else
  35. cat << EOF
  36. style: danger
  37. value: $amount
  38. ask:
  39. en: Not so much donation this month: $amount €
  40. EOF
  41. fi
  42. }
  43. get__prices() {
  44. local prices = "$(grep "DONATION\['" "$final_path/settings.py" | sed -r "s@^DONATION\['([^']*)'\]\['([^']*)'\] = '([^']*)'@\1/\2/\3@g" | sed -z 's/\n/,/g;s/,$/\n/')"
  45. if [ "$prices" == "," ];
  46. then
  47. # Return YNH_NULL if you prefer to not return a value at all.
  48. echo YNH_NULL
  49. else
  50. echo $prices
  51. fi
  52. }
  53. #=================================================
  54. # SPECIFIC VALIDATORS FOR TOML SHORT KEYS
  55. #=================================================
  56. validate__publishable_key() {
  57. # We can imagine here we test if the key is really a publisheable key
  58. (is_secret_key $publishable_key) &&
  59. echo 'This key seems to be a secret key'
  60. }
  61. #=================================================
  62. # SPECIFIC SETTERS FOR TOML SHORT KEYS
  63. #=================================================
  64. set__prices() {
  65. #---------------------------------------------
  66. # IMPORTANT: setter are trigger only if a change is detected
  67. #---------------------------------------------
  68. for price in $(echo $prices | sed "s/,/ /"); do
  69. frequency=$(echo $price | cut -d/ -f1)
  70. currency=$(echo $price | cut -d/ -f2)
  71. price_id=$(echo $price | cut -d/ -f3)
  72. sed "d/DONATION\['$frequency'\]\['$currency'\]" "$final_path/settings.py"
  73. echo "DONATION['$frequency']['$currency'] = '$price_id'" >> "$final_path/settings.py"
  74. done
  75. #---------------------------------------------
  76. # IMPORTANT: to be able to upgrade properly, you have to saved the value in settings too
  77. #---------------------------------------------
  78. ynh_app_setting_set $app prices $prices
  79. }
  80. #=================================================
  81. # GENERIC FINALIZATION
  82. #=================================================
  83. ynh_app_config_run $1