[enh] helpers: allow to get/set/delete app settings without explicitly passing app id everytime...

This commit is contained in:
Alexandre Aubin 2021-10-24 18:16:21 +02:00
parent 974ea71fc8
commit fcd2ef9d20
2 changed files with 35 additions and 0 deletions

View file

@ -8,6 +8,7 @@
#
# Requires YunoHost version 2.2.4 or higher.
ynh_app_setting_get() {
local _globalapp=${app-:}
# Declare an array to define the options of this helper.
local legacy_args=ak
local -A args_array=([a]=app= [k]=key=)
@ -15,6 +16,7 @@ ynh_app_setting_get() {
local key
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
app="${app:-$_globalapp}"
if [[ $key =~ (unprotected|protected|skipped)_ ]]; then
yunohost app setting $app $key
@ -32,6 +34,7 @@ ynh_app_setting_get() {
#
# Requires YunoHost version 2.2.4 or higher.
ynh_app_setting_set() {
local _globalapp=${app-:}
# Declare an array to define the options of this helper.
local legacy_args=akv
local -A args_array=([a]=app= [k]=key= [v]=value=)
@ -40,6 +43,7 @@ ynh_app_setting_set() {
local value
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
app="${app:-$_globalapp}"
if [[ $key =~ (unprotected|protected|skipped)_ ]]; then
yunohost app setting $app $key -v $value
@ -56,6 +60,7 @@ ynh_app_setting_set() {
#
# Requires YunoHost version 2.2.4 or higher.
ynh_app_setting_delete() {
local _globalapp=${app-:}
# Declare an array to define the options of this helper.
local legacy_args=ak
local -A args_array=([a]=app= [k]=key=)
@ -63,6 +68,7 @@ ynh_app_setting_delete() {
local key
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
app="${app:-$_globalapp}"
if [[ "$key" =~ (unprotected|skipped|protected)_ ]]; then
yunohost app setting $app $key -d

View file

@ -0,0 +1,29 @@
ynhtest_settings() {
test -n "$app"
mkdir -p "/etc/yunohost/apps/$app"
echo "label: $app" > "/etc/yunohost/apps/$app/settings.yml"
test -z "$(ynh_app_setting_get --key="foo")"
test -z "$(ynh_app_setting_get --key="bar")"
test -z "$(ynh_app_setting_get --app="$app" --key="baz")"
ynh_app_setting_set --key="foo" --value="foovalue"
ynh_app_setting_set --app="$app" --key="bar" --value="barvalue"
ynh_app_setting_set "$app" baz bazvalue
test "$(ynh_app_setting_get --key="foo")" == "foovalue"
test "$(ynh_app_setting_get --key="bar")" == "barvalue"
test "$(ynh_app_setting_get --app="$app" --key="baz")" == "bazvalue"
ynh_app_setting_delete --key="foo"
ynh_app_setting_delete --app="$app" --key="bar"
ynh_app_setting_delete "$app" baz
test -z "$(ynh_app_setting_get --key="foo")"
test -z "$(ynh_app_setting_get --key="bar")"
test -z "$(ynh_app_setting_get --app="$app" --key="baz")"
rm -rf "/etc/yunohost/apps/$app"
}