From cdc207061b53874c0f4c6847b77eea21ebd6bd49 Mon Sep 17 00:00:00 2001 From: Tobias Klaus Date: Sun, 1 Dec 2019 01:03:29 +0100 Subject: [PATCH] Allow for options containing commas * instead of the boolean forcelist use force=option or force=list --- README.md | 10 +++++----- src/uci.lua | 14 ++++++++------ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 295d992..743b8c6 100644 --- a/README.md +++ b/README.md @@ -172,8 +172,8 @@ parameters: A special warning about types: UCI has two types for values internally: `list` and `option`. The module tries to infer the type by looking for `,` in the -input. If you need to force a singleentry list, please be sure to set the -`forcelist=yes` parameter. +input. If you need to force a single entry list, please be sure to set `force=list`. +On the other hand setting `force=option` allows setting options containing commas. | parameter | required | default | choices | comments | |-----------|----------|---------|-----------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| @@ -181,7 +181,7 @@ input. If you need to force a singleentry list, please be sure to set the | value | no | | | For set: value to set the property to | | match | no | | | When present in a set or get op: properties a section must have to be modified or returned | | values | no | | | For set with match: values to set on matching section | -| forcelist | no | false | Boolean | The module trys to guess the uci config type (list or string) from the supplied value via the existance of `,` in the input. Single entry lists require `forcelist=yes` to be recognized correctly | +| force | no | | list, option | The module tries to guess the uci config type (list or option) from the supplied value via the existance of `,` in the input. Single entry lists require `force=list` to be recognized correctly to be able to set options containing commas `force=option` is needed. | | state | no | present | present, absent, set, unset | State of the property | | op | no | | configs, commit, revert, get| If specified, instead of enforcing a value, either list the available configurations, execute a commit/revert operation, or query properties. | | reload | no | | Boolean | Whether to reload the configuration from disk before executing. _Aliases: reload_configs, reload-configs_ | @@ -218,11 +218,11 @@ uci: name=uhttpd.test state="absent" autocommit=true' uci: op=configs ``` -An more complex example showing the usage of forcelist: +A more complex example showing the usage of force: ```yaml - name: Securing uhttpd - Disable listening on wan - uci: name={{ item.key }} value={{ uci.state.network.lan.ipaddr }}:{{ item.port }} forcelist=true autocommit=false + uci: name={{ item.key }} value={{ uci.state.network.lan.ipaddr }}:{{ item.port }} force=list autocommit=false with_items: - { key: 'uhttpd.main.listen_http', port: '80' } - { key: 'uhttpd.main.listen_https', port: '443' } diff --git a/src/uci.lua b/src/uci.lua index 88a7129..f3d0bf9 100644 --- a/src/uci.lua +++ b/src/uci.lua @@ -183,10 +183,12 @@ function set_value(module) local conf, sec = check_config(module, conn, path["config"], path["section"]) local target = p["value"] - local forcelist = p["forcelist"] + local force = p["force"] - if type(target) == "table" and #target == 1 and not forcelist then - target = target[1] + if (force == "option" and type(target) == "table") or type(target) == "table" and #target == 1 then + target = table.concat(target, ',') + elseif force == "list" and (type(target) == "string" or type(target) == "number" ) then + target = { target } end local values = {} @@ -347,8 +349,8 @@ function check_parameters(module) module:fail_json({msg="When deleting options, no value can be set"}) end - if nil ~= p["forcelist"] and ("unset" == p["state"] or "absent" == p["state"]) then - module:fail_json({msg="'forcelist' only applies to set operations"}) + if nil ~= p["force"] and ("unset" == p["state"] or "absent" == p["state"]) then + module:fail_json({msg="'force' only applies to set operations"}) end end @@ -362,7 +364,7 @@ function main(arg) op = { choices={"configs", "commit", "revert", "get"} }, reload = { aliases = {"reload_configs", "reload-configs"}, type='bool'}, autocommit = { default=true, type="bool" }, - forcelist = { default=false, type="bool" }, + force = { choices={"list", "option" } }, type = { aliases = {"section-type"}, type="str" }, socket = { type="path" }, timeout = { type="int"},