base-files: allow reusing of boolean value extraction logic
authorOldřich Jedlička <oldium.pro@gmail.com>
Sun, 8 Nov 2020 15:15:04 +0000 (16:15 +0100)
committerPaul Spooren <mail@aparcar.org>
Fri, 1 Jan 2021 20:23:56 +0000 (10:23 -1000)
The `functions.sh` script has `config_get_bool()` function, which is
usable when using UCI config direct access API, but there is no
equivalent for the callback API. Introduce `get_bool()` function to
allow reusing it from init scripts.

Example:

```sh
option_cb() {
    local option="$1"
    local value="$(get_bool "$2")"
    ...
}
```

Signed-off-by: Oldřich Jedlička <oldium.pro@gmail.com>
package/base-files/Makefile
package/base-files/files/lib/functions.sh

index 35a07f2aad3e6fc41cf3010ce8c7b14ef12d2cde..da3976424f35881326a3c0646992fe2da409c5da 100644 (file)
@@ -12,7 +12,7 @@ include $(INCLUDE_DIR)/version.mk
 include $(INCLUDE_DIR)/feeds.mk
 
 PKG_NAME:=base-files
-PKG_RELEASE:=243
+PKG_RELEASE:=244
 PKG_FLAGS:=nonshared
 
 PKG_FILE_DEPENDS:=$(PLATFORM_DIR)/ $(GENERIC_PLATFORM_DIR)/base-files/
index ee4ad1af8398517c056f4051eb48651fecf695d2..272e230db26d63e83e139681bd182fd216c0ec08 100755 (executable)
@@ -118,15 +118,22 @@ config_get() {
        esac
 }
 
-# config_get_bool <variable> <section> <option> [<default>]
-config_get_bool() {
-       local _tmp
-       config_get _tmp "$2" "$3" "$4"
+# get_bool <value> [<default>]
+get_bool() {
+       local _tmp="$1"
        case "$_tmp" in
                1|on|true|yes|enabled) _tmp=1;;
                0|off|false|no|disabled) _tmp=0;;
-               *) _tmp="$4";;
+               *) _tmp="$2";;
        esac
+       echo -n "$_tmp"
+}
+
+# config_get_bool <variable> <section> <option> [<default>]
+config_get_bool() {
+       local _tmp
+       config_get _tmp "$2" "$3" "$4"
+       _tmp="$(get_bool "$_tmp" "$4")"
        export ${NO_EXPORT:+-n} "$1=$_tmp"
 }