These are the basic functions to work with hids.
Fixes: #11406
Signed-off-by: Jonatan Schlag jonatan.schlag@ipfire.org --- src/functions/functions.zone | 80 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+)
diff --git a/src/functions/functions.zone b/src/functions/functions.zone index bba1705..0696750 100644 --- a/src/functions/functions.zone +++ b/src/functions/functions.zone @@ -656,6 +656,69 @@ zone_config_list_ids() { echo ${ids} }
+# List all hids of a zone +zone_config_list_hids() { + assert [ $# -eq 1 ] + local zone=${1} + + local config + local hids + + for config in $(zone_configs_list ${zone}); do + # Append hids to the list + zone_config_get_hid "${zone}" "${config}" + done +} + +# get the hid from a given config +zone_config_get_hid() { + assert [ $# -eq 2 ] + local zone=${1} + local config=${2} + + local hook + + hook="$(zone_config_get_hook "${zone}" "${config}")" + hook_exec "config" "${hook}" "hid" "${zone}" "${config}" +} + +# Checks if a hid is valid for a given zone +zone_config_hid_is_valid() { + assert [ $# -eq 2] + local zone=${1} + local hid=${2} + + local _hid + for _hid in $(zone_config_list_hids "${zone}"); do + if [[ ${_hid} = ${hid} ]]; then + return ${EXIT_TRUE} + fi + done + + return ${EXIT_FALSE} +} + +# This function converts a hid to a id +zone_config_convert_hid_to_id() { + assert [ $# -eq 2 ] + local zone=${1} + local hid=${2} + + local hook + local config + + for config in $(zone_configs_list ${zone}); do + # Get hook from config + hook="$(zone_config_get_hook "${zone}" "${config}")" + if [[ "$(hook_exec "config" "${hook}" "hid" "${zone}" "${config}")" == "${hid}" ]]; then + config_get_id_from_config "${config}" + return ${EXIT_TRUE} + fi + done + + return ${EXIT_FALSE} +} + zone_show() { local zone=${1}
@@ -1114,6 +1177,23 @@ zone_config_id_is_valid() { [ -f ${zone_path}/configs/*.${id} ]; }
+# This function checks if a given hid is valid for a zone +# Return True when yes and false when no +zone_config_hid_is_valid() { + assert [ $# -eq 2 ] + local zone=${1} + local hid=${2} + + local _hid + for _hid in $(zone_config_list_hids ${zone}); do + if [[ ${_hid} == ${hid} ]]; then + return ${EXIT_TRUE} + fi + done + + return ${EXIT_FALSE} +} + zone_config_get_hook_from_id() { # Returns the hook for a given id assert [ $# -eq 2 ]
Signed-off-by: Jonatan Schlag jonatan.schlag@ipfire.org --- src/functions/functions.zone | 46 ++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-)
diff --git a/src/functions/functions.zone b/src/functions/functions.zone index 0696750..90dfbc6 100644 --- a/src/functions/functions.zone +++ b/src/functions/functions.zone @@ -550,28 +550,46 @@ zone_config() { zone_config_new "${zone}" "$@" ;; destroy) - local id=${1} - if zone_config_id_is_valid ${zone} ${id}; then - zone_config_destroy "${zone}" "$@" + # usually ${1} is a valid hid + local hid=${1} + shift 1 + # We convert the hid into an id + local id=$(zone_config_convert_hid_to_id ${zone} ${hid}) + # If id isset the hid is valid and we can go on with the id + if isset id; then + zone_config_destroy "${zone}" "${id}" "$@" else - log ERROR "${id} is not a valid id" + # We get no valid hid so we check if we get a valid id + if zone_config_id_is_valid ${zone} ${hid}; then + zone_config_destroy "${zone}" ${hid} "$@" + else + log ERROR "${id} is not a valid id or hid" + fi fi ;; list) zone_config_list "${zone}" "$@" ;; *) - # Check is we get a valid id - # TODO This could be also a valid hid - local id=${cmd} - - if zone_config_id_is_valid ${zone} ${id} && [[ ${1} == "edit" ]]; then - shift 1 - zone_config_edit "${zone}" "${id}" "$@" + # usually ${1} is a valid hid + local hid=${cmd} + local cmd=${1} + shift 1 + local id=$(zone_config_convert_hid_to_id ${zone} ${hid}) + # If id isset the hid is valid and we can go on with the id + if isset id && [[ ${cmd} == "edit" ]]; then + zone_config_edit "${zone}" "${id}" "$@" else - error "Unrecognized argument: ${cmd}" - cli_usage root-zone-config-subcommands - exit ${EXIT_ERROR} + # We get no valid hid so we check if we get a valid id + if zone_config_id_is_valid ${zone} ${id} && [[ ${cmd} == "edit" ]]; then + shift 1 + zone_config_edit "${zone}" "${id}" "$@" + else + # in ${hid} is saved the command after network zone ${zone} config + error "Unrecognized argument: ${hid}" + cli_usage root-zone-config-subcommands + exit ${EXIT_ERROR} + fi fi ;; esac
Signed-off-by: Jonatan Schlag jonatan.schlag@ipfire.org --- src/functions/functions.zone | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/src/functions/functions.zone b/src/functions/functions.zone index 90dfbc6..7947d9a 100644 --- a/src/functions/functions.zone +++ b/src/functions/functions.zone @@ -637,21 +637,22 @@ zone_config_list() { assert isset zone
# Print a nice header - local format="%-3s %-20s" - print "${format}" "ID" "HOOK" + local format="%-3s %-20s %-20s" + print "${format}" "ID" "HOOK" "HID"
local config local hook local id + local hid
# Print for all config: # id and hook - # TODO: Add hids here for config in $(zone_configs_list "${zone}"); do id=${config##*.} hook=$(zone_config_get_hook "${zone}" "${config}") + hid=$(zone_config_get_hid "${zone}" "${config}") assert isset hook - print "${format}" "${id}" "${hook}" + print "${format}" "${id}" "${hook}" "${hid}" done }
Signed-off-by: Jonatan Schlag jonatan.schlag@ipfire.org --- src/functions/functions.hook | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/functions/functions.hook b/src/functions/functions.hook index d1f6506..d2f4a78 100644 --- a/src/functions/functions.hook +++ b/src/functions/functions.hook @@ -204,7 +204,7 @@ hook_valid_command_config() { local cmd="${1}"
case "${cmd}" in - new|destroy|edit|up|down|status) + new|destroy|edit|up|down|status|hid) return ${EXIT_TRUE} ;; esac
This function will always be there so when we call hook_hid we will get a result. This is also nice for testing.
Signed-off-by: Jonatan Schlag jonatan.schlag@ipfire.org --- src/header-config | 8 ++++++++ 1 file changed, 8 insertions(+)
diff --git a/src/header-config b/src/header-config index e3c6423..1b6f530 100644 --- a/src/header-config +++ b/src/header-config @@ -76,3 +76,11 @@ hook_edit() {
exit ${EXIT_OK} } + +# We will return the id as generic variant of the hid +# Although this should not be the standard +hook_hid() { + # the zone name would be ${1} + local config=${2} + config_get_id_from_config "${config}" +}
Signed-off-by: Jonatan Schlag jonatan.schlag@ipfire.org --- src/network | 3 +++ 1 file changed, 3 insertions(+)
diff --git a/src/network b/src/network index adc9ac3..7965d62 100644 --- a/src/network +++ b/src/network @@ -1380,6 +1380,9 @@ cli_raw() { list-zone-config-ids) zone_config_list_ids $@ ;; + list-zone-config-hids) + zone_config_list_hids $@ + ;; zone-name-is-valid) zone_name_is_valid $@ ;;
Signed-off-by: Jonatan Schlag jonatan.schlag@ipfire.org --- src/network | 3 +++ 1 file changed, 3 insertions(+)
diff --git a/src/network b/src/network index 7965d62..16c8f60 100644 --- a/src/network +++ b/src/network @@ -1389,6 +1389,9 @@ cli_raw() { zone-config-id-is-valid) zone_config_id_is_valid $@ ;; + zone-config-hid-is-valid) + zone_config_hid_is_valid $@ + ;; *) error "No such command: ${cmd}" exit ${EXIT_ERROR}
Signed-off-by: Jonatan Schlag jonatan.schlag@ipfire.org --- src/bash-completion/network | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/bash-completion/network b/src/bash-completion/network index 820c2d4..529a772 100644 --- a/src/bash-completion/network +++ b/src/bash-completion/network @@ -421,7 +421,7 @@ _network_zone_subcommand_config() {
local words=( $@ )
- local commands="destroy list new $(network raw list-zone-config-ids ${zone})" + local commands="destroy list new $(network raw list-zone-config-hids ${zone})"
local cmd="$(_network_find_on_cmdline "${commands}")" if [[ -z "${cmd}" ]]; then @@ -442,7 +442,7 @@ _network_zone_subcommand_config() { *) # Check if we get a valid id # TODO: We should also accept a valid hid - if network raw zone-config-id-is-valid ${zone} ${cmd}; then + if network raw zone-config-id-is-valid ${zone} ${cmd} || network raw zone-config-hid-is-valid ${zone} ${cmd}; then _network_zone_subcommand_config_subcommand ${zone} ${args} fi ;;