forked from ungleich-public/cdist
[type/__uci] Externalise functions to separate file
This commit is contained in:
parent
63d41a1053
commit
7b30119504
3 changed files with 85 additions and 79 deletions
75
cdist/conf/type/__uci/files/functions.sh
Normal file
75
cdist/conf/type/__uci/files/functions.sh
Normal file
|
@ -0,0 +1,75 @@
|
|||
# -*- mode: sh; indent-tabs-mode: t -*-
|
||||
|
||||
in_list() {
|
||||
printf '%s\n' "$@" | { grep -qxF "$(read -r ndl; echo "${ndl}")"; }
|
||||
}
|
||||
|
||||
quote() {
|
||||
for _arg
|
||||
do
|
||||
shift
|
||||
if test -n "$(printf %s "${_arg}" | tr -d -c '\t\n \042-\047\050-\052\073-\077\133\\`|~' | tr -c '' '.')"
|
||||
then
|
||||
# needs quoting
|
||||
set -- "$@" "$(printf "'%s'" "$(printf %s "${_arg}" | sed -e "s/'/'\\\\''/g")")"
|
||||
else
|
||||
set -- "$@" "${_arg}"
|
||||
fi
|
||||
done
|
||||
unset _arg
|
||||
|
||||
# NOTE: Use printf because POSIX echo interprets escape sequences
|
||||
printf '%s' "$*"
|
||||
}
|
||||
|
||||
uci_cmd() {
|
||||
# Usage: uci_cmd [UCI ARGUMENTS]...
|
||||
# Will output a shell command to be executed locally to add the given
|
||||
# command to $tmpfile.
|
||||
printf "printf '%%s\\\\n' %s" "$(quote "$(quote "$@")")"
|
||||
printf ' >>%s\n' "$(quote "${tmpfile}")"
|
||||
}
|
||||
|
||||
uci_validate_name() {
|
||||
# like util.c uci_validate_name()
|
||||
test -n "$*" && test -z "$(echo "$*" | tr -d '[:alnum:]_')"
|
||||
}
|
||||
|
||||
uci_validate_tuple() (
|
||||
tok=${1:?}
|
||||
case $tok
|
||||
in
|
||||
(*.*.*)
|
||||
# check option
|
||||
option=${tok##*.}
|
||||
uci_validate_name "${option}" || {
|
||||
printf 'Invalid option: %s\n' "${option}" >&2
|
||||
return 1
|
||||
}
|
||||
tok=${tok%.*}
|
||||
;;
|
||||
(*.*)
|
||||
# no option (section definition)
|
||||
;;
|
||||
(*)
|
||||
printf 'Invalid tuple: %s\n' "$1" >&2
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
|
||||
case ${tok#*.}
|
||||
in
|
||||
(@*) section=$(expr "${tok#*.}" : '@\(.*\)\[-*[0-9]*\]$') ;;
|
||||
(*) section=${tok#*.} ;;
|
||||
esac
|
||||
uci_validate_name "${section}" || {
|
||||
printf 'Invalid section: %s\n' "${1#*.}" >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
config=${tok%%.*}
|
||||
uci_validate_name "${config}" || {
|
||||
printf 'Invalid config: %s\n' "${config}" >&2
|
||||
return 1
|
||||
}
|
||||
)
|
|
@ -18,84 +18,6 @@
|
|||
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
in_list() {
|
||||
printf '%s\n' "$@" | { grep -qxF "$(read -r ndl; echo "${ndl}")"; }
|
||||
}
|
||||
|
||||
quote() {
|
||||
for _arg
|
||||
do
|
||||
shift
|
||||
if test -n "$(printf %s "${_arg}" | tr -d -c '\t\n \042-\047\050-\052\073-\077\133\\`|~' | tr -c '' '.')"
|
||||
then
|
||||
# needs quoting
|
||||
set -- "$@" "$(printf "'%s'" "$(printf %s "${_arg}" | sed -e "s/'/'\\\\''/g")")"
|
||||
else
|
||||
set -- "$@" "${_arg}"
|
||||
fi
|
||||
done
|
||||
unset _arg
|
||||
|
||||
# NOTE: Use printf because POSIX echo interprets escape sequences
|
||||
printf '%s' "$*"
|
||||
}
|
||||
|
||||
uci_cmd() {
|
||||
# Usage: uci_cmd [UCI ARGUMENTS]...
|
||||
# Will output a shell command to be executed locally to add the given
|
||||
# command to $tmpfile.
|
||||
printf "printf '%%s\\\\n' %s" "$(quote "$(quote "$@")")"
|
||||
printf ' >>%s\n' "$(quote "${tmpfile}")"
|
||||
}
|
||||
|
||||
uci_validate_name() {
|
||||
# like util.c uci_validate_name()
|
||||
test -n "$*" && test -z "$(echo "$*" | tr -d '[:alnum:]_')"
|
||||
}
|
||||
|
||||
uci_validate_tuple() (
|
||||
tok=${1:?}
|
||||
case $tok
|
||||
in
|
||||
(*.*.*)
|
||||
# check option
|
||||
option=${tok##*.}
|
||||
uci_validate_name "${option}" || {
|
||||
printf 'Invalid option: %s\n' "${option}" >&2
|
||||
return 1
|
||||
}
|
||||
tok=${tok%.*}
|
||||
;;
|
||||
(*.*)
|
||||
# no option (section definition)
|
||||
;;
|
||||
(*)
|
||||
printf 'Invalid tuple: %s\n' "$1" >&2
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
|
||||
case ${tok#*.}
|
||||
in
|
||||
(@*) section=$(expr "${tok#*.}" : '@\(.*\)\[-*[0-9]*\]$') ;;
|
||||
(*) section=${tok#*.} ;;
|
||||
esac
|
||||
uci_validate_name "${section}" || {
|
||||
printf 'Invalid section: %s\n' "${1#*.}" >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
config=${tok%%.*}
|
||||
uci_validate_name "${config}" || {
|
||||
printf 'Invalid config: %s\n' "${config}" >&2
|
||||
return 1
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
config=${__object_id:?}
|
||||
uci_validate_tuple "${config}"
|
||||
|
||||
state_is=$(cat "${__object:?}/explorer/state")
|
||||
state_should=$(cat "${__object:?}/parameter/state")
|
||||
|
||||
|
@ -107,6 +29,13 @@ mkdir -p "${tmpdir}"
|
|||
|
||||
tmpfile="${tmpdir}/${transaction_name}.txt"
|
||||
|
||||
# shellcheck source=files/functions.sh
|
||||
. "${__type:?}/files/functions.sh"
|
||||
|
||||
|
||||
config=${__object_id:?}
|
||||
uci_validate_tuple "${config}"
|
||||
|
||||
|
||||
case ${state_should}
|
||||
in
|
||||
|
|
|
@ -18,7 +18,9 @@
|
|||
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
in_list() { printf '%s\n' "$@" | { grep -qxF "$(read -r ndl; echo "${ndl}")"; } }
|
||||
# shellcheck source=files/functions.sh
|
||||
. "${__type:?}/files/functions.sh"
|
||||
|
||||
|
||||
os=$(cat "${__global:?}/explorer/os")
|
||||
|
||||
|
|
Loading…
Reference in a new issue