From: Michael Tremer Date: Sat, 6 Dec 2014 19:06:51 +0000 (+0100) Subject: Improve parsing configuration files X-Git-Url: http://git.ipfire.org/?p=people%2Fstevee%2Fnetwork.git;a=commitdiff_plain;h=2472e0eae594b0a4f3839dcb76379757e13bee5c Improve parsing configuration files --- diff --git a/src/functions/functions.settings b/src/functions/functions.settings index ff1e9ebf..2caa81fa 100644 --- a/src/functions/functions.settings +++ b/src/functions/functions.settings @@ -20,11 +20,29 @@ ############################################################################### function settings_read() { - local file=${1} + local file="${1}" assert isset file shift - local valid_keys=$@ + local valid_keys + local ignore_superfluous_settings="false" + + local arg + while read -r arg; do + case "${arg}" in + --ignore-superfluous-settings) + ignore_superfluous_settings="true" + ;; + *) + list_append valid_keys "${arg}" + ;; + esac + done <<< "$(args $@)" + + if [ -d "${file}" ]; then + error "Not a configuration file: '${file}'" + return ${EXIT_ERROR} + fi # Exit if the file cannot be read. [ -r "${file}" ] || return ${EXIT_ERROR} @@ -38,7 +56,10 @@ function settings_read() { # If valid keys is set, key must be in the list. if [ -n "${valid_keys}" ]; then if ! listmatch ${key} ${valid_keys}; then - log DEBUG "Ignoring configuration setting: ${key}" + if ! enabled ignore_superfluous_settings; then + log DEBUG "Ignoring configuration setting: ${key}" + fi + continue fi fi diff --git a/src/functions/functions.zone b/src/functions/functions.zone index 65296acf..2a04ccf1 100644 --- a/src/functions/functions.zone +++ b/src/functions/functions.zone @@ -237,8 +237,7 @@ function zone_edit() { return ${EXIT_ERROR} fi - local hook=$(config_get_hook $(zone_dir ${zone})/settings) - + local hook="$(zone_get_hook "${zone}")" if [ -z "${hook}" ]; then error "Config file did not provide any hook." return ${EXIT_ERROR} @@ -303,8 +302,7 @@ function zone_up() { return ${EXIT_ERROR} fi - local hook=$(config_get_hook $(zone_dir ${zone})/settings) - + local hook="$(zone_get_hook "${zone}")" if [ -z "${hook}" ]; then error "Config file did not provide any hook." return ${EXIT_ERROR} @@ -331,8 +329,7 @@ function zone_down() { return ${EXIT_ERROR} fi - local hook=$(config_get_hook $(zone_dir ${zone})/settings) - + local hook="$(zone_get_hook "${zone}")" if [ -z "${hook}" ]; then error "Config file did not provide any hook." return ${EXIT_ERROR} @@ -356,27 +353,27 @@ function zone_down() { } function zone_status() { - local zone=${1} + local zone="${1}" + assert isset zone shift - if ! zone_exists ${zone}; then + if ! zone_exists "${zone}"; then error "Zone '${zone}' does not exist." return ${EXIT_ERROR} fi - local hook=$(config_get_hook $(zone_dir ${zone})/settings) - + local hook="$(zone_get_hook "${zone}")" if [ -z "${hook}" ]; then error "Config file did not provide any hook." return ${EXIT_ERROR} fi - if ! hook_zone_exists ${hook}; then + if ! hook_zone_exists "${hook}"; then error "Hook '${hook}' does not exist." return ${EXIT_ERROR} fi - hook_zone_exec ${hook} status ${zone} $@ + hook_zone_exec "${hook}" "status" "${zone}" "$@" # Show that the zone it to be removed soon. if zone_has_remove_tag ${zone}; then @@ -716,7 +713,7 @@ function zone_ports_status() { } function zone_configs_cmd() { - assert [ $# -gt 2 ] + assert [ $# -ge 2 ] local cmd="${1}" local zone="${2}" @@ -725,8 +722,11 @@ function zone_configs_cmd() { assert zone_exists "${zone}" local config - for config in $(zone_get_configs "${zone}"); do - hook_config_exec "${config}" "${cmd}" "${zone}" $@ + for config in $(zone_configs_list "${zone}"); do + local config_hook="$(zone_config_get_hook "${zone}" "${config}")" + assert isset config_hook + + hook_config_exec "${config_hook}" "${cmd}" "${zone}" "${config}" $@ done } @@ -753,6 +753,22 @@ function zone_configs_list() { done } +function zone_config_get_hook() { + assert [ $# -eq 2 ] + + local zone="${1}" + assert isset zone + + local config="${2}" + assert isset config + + local HOOK + zone_config_settings_read "${zone}" "${config}" \ + --ignore-superfluous-settings HOOK + + print "${HOOK}" +} + function zone_has_ip() { device_has_ip $@ } diff --git a/src/hooks/configs/ipv4-static b/src/hooks/configs/ipv4-static index 50eda815..6ffb0bcf 100644 --- a/src/hooks/configs/ipv4-static +++ b/src/hooks/configs/ipv4-static @@ -159,8 +159,12 @@ function hook_down() { } function hook_status() { - local zone=${1} - local config=${2} + local zone="${1}" + assert isset zone + + local config="${2}" + assert isset config + shift 2 if ! device_exists ${zone}; then