Commit 6ffb9edf authored by ssrq's avatar ssrq

[type/__uci_section] Create only necessary __uci objects

parent 0840afce
Pipeline #2487 failed with stage
in 58 seconds
...@@ -24,5 +24,5 @@ RS=$(printf '\036') ...@@ -24,5 +24,5 @@ RS=$(printf '\036')
section=$("${__type_explorer:?}/match") section=$("${__type_explorer:?}/match")
test -n "${section}" || exit 0 test -n "${section}" || exit 0
uci -s -N -d "${RS}" show "${section}" 2>/dev/null \ # uci(1) returns 1 if the section does not exist
| grep -v -e "^${section}=" || true uci -s -N -d "${RS}" show "${section}" 2>/dev/null || true
...@@ -74,6 +74,10 @@ in ...@@ -74,6 +74,10 @@ in
type_is=$(cat "${__object:?}/explorer/type") type_is=$(cat "${__object:?}/explorer/type")
type_should=$(cat "${__object:?}/parameter/type") type_should=$(cat "${__object:?}/parameter/type")
optnames_is=$(
sed -n -e 's/=.*$//' \
-e 's/^[^.]*\.[^.]*\.//p' "${__object:?}/explorer/options")
if test -f "${__object:?}/parameter/option" if test -f "${__object:?}/parameter/option"
then then
optnames_should=$( optnames_should=$(
...@@ -106,33 +110,51 @@ in ...@@ -106,33 +110,51 @@ in
fi fi
# Make sure the section itself is present # Make sure the section itself is present
if ! grep -qxF "${section}=${sect_type}" "${__object}/explorer/options"
then
__uci "${section}" --state present --transaction "${transaction_name}" \ __uci "${section}" --state present --transaction "${transaction_name}" \
--value "${sect_type}" --value "${sect_type}"
export require=__uci/"${section}" export require=__uci/"${section}"
fi
# Delete options not in "should" # Delete options not in "should"
sed -e 's/=.*$//;s/^.*\.//' "${__object:?}/explorer/options" \ echo "${optnames_is}" \
| while read -r optname | while read -r _optname
do do
if ! grep_line "${optname}" "${optnames_should}" if ! grep_line "${_optname}" "${optnames_should}"
then then
__uci "${section}.${optname}" --state absent \ __uci "${section}.${_optname}" --state absent \
--transaction "${transaction_name}" </dev/null --transaction "${transaction_name}" </dev/null
fi fi
done done
# Set "should" options # Set "should" options
echo "${optnames_should}" \ echo "${optnames_should}" \
| while read -r optname | grep -e . \
| while read -r _optname
do do
test -n "${optname}" || continue # ignore empty lines values_is=$(
sed -n -e 's/^[^.]*\.[^.]*\.//;T' \
-e "/^${_optname}=/p" \
-e 's/^.*=//' "${__object}/explorer/options" \
| tr '\036' '\n' \
| unquote_lines)
grep "^${optname}=" <"${__object:?}/parameter/option" \ values_should=$(
grep -e "^${_optname}=" "${__object:?}/parameter/option" \
| sed -e 's/^.*=//' \ | sed -e 's/^.*=//' \
| unquote_lines \ | unquote_lines)
| append_values \
__uci "${section}.${optname}" --state present \ # Compare "is" and "should" and only generate __uci object
# if they differ
if test "${values_is}" != "${values_should}"
then
# NOTE: Do not use dash's echo, as it evaluates escape
# sequences.
printf '%s\n' "${values_should}" | append_values \
__uci "${section}.${_optname}" --state present \
--transaction "${transaction_name}" --transaction "${transaction_name}"
fi
done done
;; ;;
(absent) (absent)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment