From: Jonatan Schlag Date: Wed, 7 Jun 2017 06:20:28 +0000 (+0200) Subject: improve autocompletion of network zone new X-Git-Tag: 009~226 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ca5fe9f384e98872aa92ebd8b7fee17b98dfe7d;p=network.git improve autocompletion of network zone new Fixes: #11386 Signed-off-by: Jonatan Schlag Signed-off-by: Michael Tremer --- diff --git a/src/bash-completion/network b/src/bash-completion/network index ae358bdc..4b5e34d3 100644 --- a/src/bash-completion/network +++ b/src/bash-completion/network @@ -318,11 +318,11 @@ _network_zone() { return 0 fi + local args="${words[@]:1}" case "${cmd}" in new) - # TODO - return 0 + _network_zone_new ${args} ;; destroy) _network_complete_zones @@ -335,6 +335,24 @@ _network_zone() { esac } +_network_zone_new() { + local words=( $@ ) + local cmd=${words[@]:0:1} + + # Suggest useful zone names + if [[ -z "${cmd}" ]]; then + local commands="$(network raw list-next-free-zones)" + COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) + + # If a valid zone name was entered, we can move on + elif network raw zone-name-is-valid ${cmd}; then + local args="${words[@]:1}" + _network_complete_hooks zone ${args} + fi + + return 0 +} + _network_zone_subcommand() { local zone="${1}" shift diff --git a/src/functions/functions.zone b/src/functions/functions.zone index c7275060..88c81a86 100644 --- a/src/functions/functions.zone +++ b/src/functions/functions.zone @@ -629,6 +629,26 @@ zones_get_all() { done } +zones_get_next_free() { + # This function return the next free zones. + # Example net0 upl0 upl1 are configured so the next free zones are: + # net1 upl2 + local i + local zone_name + for zone_name in ${VALID_ZONES}; do + i=0 + + while true; do + local zone="${zone_name}${i}" + if ! zone_exists ${zone}; then + echo "${zone}" + break + fi + i=$(( i + 1 )) + done + done +} + zones_get_local() { local zone for zone in $(zones_get_all); do diff --git a/src/network b/src/network index 4d5955fa..e1934b85 100644 --- a/src/network +++ b/src/network @@ -1350,6 +1350,12 @@ cli_raw() { list-zones) zones_get_all ;; + list-next-free-zones) + zones_get_next_free + ;; + zone-name-is-valid) + zone_name_is_valid $@ + ;; *) error "No such command: ${cmd}" exit ${EXIT_ERROR}