[prev in list] [next in list] [prev in thread] [next in thread] 

List:       lnst-developers
Subject:    [PATCH v2] install: Adding bash completion scripts
From:       Radek Pazdera <rpazdera () redhat ! com>
Date:       2013-10-09 8:19:29
Message-ID: 1381306769-20317-1-git-send-email-rpazdera () redhat ! com
[Download RAW message or body]

This commit adds bash completion scripts for both lnst-ctl and
lnst-slave. These scripts have to be installed to

    /etc/bash_completion.d/

in order to work. These procedure was included into the setup.py
installation script that is a part of the package.

Issue #7: https://github.com/jpirko/lnst/issues/7

Signed-off-by: Radek Pazdera <rpazdera@redhat.com>
---
 install/lnst-ctl.bash   | 89 +++++++++++++++++++++++++++++++++++++++++++++++++
 install/lnst-slave.bash | 38 +++++++++++++++++++++
 setup.py                |  7 +++-
 3 files changed, 133 insertions(+), 1 deletion(-)
 create mode 100644 install/lnst-ctl.bash
 create mode 100644 install/lnst-slave.bash

diff --git a/install/lnst-ctl.bash b/install/lnst-ctl.bash
new file mode 100644
index 0000000..05a9b6d
--- /dev/null
+++ b/install/lnst-ctl.bash
@@ -0,0 +1,89 @@
+#!/bin/bash
+
+# Bash completion script for lnst-ctl command
+# Author: Radek Pazdera <rpazdera@redhat.com>
+
+_list_has_item()
+{
+    for entry in $1; do
+        [ "$entry" == "$2" ] && return 0
+    done
+
+    return 1
+}
+
+_lnst_ctl()
+{
+    local SHORT_OPTS="-a -A -c -d -h -m -o -p -x"
+    local LONG_OPTS="--define-alias --override-alias --config --debug \
+                     --help --no-colours --disable-pool-checks \
+                     --packet-capture --result"
+    local REQUIRE_ARG="-a --define-alias \
+                       -A --override-alias \
+                       -c --config \
+                       -x --result"
+    local ACTIONS="config_only match_setup run"
+
+    local cur=${COMP_WORDS[COMP_CWORD]}
+    local prev=${COMP_WORDS[COMP_CWORD-1]}
+
+    # Look for option arguments first
+    case "$prev" in
+        -a|--define-alias) return 0 ;;
+        -A|--override-alias) return 0 ;;
+        -c|--config)
+            _filedir
+            return 0
+            ;;
+        -x|--result)
+            _filedir
+            return 0
+            ;;
+    esac
+
+    # Complete long and shor options
+    if [[ "$cur" == --* ]]; then
+        COMPREPLY=( $(compgen -W "$LONG_OPTS" -- $cur) )
+        [ ${#COMPREPLY[@]} = 1 ] && COMPREPLY=$(printf %q%s "$COMPREPLY" " ")
+        return 0
+    elif [[ "$cur" == -* ]]; then
+        COMPREPLY=( $(compgen -W "$SHORT_OPTS" -- $cur) )
+        [ ${#COMPREPLY[@]} = 1 ] && COMPREPLY=$(printf %q%s "$COMPREPLY" " ")
+        return 0
+    fi
+
+    # Check if the action positional argument has
+    # already been specified somewhere
+    for (( n=1; n < $COMP_CWORD; n++ )); do
+        local word=${COMP_WORDS[n]}
+        local prev_word=${COMP_WORDS[n-1]}
+
+        # Is it an option?
+        if [[ "$word" == --* ]] || [[ "$word" == -* ]]; then
+            continue
+        else
+            # Is the previous word an option that requires and argument?
+            _list_has_item "$REQUIRE_ARG" "$prev_word"
+            if [ $? -eq 0 ]; then
+                continue
+            else
+                # Is the positional argument an action?
+                _list_has_item "$ACTIONS" "$word"
+                if [ $? -eq 0 ]; then
+                    # Action positional argument was found.
+                    # Therefore, we will suggest files.
+                    _filedir
+                    return 0
+                fi
+            fi
+        fi
+    done
+
+    # No action defined yet, we will suggest actions.
+    COMPREPLY=( $(compgen -W "$ACTIONS" -- $cur) )
+    [ ${#COMPREPLY[@]} = 1 ] && COMPREPLY=$(printf %q%s "$COMPREPLY" " ")
+
+    return 0
+}
+
+complete -o nospace -F _lnst_ctl lnst-ctl
diff --git a/install/lnst-slave.bash b/install/lnst-slave.bash
new file mode 100644
index 0000000..d939d7a
--- /dev/null
+++ b/install/lnst-slave.bash
@@ -0,0 +1,38 @@
+#!/bin/bash
+
+# Bash completion script for lnst-slave command
+# Author: Radek Pazdera <rpazdera@redhat.com>
+
+_lnst_slave()
+{
+    local SHORT_OPTS="-d -e -h -i -m -p"
+    local LONG_OPTS="--debug --daemonize --help --pidfile --no-colours --port"
+    local REQUIRE_ARG="-i --pidfile -p --port"
+
+    local cur=${COMP_WORDS[COMP_CWORD]}
+    local prev=${COMP_WORDS[COMP_CWORD-1]}
+
+    # Look for option arguments first
+    case "$prev" in
+        -i|--pidfile)
+            _filedir
+            return 0
+            ;;
+        -p|--port) return 0 ;;
+    esac
+
+    # Complete long and shor options
+    if [[ "$cur" == --* ]]; then
+        COMPREPLY=( $(compgen -W "$LONG_OPTS" -- $cur) )
+        [ ${#COMPREPLY[@]} = 1 ] && COMPREPLY=$(printf %q%s "$COMPREPLY" " ")
+        return 0
+    elif [[ "$cur" == -* ]]; then
+        COMPREPLY=( $(compgen -W "$SHORT_OPTS" -- $cur) )
+        [ ${#COMPREPLY[@]} = 1 ] && COMPREPLY=$(printf %q%s "$COMPREPLY" " ")
+        return 0
+    fi
+
+    return 0
+}
+
+complete -o nospace -F _lnst_slave lnst-slave
diff --git a/setup.py b/setup.py
index 3bb74bd..a54b056 100755
--- a/setup.py
+++ b/setup.py
@@ -41,6 +41,7 @@ def gzip_file(path):
 
 # Various paths
 CONF_DIR = "/etc/"
+BASH_COMP_DIR = CONF_DIR + "bash_completion.d/"
 MAN_DIR = "/usr/share/man/man1/"
 
 CTL_MODULES_LOCATIONS = "/usr/share/lnst/test_modules/"
@@ -147,9 +148,13 @@ MAN_PAGES = [(MAN_DIR, ["install/lnst-ctl.1.gz", "install/lnst-slave.1.gz"])]
 
 CONFIG = [(CONF_DIR, ["install/lnst-ctl.conf", "install/lnst-slave.conf"])]
 
+BASH_COMP = [(BASH_COMP_DIR, ["install/lnst-ctl.bash",
+                              "install/lnst-slave.bash"])]
+
 SCHEMAS = [(CTL_RESOURCE_DIR, ["schema-recipe.rng", "schema-sm.rng"])]
 
-DATA_FILES = CONFIG + TEST_MODULES + MULTICAST_TEST_TOOLS + MAN_PAGES + SCHEMAS
+DATA_FILES = CONFIG + TEST_MODULES + MULTICAST_TEST_TOOLS + MAN_PAGES + \
+             SCHEMAS + BASH_COMP
 
 setup(name="lnst",
     version="git",
-- 
1.8.3.1

_______________________________________________
LNST-developers mailing list
LNST-developers@lists.fedorahosted.org
https://lists.fedorahosted.org/mailman/listinfo/lnst-developers

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic