[type/__uci_section] Split up --option and --list

This commit is contained in:
Dennis Camera 2020-10-24 16:01:02 +02:00
parent 49e867fab4
commit b99ca3cbdf
3 changed files with 50 additions and 18 deletions

View file

@ -28,15 +28,21 @@ None.
OPTIONAL PARAMETERS OPTIONAL PARAMETERS
------------------- -------------------
list
An option that is part of a list and should be present in the section (as
part of a list). Lists with multiple options can be expressed by using the
same ``<option>`` repeatedly.
The value to this parameter is a ``<option>=<value>`` string.
match match
Allows to find a section to "replace" through one of its parameters. Allows to find a section to "replace" through one of its parameters.
The value to this parameter is a ``<option>=<value>`` string. The value to this parameter is a ``<option>=<value>`` string.
option option
An option that should be present in the section. An option that should be present in the section.
This parameter can be used multiple times to specify multiple options. This parameter can be used multiple times to specify multiple options.
The value to this parameter is a ``<option>=<value>`` string.
Lists can be expressed by repeatedly using the same key. The value to this parameter is a ``<option>=<value>`` string.
state state
``present`` or ``absent``, defaults to ``present``. ``present`` or ``absent``, defaults to ``present``.
transaction transaction
@ -62,6 +68,15 @@ EXAMPLES
--option PasswordAuth=off \ --option PasswordAuth=off \
--option RootPasswordAuth=off --option RootPasswordAuth=off
# Define a firewall zone comprised of lan and wlan networks
__uci_section firewall.internal --type firewall.zone --transaction fw \
--option name='internal' \
--list network='lan' \
--list network='wlan' \
--option input='ACCEPT' \
--option output='ACCEPT' \
--option forward='ACCEPT'
# Block SSH access from the guest network # Block SSH access from the guest network
__uci_section firewall.block_ssh_from_guest --type firewall.rule \ __uci_section firewall.block_ssh_from_guest --type firewall.rule \
--transaction fwrules \ --transaction fwrules \

View file

@ -18,7 +18,14 @@
# along with cdist. If not, see <http://www.gnu.org/licenses/>. # along with cdist. If not, see <http://www.gnu.org/licenses/>.
# #
grep_line() { echo "$2" | grep -qxF "$1"; } grep_line() { { shift; printf '%s\n' "$@"; } | grep -qxF "$1"; }
prefix_lines() {
while test $# -gt 0
do
echo "$2" | awk -v prefix="$1" '$0 { printf "%s %s\n", prefix, $0 }'
shift; shift
done
}
uci_validate_name() { uci_validate_name() {
# like util.c uci_validate_name() # like util.c uci_validate_name()
test -n "$*" && test -z "$(printf %s "$*" | tr -d '[:alnum:]_' | tr -c '' .)" test -n "$*" && test -z "$(printf %s "$*" | tr -d '[:alnum:]_' | tr -c '' .)"
@ -115,14 +122,25 @@ in
fi fi
# Check options for syntax errors # Check options for syntax errors
validate_options "${__object:?}/parameter/object" \ validate_options "${__object:?}/parameter/list" "${__object:?}/parameter/object" \
| print_errors 'Found erroneous options in arguments:' | print_errors 'Found erroneous options in arguments:'
# Collect option names # Collect option names
if test -f "${__object:?}/parameter/list"
then
listnames_should=$(
sed -e 's/=.*$//' "${__object:?}/parameter/list" | sort -u)
fi
if test -f "${__object:?}/parameter/option" if test -f "${__object:?}/parameter/option"
then then
optnames_should=$( optnames_should=$(sed -e 's/=.*$//' "${__object:?}/parameter/option" | sort)
sed -e 's/=.*$//' "${__object:?}/parameter/option" | sort -u)
echo "${optnames_should}" \
| uniq -d \
| print_errors \
'Found duplicate --options:' \
"$(printf '\nUse --list for lists, instead.')"
fi fi
# Make sure the section itself is present # Make sure the section itself is present
@ -130,29 +148,27 @@ in
--value "${sect_type}" --value "${sect_type}"
export require=__uci/"${section}" export require=__uci/"${section}"
# Delete options not in "should" # Delete options/lists not in "should"
sed -e 's/=.*$//;s/^.*\.//' "${__object:?}/explorer/options" \ sed -e 's/=.*$//;s/^.*\.//' "${__object:?}/explorer/options" \
| while read -r optname | while read -r optname
do do
if ! grep_line "${optname}" "${optnames_should}" grep_line "${optname}" "${listnames_should}" "${optnames_should}" || {
then __uci "${section}.${optname}" --state absent --transaction "${transaction_name}"
__uci "${section}.${optname}" --state absent \ } </dev/null
--transaction "${transaction_name}" </dev/null
fi
done done
# Set "should" options # Set "should" options
echo "${optnames_should}" \ prefix_lines option "${optnames_should}" list "${listnames_should}" \
| while read -r optname | while read -r _type _optname
do do
test -n "${optname}" || continue # ignore empty lines test -n "${_type}${_optname}" || continue # ignore empty lines
grep "^${optname}=" <"${__object:?}/parameter/option" \ grep "^${_optname}=" <"${__object:?}/parameter/${_type}" \
| sed -e 's/^.*=//' \ | sed -e 's/^.*=//' \
| unquote_lines \ | unquote_lines \
| append_values \ | append_values \
__uci "${section}.${optname}" --state present \ __uci "${section}.${_optname}" --state present \
--transaction "${transaction_name}" --type "${_type}" --transaction "${transaction_name}"
done done
;; ;;
(absent) (absent)

View file

@ -1 +1,2 @@
list
option option