aboutsummaryrefslogtreecommitdiff
path: root/scripts/config
blob: 48be2ff3fc0d7299c27d926ea395b3e2d37e19d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash
# 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 ...) 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,
# display dynamic informations or choices, pre-loading of config type .cube... ).

#=================================================
# IMPORT GENERIC HELPERS
#=================================================

source /usr/share/yunohost/helpers

ynh_abort_if_errors

#=================================================
# RETRIEVE ARGUMENTS
#=================================================

install_dir=$(ynh_app_setting_get --app="$app" --key=install_dir)

#=================================================
# SPECIFIC GETTERS FOR TOML SHORT KEY
#=================================================

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
    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__prices() {
    local prices
    prices="$(grep "DONATION\['" "$install_dir/settings.py" | sed -r "s@^DONATION\['([^']*)'\]\['([^']*)'\] = '([^']*)'@\1/\2/\3@g" | sed -z 's/\n/,/g;s/,$/\n/')"
    if [ "$prices" == "," ]; then
        # Return YNH_NULL if you prefer to not return a value at all.
        echo YNH_NULL
    else
        echo "$prices"
    fi
}


#=================================================
# SPECIFIC VALIDATORS FOR TOML SHORT KEYS
#=================================================
validate__publishable_key() {

    # We can imagine here we test if the key is really a publishable key
    (is_secret_key "$publishable_key") &&
        echo 'This key seems to be a secret key'
}

#=================================================
# SPECIFIC SETTERS FOR TOML SHORT KEYS
#=================================================
set__prices() {

    #---------------------------------------------
    # IMPORTANT: setters are triggered 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'\]" "$install_dir/settings.py"

        echo "DONATION['$frequency']['$currency'] = '$price_id'" >> "$install_dir/settings.py"
    done

    #---------------------------------------------
    # IMPORTANT: to be able to upgrade properly, you have to save the value in settings too
    #---------------------------------------------
    ynh_app_setting_set --app="$app" --key=prices --value="$prices"
}

#=================================================
# GENERIC FINALIZATION
#=================================================
ynh_app_config_run "$1"