]> git.ipfire.org Git - people/ms/network.git/commitdiff
Improve parsing configuration files
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 6 Dec 2014 19:06:51 +0000 (20:06 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 6 Dec 2014 19:06:51 +0000 (20:06 +0100)
src/functions/functions.settings
src/functions/functions.zone
src/hooks/configs/ipv4-static

index ff1e9ebff1d799f79a2aa8cb0907a7035a22a35b..2caa81faa1297b34b2660bda08c5bbcdbe605fc2 100644 (file)
 ###############################################################################
 
 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
index 65296acf26237f8eb16f53cadaa4c76ff263733c..2a04ccf13f796192068c1799a630c14dbd796fa1 100644 (file)
@@ -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 $@
 }
index 50eda81536651564cc657b5fe27c48777f31f512..6ffb0bcf1ee6ea728269a931fe2d56e15ed504e3 100644 (file)
@@ -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