improve autocompletion of network zone new

Submitted by Jonatan Schlag on June 7, 2017, 6:20 a.m.

Details

Message ID 1496816428-6275-1-git-send-email-jonatan.schlag@ipfire.org
State New
Headers show

Commit Message

Jonatan Schlag June 7, 2017, 6:20 a.m.
Fixes: #11386

Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
---
 src/bash-completion/network  | 22 ++++++++++++++++++++--
 src/functions/functions.zone | 20 ++++++++++++++++++++
 src/network                  |  6 ++++++
 3 files changed, 46 insertions(+), 2 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/bash-completion/network b/src/bash-completion/network
index 6f63f1b..fd74027 100644
--- a/src/bash-completion/network
+++ b/src/bash-completion/network
@@ -299,11 +299,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
@@ -316,6 +316,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 c727506..88c81a8 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 e65eb6b..34faa36 100644
--- a/src/network
+++ b/src/network
@@ -1320,6 +1320,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}