]> git.ipfire.org Git - people/stevee/network.git/commitdiff
Improve the check for valid hook commands
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 25 Dec 2014 21:11:58 +0000 (21:11 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 25 Dec 2014 21:11:58 +0000 (21:11 +0000)
It is now cleaner coded and faster.

src/functions/functions.hook

index 27f4389a5f45c783a096994888e78e3cddd11ef7..4accfc977658a56714ae71a33518045d8c4cba9b 100644 (file)
 #                                                                             #
 ###############################################################################
 
-HOOK_COMMANDS_CONFIG="hook_create hook_down hook_edit hook_status hook_remove"
-HOOK_COMMANDS_CONFIG="${HOOK_COMMANDS_CONFIG} hook_up"
-
-HOOK_COMMANDS_PORT="hook_new hook_edit hook_create hook_remove hook_up hook_down \
-       hook_hotplug hook_hotplug_rename hook_info hook_status"
-
-HOOK_COMMANDS_ZONE="hook_add hook_new hook_discover hook_down hook_edit hook_help \
-       hook_info hook_destroy hook_status hook_up hook_hotplug \
-       \
-       hook_config_create hook_config_edit hook_config_remove hook_config_show \
-       \
-       hook_port hook_port_add hook_port_edit hook_port_remove hook_port_show \
-       hook_port_status hook_port_up hook_port_down \
-       \
-       hook_ppp_ip_pre_up hook_ppp_ipv4_down hook_ppp_ipv4_up \
-       hook_ipv6_down hook_ipv6_up hook_ppp_write_config"
-
 function hook_dir() {
        local type=${1}
 
@@ -77,19 +60,7 @@ function hook_exec() {
        local hook_cmd="hook_${cmd}"
 
        # Check if the hook action is valid.
-       local valid_commands
-       case "${type}" in
-               "config")
-                       valid_commands="${HOOK_COMMANDS_CONFIG}"
-                       ;;
-               "port")
-                       valid_commands="${HOOK_COMMANDS_PORT}"
-                       ;;
-               "zone")
-                       valid_commands="${HOOK_COMMANDS_ZONE}"
-                       ;;
-       esac
-       isset valid_commands && assert list_match "${hook_cmd}" ${valid_commands}
+       assert hook_valid_command "${type}" "${cmd}"
 
        local hook_path="$(hook_dir ${type})/${hook}"
 
@@ -190,3 +161,112 @@ function hook_config_exec() {
 function hook_config_get_all() {
        hook_list config
 }
+
+function hook_valid_command() {
+       local type="${1}"
+       local cmd="${2}"
+
+       case "${type}" in
+               config)
+                       hook_valid_command_config "${cmd}"
+                       return ${?}
+                       ;;
+               port)
+                       hook_valid_command_port "${cmd}"
+                       return ${?}
+                       ;;
+               zone)
+                       hook_valid_command_zone "${cmd}"
+                       return ${?}
+                       ;;
+       esac
+
+       return ${EXIT_FALSE}
+}
+
+function hook_valid_command_config() {
+       local cmd="${1}"
+
+       case "${cmd}" in
+               create|remove|edit|up|down|status)
+                       return ${EXIT_TRUE}
+                       ;;
+       esac
+
+       return ${EXIT_FALSE}
+}
+
+function hook_valid_command_port() {
+       local cmd="${1}"
+
+       case "${cmd}" in
+               # Configuration hooks
+               new|edit|destroy)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Control hooks
+               create|remove|up|down)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Hotplug
+               hotplug|hotplug_rename)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Status
+               status|info)
+                       return ${EXIT_TRUE}
+                       ;;
+       esac
+
+       return ${EXIT_FALSE}
+}
+
+function hook_valid_command_zone() {
+       local cmd="${1}"
+
+       case "${cmd}" in
+               # Configuration hooks
+               new|edit|destroy)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               config_create|config_edit|config_remove|config_show)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Control hooks
+               up|down)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Hotplug
+               hotplug)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Ports
+               port_add|port_edit|port_remove|port_show|port_status|port_up|port_down)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Status
+               status|info|help)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Discovery
+               discover)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # PPP
+               ppp_ip_pre_up|ppp_ipv[64]_up|ppp_ipv[64]_down|ppp_write_config)
+                       return ${EXIT_TRUE}
+                       ;;
+       esac
+
+       return ${EXIT_FALSE}
+}