netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC nftables PATCH] nft: add bash completion script
@ 2016-02-05 16:53 Giuseppe Longo
  2016-02-06  7:56 ` AllKind
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Giuseppe Longo @ 2016-02-05 16:53 UTC (permalink / raw)
  To: netfilter-devel; +Cc: 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>

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.

Signed-off-by: Giuseppe Longo <giuseppelng@gmail.com>
---
 files/nft-completion | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
 create mode 100644 files/nft-completion

diff --git a/files/nft-completion b/files/nft-completion
new file mode 100644
index 0000000..c138312
--- /dev/null
+++ b/files/nft-completion
@@ -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
+
-- 
2.5.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [RFC nftables PATCH] nft: add bash completion script
  2016-02-05 16:53 [RFC nftables PATCH] nft: add bash completion script Giuseppe Longo
@ 2016-02-06  7:56 ` AllKind
  2016-02-06 20:13 ` AllKind
  2016-02-15 19:56 ` Pablo Neira Ayuso
  2 siblings, 0 replies; 6+ messages in thread
From: AllKind @ 2016-02-06  7:56 UTC (permalink / raw)
  To: Giuseppe Longo; +Cc: netfilter-devel

On 05.02.2016 17:53, Giuseppe Longo wrote:
> 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.
>
> 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.

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() ).
>
> A second patch to add completion in interactive mode
> will come.
>
> This is only a draft,
> any feedback is appreciated.
>
> Signed-off-by: Giuseppe Longo <giuseppelng@gmail.com>
> ---
>   files/nft-completion | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 60 insertions(+)
>   create mode 100644 files/nft-completion
>
> diff --git a/files/nft-completion b/files/nft-completion
> new file mode 100644
> index 0000000..c138312
> --- /dev/null
> +++ b/files/nft-completion
> @@ -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.

> +    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

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.

> +    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

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

> +            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
> +
>

Good day,
AllKind


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC nftables PATCH] nft: add bash completion script
  2016-02-05 16:53 [RFC nftables PATCH] nft: add bash completion script Giuseppe Longo
  2016-02-06  7:56 ` AllKind
@ 2016-02-06 20:13 ` AllKind
  2016-02-15 19:56 ` Pablo Neira Ayuso
  2 siblings, 0 replies; 6+ messages in thread
From: AllKind @ 2016-02-06 20:13 UTC (permalink / raw)
  To: Giuseppe Longo; +Cc: netfilter-devel

On 05.02.2016 17:53, Giuseppe Longo wrote:
> 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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC nftables PATCH] nft: add bash completion script
  2016-02-05 16:53 [RFC nftables PATCH] nft: add bash completion script Giuseppe Longo
  2016-02-06  7:56 ` AllKind
  2016-02-06 20:13 ` AllKind
@ 2016-02-15 19:56 ` Pablo Neira Ayuso
  2016-02-16 13:00   ` Giuseppe Longo
  2 siblings, 1 reply; 6+ messages in thread
From: Pablo Neira Ayuso @ 2016-02-15 19:56 UTC (permalink / raw)
  To: Giuseppe Longo; +Cc: netfilter-devel

Hi Giuseppe,

On Fri, Feb 05, 2016 at 05:53:02PM +0100, Giuseppe Longo wrote:
> 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.

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!

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC nftables PATCH] nft: add bash completion script
  2016-02-15 19:56 ` Pablo Neira Ayuso
@ 2016-02-16 13:00   ` Giuseppe Longo
  2016-02-16 16:26     ` Pablo Neira Ayuso
  0 siblings, 1 reply; 6+ messages in thread
From: Giuseppe Longo @ 2016-02-16 13:00 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: netfilter-devel@vger.kernel.org

Hi Pablo,

2016-02-15 20:56 GMT+01:00 Pablo Neira Ayuso <pablo@netfilter.org>:
> Hi Giuseppe,
>
> On Fri, Feb 05, 2016 at 05:53:02PM +0100, Giuseppe Longo wrote:
>> 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.
>
> 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!

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.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC nftables PATCH] nft: add bash completion script
  2016-02-16 13:00   ` Giuseppe Longo
@ 2016-02-16 16:26     ` Pablo Neira Ayuso
  0 siblings, 0 replies; 6+ messages in thread
From: Pablo Neira Ayuso @ 2016-02-16 16:26 UTC (permalink / raw)
  To: Giuseppe Longo; +Cc: netfilter-devel@vger.kernel.org

On Tue, Feb 16, 2016 at 02:00:17PM +0100, Giuseppe Longo wrote:
> Hi Pablo,
> 
> 2016-02-15 20:56 GMT+01:00 Pablo Neira Ayuso <pablo@netfilter.org>:
> > Hi Giuseppe,
> >
> > On Fri, Feb 05, 2016 at 05:53:02PM +0100, Giuseppe Longo wrote:
> >> 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.
> >
> > 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!
> 
> 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.

> 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.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-02-16 16:26 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-02-05 16:53 [RFC nftables PATCH] nft: add bash completion script Giuseppe Longo
2016-02-06  7:56 ` AllKind
2016-02-06 20:13 ` AllKind
2016-02-15 19:56 ` Pablo Neira Ayuso
2016-02-16 13:00   ` Giuseppe Longo
2016-02-16 16:26     ` Pablo Neira Ayuso

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).