From cc02f6bea65036ed2202a5155ca84addddc8b705 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 1 Jan 2015 22:39:33 +0000 Subject: [PATCH] bash-completion: Add some basic completion for DHCP servers --- src/bash-completion/network | 123 +++++++++++++++++++++++++++++++++- src/functions/functions.dhcpd | 23 +++---- src/network | 18 +++-- 3 files changed, 143 insertions(+), 21 deletions(-) diff --git a/src/bash-completion/network b/src/bash-completion/network index 8c3ec1b8..143295c8 100644 --- a/src/bash-completion/network +++ b/src/bash-completion/network @@ -86,6 +86,121 @@ function _network_device_subcommand() { esac } +function _network_dhcpd() { + local proto="${1}" + shift + + local words=( $@ ) + + local commands="edit reload restart show start stop subnet" + local cmd="$(_network_find_on_cmdline "${commands}")" + if [[ -z "${cmd}" ]]; then + COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) + return 0 + fi + + local args="${words[@]:1}" + case "${cmd}" in + subnet) + _network_dhcpd_subnet "${proto}" ${args} + ;; + esac +} + +function _network_dhcpd_subnet() { + local proto="${1}" + shift + + local words=( $@ ) + + local commands="new remove show $(network raw list-dhcpd-subnets "${proto}")" + local cmd="$(_network_find_on_cmdline "${commands}")" + if [[ -z "${cmd}" ]]; then + COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) + return 0 + fi + + local subnet="${words[0]}" + local args="${words[@]:1}" + case "${cmd}" in + new) + : # TODO + ;; + remove) + : # TODO + ;; + [0-9]*) + _network_dhcpd_subnet_subcommand "${proto}" "${subnet}" ${args} + ;; + esac +} + +function _network_dhcpd_subnet_subcommand() { + local proto="${1}" + local subnet="${2}" + shift 2 + + local words=( $@ ) + + local commands="edit options range show" + local cmd="$(_network_find_on_cmdline "${commands}")" + if [[ -z "${cmd}" ]]; then + COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) + return 0 + fi + + local args="${words[@]:1}" + case "${cmd}" in + edit) + : # TODO + ;; + options) + _network_dhcpd_subnet_subcommand_options "${proto}" "${subnet}" ${args} + ;; + range) + _network_dhcpd_subnet_subcommand_range "${proto}" "${subnet}" ${args} + ;; + esac +} + +function _network_dhcpd_subnet_subcommand_options() { + local proto="${1}" + local subnet="${2}" + shift 2 + + local options option + for option in $(network raw list-dhcpd-subnet-options "${proto}"); do + options="${options} ${option}=" + done + + COMPREPLY=( $(compgen -W "${options}" -- "${cur}") ) +} + +function _network_dhcpd_subnet_subcommand_range() { + local proto="${1}" + local subnet="${2}" + shift 2 + + local words=( $@ ) + + local commands="new remove" + local cmd="$(_network_find_on_cmdline "${commands}")" + if [[ -z "${cmd}" ]]; then + COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) + return 0 + fi + + case "${cmd}" in + new) + COMPREPLY=( $(compgen -W "--end= --start=" -- "${cur}") ) + ;; + remove) + COMPREPLY=( $(compgen -W "$(network raw list-dhcpd-ranges-of-subnet "${proto}" "${subnet}")" \ + -- "${cur}") ) + ;; + esac +} + function _network_dns_server() { local words=( $@ ) @@ -292,8 +407,9 @@ function _network() { COMPREPLY=( $(compgen -W "--debug" -- "${cur}") ) ;; *) - COMPREPLY=( $(compgen -W "device dns-server help hostname \ - port reset route settings status zone" -- "${cur}") ) + COMPREPLY=( $(compgen -W "device dhcpv4 dhcpv6 dns-server \ + help hostname port reset route settings status zone" \ + -- "${cur}") ) ;; esac @@ -305,6 +421,9 @@ function _network() { device) _network_device ${args} ;; + dhcpv[64]) + _network_dhcpd "${cmd/dhcpv/ipv}" ${args} + ;; dns-server) _network_dns_server ${args} ;; diff --git a/src/functions/functions.dhcpd b/src/functions/functions.dhcpd index 828ad04b..f6732db3 100644 --- a/src/functions/functions.dhcpd +++ b/src/functions/functions.dhcpd @@ -332,6 +332,13 @@ function dhcpd_options() { return ${EXIT_OK} } +function dhcpd_global_settings_list() { + local proto="${1}" + assert isset proto + + dhcpd_settings "${proto}" +} + function dhcpd_global_settings_defaults() { local proto=${1} assert isset proto @@ -842,21 +849,7 @@ function dhcpd_subnet_options_file() { function dhcpd_subnet_options_list() { local proto=${1} - - case "${proto}" in - ipv6) - print "DHCPV6D_SUBNET_OPTIONS" - ;; - ipv4) - print "DHCPV4D_SUBNET_OPTIONS" - ;; - esac - - return ${EXIT_OK} -} - -function dhcpd_subnet_options() { - local proto=${1} + assert isset proto case "${proto}" in ipv6) diff --git a/src/network b/src/network index 01b43fcf..867e3a8b 100644 --- a/src/network +++ b/src/network @@ -996,7 +996,7 @@ function cli_dhcpd_subnet_show() { cli_headline $(( ${level} + 2 )) "Options" local option - for option in $(dhcpd_subnet_options ${proto}); do + for option in $(dhcpd_subnet_options_list ${proto}); do [ -n "${options[${option}]}" ] || continue cli_print_fmt1 $(( ${level} + 2 )) \ @@ -1024,7 +1024,7 @@ function cli_dhcpd_subnet_show() { cli_space } -function cli_dhcpd_options() { +function cli_dhcpd_subnet_options() { local proto=${1} assert isset proto shift @@ -1033,8 +1033,6 @@ function cli_dhcpd_options() { assert isset subnet_id shift - local valid_options=$(dhcpd_subnet_options ${proto}) - local key val while [ $# -gt 0 ]; do case "${1}" in @@ -1253,6 +1251,18 @@ function cli_raw() { list-devices) device_list ;; + list-dhcpd-ranges-of-subnet) + dhcpd_subnet_range_list $@ + ;; + list-dhcpd-settings) + dhcpd_global_settings_list $@ + ;; + list-dhcpd-subnets) + dhcpd_subnet_list $@ + ;; + list-dhcpd-subnet-options) + dhcpd_subnet_options_list $@ + ;; list-dns-servers) dns_server_list ;; -- 2.39.2