summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Foster <tim.foster@joyent.com>2019-08-08 16:37:16 +0100
committerTim Foster <tim.foster@joyent.com>2019-08-09 13:49:17 +0100
commit5c0ce8a8f48494563f0e4436f757e51f348401ee (patch)
tree669914a1394ff4cf2e889310885a50e35192a796
parentcf8b065c913dcf6e5137334f49de9168e2b76f19 (diff)
downloadillumos-joyent-grr-OS-7921.tar.gz
OS-7921 SmartOS bash_completions should complete alias namesgrr-OS-7921
-rw-r--r--manifest1
-rw-r--r--usr/src/cmd/nsadmin/Makefile2
-rw-r--r--usr/src/cmd/nsadmin/bash/bash_completion.d/zone_alias50
-rw-r--r--usr/src/cmd/nsadmin/bash/bash_completion.d/zones9
4 files changed, 61 insertions, 1 deletions
diff --git a/manifest b/manifest
index 19d79fec68..79627bb02f 100644
--- a/manifest
+++ b/manifest
@@ -268,6 +268,7 @@ f etc/bash/bash_completion 0644 root sys
d etc/bash/bash_completion.d 0755 root sys
f etc/bash/bash_completion.d/dladm 0644 root sys
f etc/bash/bash_completion.d/zones 0644 root sys
+f etc/bash/bash_completion.d/zone_alias 0644 root sys
d etc/brand 0755 root sys
d etc/certs 0755 root sys
d etc/cron.d 0755 root sys
diff --git a/usr/src/cmd/nsadmin/Makefile b/usr/src/cmd/nsadmin/Makefile
index 819a69340c..3af33b2a4d 100644
--- a/usr/src/cmd/nsadmin/Makefile
+++ b/usr/src/cmd/nsadmin/Makefile
@@ -28,7 +28,7 @@
ETCFILES = profile .login ksh.kshrc system zshrc
ETCSKELFILES = .profile .kshrc .bashrc
ETCBASHFILES = bash_completion
-ETCBASHCOMPLETIONDFILES = dladm zones
+ETCBASHCOMPLETIONDFILES = dladm zones zone_alias
ROOTFILES = .profile .bashrc .bash_profile
include ../Makefile.cmd
diff --git a/usr/src/cmd/nsadmin/bash/bash_completion.d/zone_alias b/usr/src/cmd/nsadmin/bash/bash_completion.d/zone_alias
new file mode 100644
index 0000000000..302c0f873e
--- /dev/null
+++ b/usr/src/cmd/nsadmin/bash/bash_completion.d/zone_alias
@@ -0,0 +1,50 @@
+_zone_alias()
+{
+ # Attempt alias -> uuid mapping
+ alias="${COMP_WORDS[COMP_CWORD]}"
+ uuid=$(vmadm lookup alias="$alias")
+ if [[ -n "$uuid" ]]; then
+ _describe_completion "$alias"
+ COMPREPLY=$uuid
+ return 0
+ fi
+
+ # not an exact match, lookup options
+ uuid=$(vmadm lookup alias=~"^$alias")
+ count=$(echo $uuid | wc -w | sed -e 's/ //g')
+
+ if [[ $count -eq 1 ]]; then
+ full_alias="$(vmadm get $uuid | json alias)"
+ _describe_completion "$full_alias"
+ COMPREPLY=$uuid
+ return 0
+ else
+ # inconclusive results, return alias matches, or
+ # the uuid
+ aliases=$(vmadm list | grep " $alias" | awk '{print $NF}')
+ count=$(echo $aliases | wc -w | sed -e 's/ //g')
+ if [[ $count -eq 1 ]]; then
+ uuid=$(vmadm lookup alias=$aliases)
+ _describe_completion "$aliases"
+ if [[ -n "$SMARTOS_MULTILINE_ALIAS_COMPLETION" ]]; then
+ COMPREPLY=$uuid
+ else
+ COMPREPLY=$aliases
+ fi
+ return 0
+ else
+ COMPREPLY=( $(compgen -W "$aliases" -- ${cur}) )
+ fi
+ fi
+}
+
+function _describe_completion() {
+ local alias=$1
+ if [[ -n "$SMARTOS_MULTILINE_ALIAS_COMPLETION" ]]; then
+ tput sc
+ tput bold
+ echo -en "[completed alias: ${alias}]\n"
+ tput rc
+ tput sgr0
+ fi
+}
diff --git a/usr/src/cmd/nsadmin/bash/bash_completion.d/zones b/usr/src/cmd/nsadmin/bash/bash_completion.d/zones
index 2c68ba366e..e4bc5b4155 100644
--- a/usr/src/cmd/nsadmin/bash/bash_completion.d/zones
+++ b/usr/src/cmd/nsadmin/bash/bash_completion.d/zones
@@ -10,6 +10,11 @@ _zlogin()
local running=$(zoneadm list | grep -v '^global$')
COMPREPLY=( $(compgen -W "${running}" -- ${cur}) )
fi
+ if [[ -n "$COMPREPLY" ]]; then
+ return 0
+ fi
+
+ _zone_alias
return 0
}
@@ -24,6 +29,10 @@ _dash_z_zone()
if [[ ${prev} =~ -.*z$ ]]; then
local zones=$(zoneadm list -c | grep -v '^global$')
COMPREPLY=( $(compgen -W "${zones}" -- ${cur}) )
+ if [[ -n "$COMPREPLY" ]]; then
+ return 0
+ fi
+ _zone_alias
fi
return 0
}