]> git.ipfire.org Git - people/ms/network.git/commitdiff
header-config: add generic hook_edit function
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Mon, 17 Jul 2017 15:24:26 +0000 (17:24 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 17 Jul 2017 20:17:58 +0000 (16:17 -0400)
If a hook_parse-cmdline function exists
this functions allows it do edit the hook safely.

Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/header-config

index b697797be5e2239b78e7ec5741f947ca95106323..e3c642338c33b06f0a288185ae38633c455c7b91 100644 (file)
 hook_new() {
        cmd_not_implemented
 }
+
+hook_edit() {
+       local zone=${1}
+       local config=${2}
+       shift 2
+
+       local hook=$(config_get_hook_from_config ${config})
+       local id=$(config_get_id_from_config ${config})
+
+       assert isset hook
+       assert isset id
+
+       if ! zone_exists ${zone}; then
+               error "Zone '${zone}' doesn't exist."
+               exit ${EXIT_ERROR}
+       fi
+
+       # Bring the config down
+       if device_exists ${zone}; then
+               if ! hook_config_cmd "down" "${zone}" "${hook}" "${hook}.${id}"; then
+                       log ERROR "Could not bring config ${config} down for zone ${zone}"
+                       return ${EXIT_ERROR}
+               fi
+       fi
+
+       local ${HOOK_CONFIG_SETTINGS}
+
+       # If reading the config fails we cannot go on
+       if ! zone_config_settings_read "${zone}" "${config}"; then
+               log ERROR "Could read the config ${config} for zone ${zone}"
+               return ${EXIT_ERROR}
+       fi
+
+       if ! hook_parse_cmdline $@; then
+               # Return an error if the parsing of the cmd line fails
+               return ${EXIT_ERROR}
+       fi
+
+       # Write the settings to the config file
+       if ! zone_config_settings_write "${zone}" "${hook}" ${id}; then
+               log ERROR "Could not write config settings file ${config} for ${zone}"
+               return ${EXIT_ERROR}
+       fi
+
+       # Bring the config up
+       if device_exists ${zone}; then
+               if ! hook_config_cmd "up" "${zone}" "${hook}" "${hook}.${id}"; then
+                       log ERROR "Could not bring config ${config} up for zone ${zone}"
+                       return ${EXIT_ERROR}
+               fi
+       fi
+
+       exit ${EXIT_OK}
+}