Commands that can only work on non-root cells have no cells to list if
just the root cell is running. Setting nullglob while listing and
returning early if list is empty resolves this. We can even simplify
the code.

Signed-off-by: Jan Kiszka <[email protected]>
---
 tools/jailhouse-completion.bash | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/tools/jailhouse-completion.bash b/tools/jailhouse-completion.bash
index 4592413..3c5949f 100644
--- a/tools/jailhouse-completion.bash
+++ b/tools/jailhouse-completion.bash
@@ -68,13 +68,19 @@ function _jailhouse_get_id() {
        # if we are at position 3 of the commnadline we can either input a
        # concrete `ID`/`NAME` or the option `--name`
        if [ "${COMP_CWORD}" -eq 3 ]; then
+               shopt -q nullglob && nullglob_set=true
+               shopt -s nullglob
 
                # get possible ids and names
-               if [ -d /sys/devices/jailhouse/cells ]; then
-                       for i in ${cells}; do
-                               ids="${ids} ${i##*/}"
-                               names="${names} $(<${i}/name)"
-                       done
+               for i in ${cells}; do
+                       ids="${ids} ${i##*/}"
+                       names="${names} $(<${i}/name)"
+               done
+
+               [ ! $nullglob_set ] && shopt -u nullglob
+
+               if [ "${ids}" == "" ]; then
+                       return 1;
                fi
 
                COMPREPLY=( $( compgen -W "--name ${ids} ${names}" -- \
@@ -85,12 +91,15 @@ function _jailhouse_get_id() {
        elif [ "${COMP_CWORD}" -eq 4 ]; then
                [ "${prev}" = "--name" ] || return 1
 
+               shopt -q nullglob && nullglob_set=true
+               shopt -s nullglob
+
                # get possible names
-               if [ -d /sys/devices/jailhouse/cells ]; then
-                       for n in ${cells}; do
-                               names="${names} $(<${n})"
-                       done
-               fi
+               for n in ${cells}; do
+                       names="${names} $(<${n})"
+               done
+
+               [ ! $nullglob_set ] && shopt -u nullglob
 
                COMPREPLY=( $( compgen -W "${names}" -- ${quoted_cur} ) )
 
-- 
2.1.4

-- 
You received this message because you are subscribed to the Google Groups 
"Jailhouse" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to