From 0840afce03c50824278d64cd204b86e79077b48e Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sat, 24 Oct 2020 12:57:40 +0200 Subject: [PATCH] [type/__uci] Add --type parameter --- cdist/conf/type/__uci/gencode-local | 54 ++++++++---- cdist/conf/type/__uci/gencode-remote | 101 +++++++++++++++++++++++ cdist/conf/type/__uci/man.rst | 9 +- cdist/conf/type/__uci/parameter/optional | 1 + 4 files changed, 146 insertions(+), 19 deletions(-) create mode 100644 cdist/conf/type/__uci/gencode-remote diff --git a/cdist/conf/type/__uci/gencode-local b/cdist/conf/type/__uci/gencode-local index af91a698..34482a01 100755 --- a/cdist/conf/type/__uci/gencode-local +++ b/cdist/conf/type/__uci/gencode-local @@ -117,27 +117,45 @@ in exit 0 fi - if test "$(wc -l "${__object:?}/parameter/value")" -gt 1 - then - # "should" is a list - printf 'set_list %s\n' "${config}" >>"${__messages_out:?}" + # Determine type + type=$(cat "${__object:?}/parameter/type" 2>/dev/null || true) + case ${type} + in + (option|list) ;; + ('') + # Guess type by the number of values + test "$(wc -l "${__object:?}/parameter/value")" -gt 1 \ + && type=list \ + || type=option + ;; + (*) + printf 'Invalid --type: %s\n' "${type}" >&2 + exit 1 + ;; + esac - if test "${state_is}" != 'absent' - then - uci_cmd delete "${config}" - fi + case ${type} + in + (list) + printf 'set_list %s\n' "${config}" >>"${__messages_out:?}" - while read -r value - do - uci_cmd add_list "${config}"="${value}" - done <"${__object:?}/parameter/value" - else - # "should" is a scalar - printf 'set %s\n' "${config}" >>"${__messages_out:?}" + if test "${state_is}" != 'absent' + then + uci_cmd delete "${config}" + fi - value=$(cat "${__object:?}/parameter/value") - uci_cmd set "${config}"="${value}" - fi + while read -r value + do + uci_cmd add_list "${config}"="${value}" + done <"${__object:?}/parameter/value" + ;; + (option) + printf 'set %s\n' "${config}" >>"${__messages_out:?}" + + value=$(cat "${__object:?}/parameter/value") + uci_cmd set "${config}"="${value}" + ;; + esac ;; (absent) if in_list "${state_is}" 'absent' diff --git a/cdist/conf/type/__uci/gencode-remote b/cdist/conf/type/__uci/gencode-remote new file mode 100644 index 00000000..348d15a1 --- /dev/null +++ b/cdist/conf/type/__uci/gencode-remote @@ -0,0 +1,101 @@ +#!/bin/sh -e +# +# 2020 Dennis Camera (dennis.camera@ssrq-sds-fds.ch) +# +# This file is part of cdist. +# +# cdist is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# cdist is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . +# + +# shellcheck source=files/functions.sh +. "${__type:?}/files/functions.sh" + +state_is=$(cat "${__object:?}/explorer/state") +state_should=$(cat "${__object:?}/parameter/state") + +config=${__object_id:?} +uci_validate_tuple "${config}" + + +case ${state_should} +in + (present) + if in_list "${state_is}" 'present' 'rearranged' + then + # NOTE: order is ignored so rearranged is also fine. + exit 0 + fi + + # Determine type + type=$(cat "${__object:?}/parameter/type" 2>/dev/null || true) + case ${type} + in + (option|list) ;; + ('') + # Guess type by the number of values + test "$(wc -l "${__object:?}/parameter/value")" -gt 1 \ + && type=list \ + || type=option + ;; + (*) + printf 'Invalid --type: %s\n' "${type}" >&2 + exit 1 + ;; + esac + + case ${type} + in + (list) + printf 'set_list %s\n' "${config}" >>"${__messages_out:?}" + + if test "${state_is}" != 'absent' + then + uci_cmd delete "${config}" + fi + + while read -r value + do + uci_cmd add_list "${config}"="${value}" + done <"${__object:?}/parameter/value" + ;; + (option) + printf 'set %s\n' "${config}" >>"${__messages_out:?}" + + value=$(cat "${__object:?}/parameter/value") + uci_cmd set "${config}"="${value}" + ;; + esac + ;; + (absent) + if in_list "${state_is}" 'absent' + then + exit 0 + fi + + printf 'delete %s\n' "${config}" >>"${__messages_out:?}" + uci_cmd delete "${config}" + ;; + (*) + printf 'Invalid --state: %s\n' "${state_should}" >&2 + exit 1 + ;; +esac + +if test -s "${__object:?}/files/uci_batch.txt" +then + cat "${__type:?}/files/uci_apply.sh" + printf "uci_apply <<'EOF'\n" + cat "${__object:?}/files/uci_batch.txt" + printf '\nEOF\n' +fi diff --git a/cdist/conf/type/__uci/man.rst b/cdist/conf/type/__uci/man.rst index cf184f9d..9d33d618 100644 --- a/cdist/conf/type/__uci/man.rst +++ b/cdist/conf/type/__uci/man.rst @@ -31,6 +31,10 @@ state transaction The name of the transaction this option belongs to. If none is given: ``default`` is used. +type + If the type should generate an option or a list. + One of: ``option`` or ``list``. + Defaults to auto-detect based on the number of ``--value`` parameters. BOOLEAN PARAMETERS @@ -46,10 +50,13 @@ EXAMPLES # Set the system hostname __uci system.@system[0].hostname --value 'OpenWrt' + # Set DHCP option 252: tell DHCP clients to not ask for proxy information. + __uci dhcp.lan.dhcp_option --type list --value '252,"\n"' + # Enable NTP and NTPd (in one transaction) __uci system.ntp.enabled --value 1 --transaction ntp __uci system.ntp.enable_server --value 1 --transaction ntp - __uci system.ntp.server --transaction ntp \ + __uci system.ntp.server --type list --transaction ntp \ --value '0.openwrt.pool.ntp.org' \ --value '1.openwrt.pool.ntp.org' \ --value '2.openwrt.pool.ntp.org' \ diff --git a/cdist/conf/type/__uci/parameter/optional b/cdist/conf/type/__uci/parameter/optional index ddbbba16..2703b3df 100644 --- a/cdist/conf/type/__uci/parameter/optional +++ b/cdist/conf/type/__uci/parameter/optional @@ -1,2 +1,3 @@ state transaction +type