Skip to contentSkip to conversation

nft: add bash completion script

RFC PATCH In review Giuseppe Longo 3 participants 6 messages Feb 5, 2016 – Feb 16, 2016 netfilter

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 nft: add bash completion script

The following patch adds a bash completion script which permits to complete nft commands.

To install it:
- cp files/nft-completion /etc/bash_completion.d/
- . /etc/bash_completion.d/nft-completion

The following commands are supported:
- nft add table <family> <name>
- nft list table <family> <name>
- nft list tables
- nft list sets
- nft list chains
- nft list ruleset
- nft list set <table> <name>
- nft add set <table> <name>
- nft add element <table> <set>
- nft add map <table>
- nft flush table <family> <table>

Most probably this won't work with sudo, since there are some nft commands into the script.

A second patch to add completion in interactive mode will come.

This is only a draft,
any feedback is appreciated.

---
 files/nft-completion | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
 create mode 100644 files/nft-completion
files/nft-completion new file +60 -0
new file mode 100644
index 0000000..c138312
@@ -0,0 +1,60 @@
+_nft()
+{
+    local cur prev
+    COMPREPLY=()
+    cur="${COMP_WORDS[COMP_CWORD]}"
+    prev="${COMP_WORDS[COMP_CWORD-1]}"
+
+    local families="ip ip6 arp bridge inet netdev"
+    local sets=$(nft list sets | grep -i "set" | awk '{print $2 }' | tr -d ' ')
+
+    if [ $COMP_CWORD -eq 1 ]; then
+        COMPREPLY=( $(compgen -W "add flush list" -- $cur) )
+    elif [ $COMP_CWORD -eq 2 ]; then
+        case "$prev" in
+            "add")
+                command="${prev}"
+                COMPREPLY=( $(compgen -W "element map table set" -- $cur) )
+                ;;
+            "list")
+                COMPREPLY=( $(compgen -W "chains ruleset set sets table tables" -- $cur) )
+                ;;
+            "flush")
+                COMPREPLY=( $(compgen -W "table" -- $cur) )
+                ;;
+            *)
+                ;;
+        esac
+    elif [ $COMP_CWORD -eq 3 ]; then
+        case "$prev" in
+            "table")
+                COMPREPLY=( $(compgen -W "${families}" -- $cur) )
+                ;;
+            "set"|"element"|"map")
+                local tables=$(nft list tables | awk '{print $3 }' | tr -d ' ')
+                COMPREPLY=( $(compgen -W "${tables}" -- $cur) )
+                ;;
+            *)
+                ;;
+        esac
+    elif [ $COMP_CWORD -eq 4 ]; then
+        local tables=$(nft list tables | awk '{print $3 }' | tr -d ' ')
+        if [[ "$families" =~ "$prev" ]]; then 
+            local tables=$(nft list tables "${prev}" | awk '{print $3 }' | tr -d ' ')
+            COMPREPLY=( $(compgen -W "${tables}" -- $cur) )
+        fi
+
+        if [ "${COMP_WORDS[1]}" != "add" ] && [[ "$tables" =~ "$prev" ]]; then
+            COMPREPLY=( $(compgen -W "${sets}" -- $cur) )
+        fi
+
+        if [ "${COMP_WORDS[1]}" == "add" ] && [ "${COMP_WORDS[2]}" == "element" ] && [[ "$tables" =~ "$prev" ]]; then
+            COMPREPLY=( $(compgen -W "${sets}" -- $cur) )
+        fi
+    fi
+
+    return 0;
+}
+
+complete -F _nft nft
+
Signature · 1 line
2.5.0
#1 lore raw
Quoted 2 lines · Giuseppe Longo

The following patch adds a bash completion script which permits to complete nft commands.

Hello,

ok you've been quicker than me. Now that I'm quite done with the iptables completion:
https://sourceforge.net/projects/ipt-bashcompl
https://github.com/AllKind/iptables-bash_completion
I wanted to start with nft... But well, you might find one or the other piece of code useful to reuse i.e. retrieving interface names.

Quoted 18 lines

To install it:
- cp files/nft-completion /etc/bash_completion.d/
- . /etc/bash_completion.d/nft-completion

The following commands are supported:
- nft add table <family> <name>
- nft list table <family> <name>
- nft list tables
- nft list sets
- nft list chains
- nft list ruleset
- nft list set <table> <name>
- nft add set <table> <name>
- nft add element <table> <set>
- nft add map <table>
- nft flush table <family> <table>

Quoted 2 lines

Most probably this won't work with sudo, since there are some nft commands into the script.

Untested, but from what I read in the bash_completion package (2.1), it's capable of loading the completion for the command after sudo (using the function _command_offset() ).

Quoted 19 lines

A second patch to add completion in interactive mode will come.

This is only a draft,
any feedback is appreciated.

---
  files/nft-completion | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 60 insertions(+)

create mode 100644 files/nft-completion

files/nft-completion new file +0 -0
new file mode 100644
index 0000000..c138312

files/nft-completion line 7

Quoted 7 lines
files/nft-completion new file +7 -0
@@ -0,0 +1,60 @@
+_nft()
+{
+    local cur prev
+    COMPREPLY=()
+    cur="${COMP_WORDS[COMP_CWORD]}"
+    prev="${COMP_WORDS[COMP_CWORD-1]}"
+

Are you planing to make use of the functionality of the bash_completion package? As it may contain some useful stuff.

Quoted 10 lines
files/nft-completion new file +10 -0
+    local families="ip ip6 arp bridge inet netdev"
+    local sets=$(nft list sets | grep -i "set" | awk '{print $2 }' | tr -d ' ')
+
+    if [ $COMP_CWORD -eq 1 ]; then
+        COMPREPLY=( $(compgen -W "add flush list" -- $cur) )
+    elif [ $COMP_CWORD -eq 2 ]; then
+        case "$prev" in
+            "add")
+                command="${prev}"
+                COMPREPLY=( $(compgen -W "element map table set" -- $cur) )

files/nft-completion line 27

Quoted 10 lines
files/nft-completion new file +10 -0
+                ;;
+            "list")
+                COMPREPLY=( $(compgen -W "chains ruleset set sets table tables" -- $cur) )
+                ;;
+            "flush")
+                COMPREPLY=( $(compgen -W "table" -- $cur) )
+                ;;
+            *)
+                ;;
+        esac

Using case' statements makes it harder to debug, as bash does not show which case it matched. Using if' `elif' improves this. Also this is one of the coding style guidelines of bash_completion, in case you ever want it to go there.

Quoted 10 lines
files/nft-completion new file +10 -0
+    elif [ $COMP_CWORD -eq 3 ]; then
+        case "$prev" in
+            "table")
+                COMPREPLY=( $(compgen -W "${families}" -- $cur) )
+                ;;
+            "set"|"element"|"map")
+                local tables=$(nft list tables | awk '{print $3 }' | tr -d ' ')
+                COMPREPLY=( $(compgen -W "${tables}" -- $cur) )
+                ;;
+            *)

files/nft-completion line 47

Quoted 10 lines
files/nft-completion new file +10 -0
+                ;;
+        esac
+    elif [ $COMP_CWORD -eq 4 ]; then
+        local tables=$(nft list tables | awk '{print $3 }' | tr -d ' ')
+        if [[ "$families" =~ "$prev" ]]; then
+            local tables=$(nft list tables "${prev}" | awk '{print $3 }' | tr -d ' ')
+            COMPREPLY=( $(compgen -W "${tables}" -- $cur) )
+        fi
+
+        if [ "${COMP_WORDS[1]}" != "add" ] && [[ "$tables" =~ "$prev" ]]; then

Probably you can save some typing with:
[[ a != b && x =~ y ]]

Quoted 4 lines
files/nft-completion new file +3 -0
+            COMPREPLY=( $(compgen -W "${sets}" -- $cur) )
+        fi
+

files/nft-completion line 60

Quoted 10 lines
files/nft-completion new file +10 -0
+        if [ "${COMP_WORDS[1]}" == "add" ] && [ "${COMP_WORDS[2]}" == "element" ] && [[ "$tables" =~ "$prev" ]]; then
+            COMPREPLY=( $(compgen -W "${sets}" -- $cur) )
+        fi
+    fi
+
+    return 0;
+}
+
+complete -F _nft nft
+

Good day,
AllKind

#2 lore raw
In reply to Giuseppe Longo
Quoted 6 lines · Giuseppe Longo

The following patch adds a bash completion script
which permits to complete nft commands.

To install it:
- cp files/nft-completion /etc/bash_completion.d/
- . /etc/bash_completion.d/nft-completion

[...]

Another thing - reading the README of bash completion 2.1, this behavior is deprecated and just kept for backwards compatibility. Since 2.0 dynamic loading of completion is supported and quoting the readme it should be done like this:

Q. I author/maintain package X and would like to maintain my own
completion code for this package. Where should I put it to be sure that interactive bash shells will find it and source it?

Install it in one of the directories pointed to by bash-completion's pkgconfig file variables. There are two
alternatives: the recommended one is 'completionsdir' (get it with "pkg-config --variable=completionsdir bash-completion") from which completions are loaded on demand based on invoked commands' names, so be sure to name your completion file accordingly, and to include for example symbolic links in case the file provides completions for more than one command. The other one which is present for backwards compatibility reasons is 'compatdir' (get it with "pkg-config --variable=compatdir bash-completion") from which files are loaded when bash_completion is loaded.

For packages using GNU autotools the installation can be handled for example like this in configure.ac:

PKG_CHECK_VAR(bashcompdir, [bash-completion], [completionsdir], ,
bashcompdir="${sysconfdir}/bash_completion.d")
AC_SUBST(bashcompdir)

...accompanied by this in Makefile.am:

bashcompdir = @bashcompdir@ dist_bashcomp_DATA = # completion files go here

For cmake we ship the bash-completion-config.cmake and bash-completion-config-version.cmake files. Example usage:

find_package(bash-completion)
if(BASH_COMPLETION_FOUND)
message(STATUS
"Using bash completion dir ${BASH_COMPLETION_COMPLETIONSDIR}")
else()
set (BASH_COMPLETION_COMPLETIONSDIR "/etc/bash_completion.d") message (STATUS
"Using fallback bash completion dir
${BASH_COMPLETION_COMPLETIONSDIR}")

     endif()

     install(FILES your-completion-file DESTINATION
       ${BASH_COMPLETION_COMPLETIONSDIR})

Best regards,
AllKind

#3 lore raw
In reply to Giuseppe Longo

Hi Giuseppe,

Quoted 20 lines · Giuseppe Longo

The following patch adds a bash completion script which permits to complete nft commands.

To install it:
- cp files/nft-completion /etc/bash_completion.d/
- . /etc/bash_completion.d/nft-completion

The following commands are supported:
- nft add table <family> <name>
- nft list table <family> <name>
- nft list tables
- nft list sets
- nft list chains
- nft list ruleset
- nft list set <table> <name>
- nft add set <table> <name>
- nft add element <table> <set>
- nft add map <table>
- nft flush table <family> <table>

Quoted 8 lines

Most probably this won't work with sudo, since there are some nft commands into the script.

A second patch to add completion in interactive mode will come.

This is only a draft,
any feedback is appreciated.

Could you explore adding code to inquire the parser on the next possible tokens ahead? That would greatly simplify the shell script for bash autocompletion. We should be able to reuse this from libreadline too.

I know what I'm asking is a bit more difficult, but it would help us reduce the amount of code duplication and will reduce the maintainance burden.

Let us know, thanks!

#4 lore raw

Hi Pablo,

2016-02-15 20:56 GMT+01:00 Pablo Neira Ayuso <[email protected]>:

Quoted 37 lines

Hi Giuseppe,

Quoted 20 lines · Giuseppe Longo

The following patch adds a bash completion script which permits to complete nft commands.

To install it:
- cp files/nft-completion /etc/bash_completion.d/
- . /etc/bash_completion.d/nft-completion

The following commands are supported:
- nft add table <family> <name>
- nft list table <family> <name>
- nft list tables
- nft list sets
- nft list chains
- nft list ruleset
- nft list set <table> <name>
- nft add set <table> <name>
- nft add element <table> <set>
- nft add map <table>
- nft flush table <family> <table>

Quoted 8 lines

Most probably this won't work with sudo, since there are some nft commands into the script.

A second patch to add completion in interactive mode will come.

This is only a draft,
any feedback is appreciated.

Could you explore adding code to inquire the parser on the next possible tokens ahead? That would greatly simplify the shell script for bash autocompletion. We should be able to reuse this from libreadline too.

Quoted 5 lines

I know what I'm asking is a bit more difficult, but it would help us reduce the amount of code duplication and will reduce the maintainance burden.

Let us know, thanks!

Sure, I've looked quickly at bison doc and thought the following idea:
We can add a new nft command like "nft complete" to inquire the parser, which will print all the possible word since we have a parse tree.

in that way, the bash script will be able to complete the command.

Also, do you confirm that we will skip objects like tables name, chain name, and so on?

Thanks.

#5 lore raw
Quoted 44 lines · Giuseppe Longo

Hi Pablo,

2016-02-15 20:56 GMT+01:00 Pablo Neira Ayuso <[email protected]>:

Quoted 41 lines

Hi Giuseppe,

Quoted 20 lines · Giuseppe Longo

The following patch adds a bash completion script which permits to complete nft commands.

To install it:
- cp files/nft-completion /etc/bash_completion.d/
- . /etc/bash_completion.d/nft-completion

The following commands are supported:
- nft add table <family> <name>
- nft list table <family> <name>
- nft list tables
- nft list sets
- nft list chains
- nft list ruleset
- nft list set <table> <name>
- nft add set <table> <name>
- nft add element <table> <set>
- nft add map <table>
- nft flush table <family> <table>

Quoted 8 lines

Most probably this won't work with sudo, since there are some nft commands into the script.

A second patch to add completion in interactive mode will come.

This is only a draft,
any feedback is appreciated.

Could you explore adding code to inquire the parser on the next possible tokens ahead? That would greatly simplify the shell script for bash autocompletion. We should be able to reuse this from libreadline too.

I know what I'm asking is a bit more difficult, but it would help us reduce the amount of code duplication and will reduce the maintainance burden.

Quoted 5 lines
Quoted 1 line

Let us know, thanks!

Sure, I've looked quickly at bison doc and thought the following idea:
We can add a new nft command like "nft complete" to inquire the parser, which will print all the possible word since we have a parse tree.

Sounds reasonable. Let me see if I can come up with a better command name, but this naming detail should not stop you at this stage.

Quoted 4 lines

in that way, the bash script will be able to complete the command.

Also, do you confirm that we will skip objects like tables name, chain name, and so on?

Yes, let's focus on keyword autocompletion by now.

Thanks.