diff mbox series

[bpf-next,1/7] tools: bpftool: slightly ease bash completion updates

Message ID 20210729162932.30365-2-quentin@isovalent.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series tools: bpftool: update, synchronise and | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf-next
netdev/subject_prefix success Link
netdev/cc_maintainers warning 6 maintainers not CCed: yhs@fb.com kpsingh@kernel.org kafai@fb.com john.fastabend@gmail.com songliubraving@fb.com liujian56@huawei.com
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 104 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link

Commit Message

Quentin Monnet July 29, 2021, 4:29 p.m. UTC
Bash completion for bpftool gets two minor improvements in this patch.

Move the detection of attach types for "bpftool cgroup attach" outside
of the "case/esac" bloc, where we cannot reuse our variable holding the
list of supported attach types as a pattern list. After the change, we
have only one list of cgroup attach types to update when new types are
added, instead of the former two lists.

Also rename the variables holding lists of names for program types, map
types, and attach types, to make them more unique. This can make it
slightly easier to point people to the relevant variables to update, but
the main objective here is to help run a script to check that bash
completion is up-to-date with bpftool's source code.

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
---
 tools/bpf/bpftool/bash-completion/bpftool | 57 +++++++++++++----------
 1 file changed, 32 insertions(+), 25 deletions(-)

Comments

Andrii Nakryiko July 30, 2021, 6:45 p.m. UTC | #1
On Thu, Jul 29, 2021 at 9:29 AM Quentin Monnet <quentin@isovalent.com> wrote:
>
> Bash completion for bpftool gets two minor improvements in this patch.
>
> Move the detection of attach types for "bpftool cgroup attach" outside
> of the "case/esac" bloc, where we cannot reuse our variable holding the
> list of supported attach types as a pattern list. After the change, we
> have only one list of cgroup attach types to update when new types are
> added, instead of the former two lists.
>
> Also rename the variables holding lists of names for program types, map
> types, and attach types, to make them more unique. This can make it
> slightly easier to point people to the relevant variables to update, but
> the main objective here is to help run a script to check that bash
> completion is up-to-date with bpftool's source code.
>
> Signed-off-by: Quentin Monnet <quentin@isovalent.com>
> ---
>  tools/bpf/bpftool/bash-completion/bpftool | 57 +++++++++++++----------
>  1 file changed, 32 insertions(+), 25 deletions(-)
>
> diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool
> index cc33c5824a2f..b2e33a2d8524 100644
> --- a/tools/bpf/bpftool/bash-completion/bpftool
> +++ b/tools/bpf/bpftool/bash-completion/bpftool
> @@ -404,8 +404,10 @@ _bpftool()
>                              return 0
>                              ;;
>                          5)
> -                            COMPREPLY=( $( compgen -W 'msg_verdict stream_verdict \
> -                                stream_parser flow_dissector' -- "$cur" ) )
> +                            local BPFTOOL_PROG_ATTACH_TYPES='msg_verdict \
> +                                stream_verdict stream_parser flow_dissector'
> +                            COMPREPLY=( $( compgen -W \
> +                                "$BPFTOOL_PROG_ATTACH_TYPES" -- "$cur" ) )
>                              return 0
>                              ;;
>                          6)
> @@ -464,7 +466,7 @@ _bpftool()
>
>                      case $prev in
>                          type)
> -                            COMPREPLY=( $( compgen -W "socket kprobe \
> +                            local BPFTOOL_PROG_LOAD_TYPES='socket kprobe \
>                                  kretprobe classifier flow_dissector \
>                                  action tracepoint raw_tracepoint \
>                                  xdp perf_event cgroup/skb cgroup/sock \
> @@ -479,8 +481,9 @@ _bpftool()
>                                  cgroup/post_bind4 cgroup/post_bind6 \
>                                  cgroup/sysctl cgroup/getsockopt \
>                                  cgroup/setsockopt cgroup/sock_release struct_ops \
> -                                fentry fexit freplace sk_lookup" -- \
> -                                                   "$cur" ) )
> +                                fentry fexit freplace sk_lookup'
> +                            COMPREPLY=( $( compgen -W \
> +                                "$BPFTOOL_PROG_LOAD_TYPES" -- "$cur" ) )

nit: this and similar COMPREPLY assignments now can be on a single line now, no?

>                              return 0
>                              ;;
>                          id)
> @@ -698,15 +701,16 @@ _bpftool()
>                              return 0
>                              ;;
>                          type)
> -                            COMPREPLY=( $( compgen -W 'hash array prog_array \
> -                                perf_event_array percpu_hash percpu_array \
> -                                stack_trace cgroup_array lru_hash \
> +                            local BPFTOOL_MAP_CREATE_TYPES='hash array \
> +                                prog_array perf_event_array percpu_hash \
> +                                percpu_array stack_trace cgroup_array lru_hash \
>                                  lru_percpu_hash lpm_trie array_of_maps \
>                                  hash_of_maps devmap devmap_hash sockmap cpumap \
>                                  xskmap sockhash cgroup_storage reuseport_sockarray \
>                                  percpu_cgroup_storage queue stack sk_storage \
> -                                struct_ops inode_storage task_storage' -- \
> -                                                   "$cur" ) )
> +                                struct_ops inode_storage task_storage'
> +                            COMPREPLY=( $( compgen -W \
> +                                "$BPFTOOL_MAP_CREATE_TYPES" -- "$cur" ) )
>                              return 0
>                              ;;
>                          key|value|flags|entries)
> @@ -1017,34 +1021,37 @@ _bpftool()
>                      return 0
>                      ;;
>                  attach|detach)
> -                    local ATTACH_TYPES='ingress egress sock_create sock_ops \
> -                        device bind4 bind6 post_bind4 post_bind6 connect4 connect6 \
> +                    local BPFTOOL_CGROUP_ATTACH_TYPES='ingress egress \
> +                        sock_create sock_ops device \
> +                        bind4 bind6 post_bind4 post_bind6 connect4 connect6 \
>                          getpeername4 getpeername6 getsockname4 getsockname6 \
>                          sendmsg4 sendmsg6 recvmsg4 recvmsg6 sysctl getsockopt \
>                          setsockopt sock_release'
>                      local ATTACH_FLAGS='multi override'
>                      local PROG_TYPE='id pinned tag name'
> -                    case $prev in
> -                        $command)
> -                            _filedir
> -                            return 0
> -                            ;;
> -                        ingress|egress|sock_create|sock_ops|device|bind4|bind6|\
> -                        post_bind4|post_bind6|connect4|connect6|getpeername4|\
> -                        getpeername6|getsockname4|getsockname6|sendmsg4|sendmsg6|\
> -                        recvmsg4|recvmsg6|sysctl|getsockopt|setsockopt|sock_release)
> +                    # Check for $prev = $command first
> +                    if [ $prev = $command ]; then
> +                        _filedir
> +                        return 0
> +                    # Then check for attach type. This is done outside of the
> +                    # "case $prev in" to avoid writing the whole list of attach
> +                    # types again as pattern to match (where we cannot reuse
> +                    # our variable).
> +                    elif [[ $BPFTOOL_CGROUP_ATTACH_TYPES =~ $prev ]]; then
>                              COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
>                                  "$cur" ) )
>                              return 0
> -                            ;;
> +                    fi
> +                    # case/esac for the other cases
> +                    case $prev in
>                          id)
>                              _bpftool_get_prog_ids
>                              return 0
>                              ;;
>                          *)
> -                            if ! _bpftool_search_list "$ATTACH_TYPES"; then
> -                                COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- \
> -                                    "$cur" ) )
> +                            if ! _bpftool_search_list "$BPFTOOL_CGROUP_ATTACH_TYPES"; then
> +                                COMPREPLY=( $( compgen -W \
> +                                    "$BPFTOOL_CGROUP_ATTACH_TYPES" -- "$cur" ) )
>                              elif [[ "$command" == "attach" ]]; then
>                                  # We have an attach type on the command line,
>                                  # but it is not the previous word, or
> --
> 2.30.2
>
Quentin Monnet July 30, 2021, 9:46 p.m. UTC | #2
2021-07-30 11:45 UTC-0700 ~ Andrii Nakryiko <andrii.nakryiko@gmail.com>
> On Thu, Jul 29, 2021 at 9:29 AM Quentin Monnet <quentin@isovalent.com> wrote:
>>
>> Bash completion for bpftool gets two minor improvements in this patch.
>>
>> Move the detection of attach types for "bpftool cgroup attach" outside
>> of the "case/esac" bloc, where we cannot reuse our variable holding the
>> list of supported attach types as a pattern list. After the change, we
>> have only one list of cgroup attach types to update when new types are
>> added, instead of the former two lists.
>>
>> Also rename the variables holding lists of names for program types, map
>> types, and attach types, to make them more unique. This can make it
>> slightly easier to point people to the relevant variables to update, but
>> the main objective here is to help run a script to check that bash
>> completion is up-to-date with bpftool's source code.
>>
>> Signed-off-by: Quentin Monnet <quentin@isovalent.com>
>> ---
>>  tools/bpf/bpftool/bash-completion/bpftool | 57 +++++++++++++----------
>>  1 file changed, 32 insertions(+), 25 deletions(-)
>>
>> diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool
>> index cc33c5824a2f..b2e33a2d8524 100644
>> --- a/tools/bpf/bpftool/bash-completion/bpftool
>> +++ b/tools/bpf/bpftool/bash-completion/bpftool
>> @@ -404,8 +404,10 @@ _bpftool()
>>                              return 0
>>                              ;;
>>                          5)
>> -                            COMPREPLY=( $( compgen -W 'msg_verdict stream_verdict \
>> -                                stream_parser flow_dissector' -- "$cur" ) )
>> +                            local BPFTOOL_PROG_ATTACH_TYPES='msg_verdict \
>> +                                stream_verdict stream_parser flow_dissector'
>> +                            COMPREPLY=( $( compgen -W \
>> +                                "$BPFTOOL_PROG_ATTACH_TYPES" -- "$cur" ) )
>>                              return 0
>>                              ;;
>>                          6)
>> @@ -464,7 +466,7 @@ _bpftool()
>>
>>                      case $prev in
>>                          type)
>> -                            COMPREPLY=( $( compgen -W "socket kprobe \
>> +                            local BPFTOOL_PROG_LOAD_TYPES='socket kprobe \
>>                                  kretprobe classifier flow_dissector \
>>                                  action tracepoint raw_tracepoint \
>>                                  xdp perf_event cgroup/skb cgroup/sock \
>> @@ -479,8 +481,9 @@ _bpftool()
>>                                  cgroup/post_bind4 cgroup/post_bind6 \
>>                                  cgroup/sysctl cgroup/getsockopt \
>>                                  cgroup/setsockopt cgroup/sock_release struct_ops \
>> -                                fentry fexit freplace sk_lookup" -- \
>> -                                                   "$cur" ) )
>> +                                fentry fexit freplace sk_lookup'
>> +                            COMPREPLY=( $( compgen -W \
>> +                                "$BPFTOOL_PROG_LOAD_TYPES" -- "$cur" ) )
> 
> nit: this and similar COMPREPLY assignments now can be on a single line now, no?

It will go over 80 characters, but OK, it will probably be more readable
on a single line. I'll change for v2.
Andrii Nakryiko July 30, 2021, 9:54 p.m. UTC | #3
On Fri, Jul 30, 2021 at 2:47 PM Quentin Monnet <quentin@isovalent.com> wrote:
>
> 2021-07-30 11:45 UTC-0700 ~ Andrii Nakryiko <andrii.nakryiko@gmail.com>
> > On Thu, Jul 29, 2021 at 9:29 AM Quentin Monnet <quentin@isovalent.com> wrote:
> >>
> >> Bash completion for bpftool gets two minor improvements in this patch.
> >>
> >> Move the detection of attach types for "bpftool cgroup attach" outside
> >> of the "case/esac" bloc, where we cannot reuse our variable holding the
> >> list of supported attach types as a pattern list. After the change, we
> >> have only one list of cgroup attach types to update when new types are
> >> added, instead of the former two lists.
> >>
> >> Also rename the variables holding lists of names for program types, map
> >> types, and attach types, to make them more unique. This can make it
> >> slightly easier to point people to the relevant variables to update, but
> >> the main objective here is to help run a script to check that bash
> >> completion is up-to-date with bpftool's source code.
> >>
> >> Signed-off-by: Quentin Monnet <quentin@isovalent.com>
> >> ---
> >>  tools/bpf/bpftool/bash-completion/bpftool | 57 +++++++++++++----------
> >>  1 file changed, 32 insertions(+), 25 deletions(-)
> >>
> >> diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool
> >> index cc33c5824a2f..b2e33a2d8524 100644
> >> --- a/tools/bpf/bpftool/bash-completion/bpftool
> >> +++ b/tools/bpf/bpftool/bash-completion/bpftool
> >> @@ -404,8 +404,10 @@ _bpftool()
> >>                              return 0
> >>                              ;;
> >>                          5)
> >> -                            COMPREPLY=( $( compgen -W 'msg_verdict stream_verdict \
> >> -                                stream_parser flow_dissector' -- "$cur" ) )
> >> +                            local BPFTOOL_PROG_ATTACH_TYPES='msg_verdict \
> >> +                                stream_verdict stream_parser flow_dissector'
> >> +                            COMPREPLY=( $( compgen -W \
> >> +                                "$BPFTOOL_PROG_ATTACH_TYPES" -- "$cur" ) )
> >>                              return 0
> >>                              ;;
> >>                          6)
> >> @@ -464,7 +466,7 @@ _bpftool()
> >>
> >>                      case $prev in
> >>                          type)
> >> -                            COMPREPLY=( $( compgen -W "socket kprobe \
> >> +                            local BPFTOOL_PROG_LOAD_TYPES='socket kprobe \
> >>                                  kretprobe classifier flow_dissector \
> >>                                  action tracepoint raw_tracepoint \
> >>                                  xdp perf_event cgroup/skb cgroup/sock \
> >> @@ -479,8 +481,9 @@ _bpftool()
> >>                                  cgroup/post_bind4 cgroup/post_bind6 \
> >>                                  cgroup/sysctl cgroup/getsockopt \
> >>                                  cgroup/setsockopt cgroup/sock_release struct_ops \
> >> -                                fentry fexit freplace sk_lookup" -- \
> >> -                                                   "$cur" ) )
> >> +                                fentry fexit freplace sk_lookup'
> >> +                            COMPREPLY=( $( compgen -W \
> >> +                                "$BPFTOOL_PROG_LOAD_TYPES" -- "$cur" ) )
> >
> > nit: this and similar COMPREPLY assignments now can be on a single line now, no?
>
> It will go over 80 characters, but OK, it will probably be more readable
> on a single line. I'll change for v2.

80 character rule was lifted a while ago. 100 is totally fine. And in
some special cases readability beats even 100, IMO.
diff mbox series

Patch

diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool
index cc33c5824a2f..b2e33a2d8524 100644
--- a/tools/bpf/bpftool/bash-completion/bpftool
+++ b/tools/bpf/bpftool/bash-completion/bpftool
@@ -404,8 +404,10 @@  _bpftool()
                             return 0
                             ;;
                         5)
-                            COMPREPLY=( $( compgen -W 'msg_verdict stream_verdict \
-                                stream_parser flow_dissector' -- "$cur" ) )
+                            local BPFTOOL_PROG_ATTACH_TYPES='msg_verdict \
+                                stream_verdict stream_parser flow_dissector'
+                            COMPREPLY=( $( compgen -W \
+                                "$BPFTOOL_PROG_ATTACH_TYPES" -- "$cur" ) )
                             return 0
                             ;;
                         6)
@@ -464,7 +466,7 @@  _bpftool()
 
                     case $prev in
                         type)
-                            COMPREPLY=( $( compgen -W "socket kprobe \
+                            local BPFTOOL_PROG_LOAD_TYPES='socket kprobe \
                                 kretprobe classifier flow_dissector \
                                 action tracepoint raw_tracepoint \
                                 xdp perf_event cgroup/skb cgroup/sock \
@@ -479,8 +481,9 @@  _bpftool()
                                 cgroup/post_bind4 cgroup/post_bind6 \
                                 cgroup/sysctl cgroup/getsockopt \
                                 cgroup/setsockopt cgroup/sock_release struct_ops \
-                                fentry fexit freplace sk_lookup" -- \
-                                                   "$cur" ) )
+                                fentry fexit freplace sk_lookup'
+                            COMPREPLY=( $( compgen -W \
+                                "$BPFTOOL_PROG_LOAD_TYPES" -- "$cur" ) )
                             return 0
                             ;;
                         id)
@@ -698,15 +701,16 @@  _bpftool()
                             return 0
                             ;;
                         type)
-                            COMPREPLY=( $( compgen -W 'hash array prog_array \
-                                perf_event_array percpu_hash percpu_array \
-                                stack_trace cgroup_array lru_hash \
+                            local BPFTOOL_MAP_CREATE_TYPES='hash array \
+                                prog_array perf_event_array percpu_hash \
+                                percpu_array stack_trace cgroup_array lru_hash \
                                 lru_percpu_hash lpm_trie array_of_maps \
                                 hash_of_maps devmap devmap_hash sockmap cpumap \
                                 xskmap sockhash cgroup_storage reuseport_sockarray \
                                 percpu_cgroup_storage queue stack sk_storage \
-                                struct_ops inode_storage task_storage' -- \
-                                                   "$cur" ) )
+                                struct_ops inode_storage task_storage'
+                            COMPREPLY=( $( compgen -W \
+                                "$BPFTOOL_MAP_CREATE_TYPES" -- "$cur" ) )
                             return 0
                             ;;
                         key|value|flags|entries)
@@ -1017,34 +1021,37 @@  _bpftool()
                     return 0
                     ;;
                 attach|detach)
-                    local ATTACH_TYPES='ingress egress sock_create sock_ops \
-                        device bind4 bind6 post_bind4 post_bind6 connect4 connect6 \
+                    local BPFTOOL_CGROUP_ATTACH_TYPES='ingress egress \
+                        sock_create sock_ops device \
+                        bind4 bind6 post_bind4 post_bind6 connect4 connect6 \
                         getpeername4 getpeername6 getsockname4 getsockname6 \
                         sendmsg4 sendmsg6 recvmsg4 recvmsg6 sysctl getsockopt \
                         setsockopt sock_release'
                     local ATTACH_FLAGS='multi override'
                     local PROG_TYPE='id pinned tag name'
-                    case $prev in
-                        $command)
-                            _filedir
-                            return 0
-                            ;;
-                        ingress|egress|sock_create|sock_ops|device|bind4|bind6|\
-                        post_bind4|post_bind6|connect4|connect6|getpeername4|\
-                        getpeername6|getsockname4|getsockname6|sendmsg4|sendmsg6|\
-                        recvmsg4|recvmsg6|sysctl|getsockopt|setsockopt|sock_release)
+                    # Check for $prev = $command first
+                    if [ $prev = $command ]; then
+                        _filedir
+                        return 0
+                    # Then check for attach type. This is done outside of the
+                    # "case $prev in" to avoid writing the whole list of attach
+                    # types again as pattern to match (where we cannot reuse
+                    # our variable).
+                    elif [[ $BPFTOOL_CGROUP_ATTACH_TYPES =~ $prev ]]; then
                             COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
                                 "$cur" ) )
                             return 0
-                            ;;
+                    fi
+                    # case/esac for the other cases
+                    case $prev in
                         id)
                             _bpftool_get_prog_ids
                             return 0
                             ;;
                         *)
-                            if ! _bpftool_search_list "$ATTACH_TYPES"; then
-                                COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- \
-                                    "$cur" ) )
+                            if ! _bpftool_search_list "$BPFTOOL_CGROUP_ATTACH_TYPES"; then
+                                COMPREPLY=( $( compgen -W \
+                                    "$BPFTOOL_CGROUP_ATTACH_TYPES" -- "$cur" ) )
                             elif [[ "$command" == "attach" ]]; then
                                 # We have an attach type on the command line,
                                 # but it is not the previous word, or