fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

exploidecompletions/ip: complete netns for ip l set dev netns, plus some option arguments6aee7bf37

master
28.1 KiB642 linesraw
1# ip(8) completion for fish
2
3# The difficulty here is that ip allows abbreviating options, so we need to complete "ip a" like "ip address", but not "ip m" like "ip mroute"
4# Also the manpage and even the grammar it accepts is utter shite (options can only be before commands, some things are only in the BNF, others only in the text)
5# It also quite likes the word "dev", even though it needs it less than the BNF specifies
6
7set -l ip_commands link address addrlabel route rule neighbour ntable tunnel tuntap maddr mroute mrule monitor xfrm netns l2tp tcp_metrics
8set -l ip_addr a ad add addr addre addres address
9set -l ip_link l li lin link
10set -l ip_neigh n ne nei neig neigh neighb neighbo neighbor neighbour
11set -l ip_route r ro rou rout route
12set -l ip_rule ru rul rule
13set -l ip_all_commands $ip_commands $ip_addr $ip_link $ip_neigh $ip_route $ip_rule
14
15function __fish_ip_commandwords
16    set -l skip 0
17    set -l cmd (commandline -xpc | string escape)
18    # Remove the first word because it's "ip" or an alias for it
19    set -e cmd[1]
20    set -l have_command 0
21    for word in $cmd
22        switch $word
23            # Normalize the commands to their full form - `ip a` is `ip address`
24            # This can't be just an unambiguity check because there's also `ip addrlabel`
25            case a ad add addr addre addres address
26                # "addr add" is a thing, so we can only echo "address" if it's actually the command
27                if test $have_command = 0
28                    set have_command 1
29                    echo address
30                else
31                    echo $word
32                end
33            case l li lin link
34                if test $have_command = 0
35                    set have_command 1
36                    echo link
37                else
38                    echo $word
39                end
40            case "addrl*"
41                if test $have_command = 0
42                    set have_command 1
43                    echo addrlabel
44                else
45                    echo $word
46                end
47            case r ro rou rout route
48                if test $have_command = 0
49                    set have_command 1
50                    echo route
51                else
52                    echo $word
53                end
54            case "ru*"
55                if test $have_command = 0
56                    set have_command 1
57                    echo rule
58                else
59                    echo $word
60                end
61            case n ne nei neig neigh neighb neighbo neighbor neighbour
62                if test $have_command = 0
63                    set have_command 1
64                    echo neighbour
65                else
66                    echo $word
67                end
68            case nt
69                if test $have_command = 0
70                    set have_command 1
71                    echo ntable
72                else
73                    echo $word
74                end
75            case t tu tun tunn tunne tunnel
76                if test $have_command = 0
77                    set have_command 1
78                    echo tunnel
79                else
80                    echo $word
81                end
82            case "tunt*"
83                if test $have_command = 0
84                    set have_command 1
85                    echo tuntap
86                else
87                    echo $word
88                end
89            case m ma mad madd maddr maddre maddres maddress
90                if test $have_command = 0
91                    set have_command 1
92                    echo maddress
93                else
94                    echo $word
95                end
96            case mr "mro*"
97                if test $have_command = 0
98                    set have_command 1
99                    echo mroute
100                else
101                    echo $word
102                end
103            case "mru*"
104                if test $have_command = 0
105                    set have_command 1
106                    echo mrule
107                else
108                    echo $word
109                end
110            case "mo*"
111                if test $have_command = 0
112                    set have_command 1
113                    echo monitor
114                else
115                    echo $word
116                end
117            case "x*"
118                if test $have_command = 0
119                    set have_command 1
120                    echo xfrm
121                else
122                    echo $word
123                end
124            case "net*"
125                if test $have_command = 0
126                    set have_command 1
127                    echo netns
128                else
129                    echo $word
130                end
131            case "l*"
132                if test $have_command = 0
133                    set have_command 1
134                    echo l2tp
135                else
136                    echo $word
137                end
138            case "tc*"
139                if test $have_command = 0
140                    set have_command 1
141                    echo tcp_metrics
142                else
143                    echo $word
144                end
145            case "to*"
146                if test $have_command = 0
147                    set have_command 1
148                    echo token
149                else
150                    echo $word
151                end
152            case -n -netns --netns
153                if test $have_command = 0
154                    set skip 1
155                else
156                    echo $word
157                end
158            case '-*'
159                test $have_command = 0
160                and continue
161                echo $word
162            case '*'
163                if test $skip = 1
164                    set skip 0
165                    continue
166                end
167                echo $word
168        end
169    end
170    # Print an empty line if the current token is empty, so we know that the one before it is finished
171    # TODO: For some reason it is necessary to always print the current token - why doesn't the above loop catch it?
172    set -l token (commandline -ct)
173    echo $token
174end
175
176function __fish_ip_device
177    command ip -o link show | while read -l a b c
178        printf '%s\t%s\n' (string replace -r '(@.*)?:' '' -- $b) Device
179    end
180end
181
182function __fish_ip_scope
183    if test -r /etc/iproute2/rt_scopes
184        string replace -r '#.*' '' </etc/iproute2/rt_scopes \
185            | string match -v '^\s*$' \
186            | string replace -r '(\S+)\s*(\S+)' '$1\t$2\n$2\t$1' \
187            | string match -rv '^(global|link|host).*' # Ignore scopes with better descriptions
188    end
189    # Predefined scopes
190    printf '%s\t%s\n' global "Address is globally valid" \
191        link "Address is link-local, only valid on this device" \
192        host "Address is only valid on this host"
193end
194
195function __fish_ip_netns_list
196    command ip netns list | while read -l a b c
197        echo -- $a
198    end
199end
200
201function __fish_ip_routing_tables
202    command ip -d route show table all 2>/dev/null | string replace -fr '.*\stable\s(\S+).*' '$1'
203end
204
205function __fish_ip_types
206    printf '%s\t%s\n' \
207        bridge "Ethernet Bridge device" \
208        bond "Bonding device" \
209        dummy "Dummy network interface" \
210        hsr "High-availability Seamless Redundancy device" \
211        ifb "Intermediate Functional Block device" \
212        ipoib "IP over Infiniband device" \
213        macvlan "Virtual interface base on link layer address (MAC)" \
214        macvtap "Virtual interface based on link layer address (MAC) and TAP." \
215        vcan "Virtual Controller Area Network interface" \
216        vxcan "Virtual Controller Area Network tunnel interface" \
217        veth "Virtual ethernet interface" \
218        vlan "802.1q tagged virtual LAN interface" \
219        vxlan "Virtual eXtended LAN" \
220        ip6tnl "Virtual tunnel interface IPv4|IPv6 over IPv6" \
221        ipip "Virtual tunnel interface IPv4 over IPv4" \
222        sit "Virtual tunnel interface IPv6 over IPv4" \
223        gre "Virtual tunnel interface GRE over IPv4" \
224        gretap "Virtual L2 tunnel interface GRE over IPv4" \
225        erspan "Encapsulated Remote SPAN over GRE and IPv4" \
226        ip6gre "Virtual tunnel interface GRE over IPv6" \
227        ip6gretap "Virtual L2 tunnel interface GRE over IPv6" \
228        ip6erspan "Encapsulated Remote SPAN over GRE and IPv6" \
229        vti "Virtual tunnel interface" \
230        nlmon "Netlink monitoring device" \
231        ipvlan "Interface for L3 (IPv6/IPv4) based VLANs" \
232        ipvtap "Interface for L3 (IPv6/IPv4) based VLANs and TAP" \
233        lowpan "Interface for 6LoWPAN (IPv6) over IEEE 802.15.4 / Bluetooth" \
234        geneve "GEneric NEtwork Virtualization Encapsulation" \
235        bareudp "Bare UDP L3 encapsulation support" \
236        macsec "Interface for IEEE 802.1AE MAC Security (MACsec)" \
237        vrf "Interface for L3 VRF domains" \
238        netdevsim "Interface for netdev API tests" \
239        rmnet "Qualcomm rmnet device" \
240        xfrm "Virtual xfrm interface"
241end
242
243function __fish_ip_neigh_states
244    printf '%s\t%s\n' \
245        permanent "entry is valid forever" \
246        noarp "entry is valid without validation" \
247        reachable "entry is valid until timeout" \
248        stale "entry is valid but suspicious" \
249        none "pseudo state" \
250        incomplete "entry has not yet been validated" \
251        delay "entry validation is currently delayed" \
252        probe "neighbor is being probed" \
253        failed "neighbor validation has ultimately failed"
254end
255
256function __fish_complete_ip
257    set -l cmd (__fish_ip_commandwords)
258    set -l count (count $cmd)
259    switch "$cmd[1]"
260        case address
261            # We're still _on_ the second word, which is the subcommand
262            if not set -q cmd[3]
263                printf '%s\t%s\n' \
264                    add "Add new protocol address" \
265                    delete "Delete protocol address" \
266                    show "Look at protocol addresses" \
267                    flush "Flush protocol addresses" \
268                    help "Display help"
269            else
270                switch $cmd[2]
271                    # Change and replace are undocumented (apart from mentions in the BNF)
272                    case add change replace
273                        switch $count
274                            case 3
275                                # __fish_ip_complete_ip
276                            case '*'
277                                switch $cmd[-2]
278                                    case dev
279                                        __fish_ip_device
280                                    case scope
281                                        __fish_ip_scope
282                                        # TODO: Figure out how to complete these
283                                    case label
284                                        # Prefix
285                                    case local peer broadcast
286                                        # Address
287                                    case valid_lft preferred_lft
288                                        # Lifetime
289                                    case '*'
290                                        printf '%s\t%s\n' forever "Keep address valid forever" \
291                                            home "(Ipv6 only) Designate address as home address" \
292                                            nodad "(Ipv6 only) Don't perform duplicate address detection" \
293                                            dev "Add address to specified device" \
294                                            scope "Set scope of address" \
295                                            label "Tag address with label"
296                                end
297                        end
298                    case delete
299                        switch $count
300                            case 3
301                                command ip -o addr show | while read -l a b c d e
302                                    echo $d
303                                end
304                            case 4
305                                # A dev argument is mandatory, but contrary to the BNF, other things (like "scope") are also valid here
306                                # And yes, unlike e.g. show, this _needs_ the "dev" before the device
307                                # Otherwise it barfs and says "??? prefix is expected"
308                                # Anyway, try to steer the user towards supplying a device
309                                echo dev
310                            case 5
311                                switch $cmd[-2]
312                                    case dev
313                                        command ip -o addr show | string match "*$cmd[3]*" | while read -l a b c
314                                            echo $b
315                                        end
316                                        # TODO: Moar
317                                end
318                        end
319                    case show save flush # These take the same args
320                        switch $cmd[-2]
321                            case dev
322                                __fish_ip_device
323                            case scope
324                                __fish_ip_scope
325                            case to
326                                # Prefix
327                            case label
328                                # Label-pattern
329                            case '*'
330                                printf '%s\t%s\n' up "Only active devices" \
331                                    dev "Limit to a certain device" \
332                                    scope "Limit scope" \
333                                    to "Limit prefix" \
334                                    label "Limit by label" \
335                                    dynamic "(Ipv6 only) Limit to dynamic addresses" \
336                                    permanent "(Ipv6 only) Limit to permanent addresses"
337                                __fish_ip_device
338                                # TODO: Moar
339                        end
340                end
341            end
342        case link
343            if not set -q cmd[3]
344                printf '%s\t%s\n' \
345                    add "Add virtual link" \
346                    delete "Delete virtual link" \
347                    set "Change device attributes" \
348                    show "Display device attributes" \
349                    help "Display help"
350            else
351                # TODO: Add moar
352                switch $cmd[2]
353                    case add
354                        switch $cmd[-2]
355                            case link
356                                __fish_ip_device
357                            case name # freeform, uncompleteable.
358                            case type
359                                __fish_ip_types
360                            case txqueuelen
361                            case address
362                            case broadcast
363                            case mtu index numtxqueues numrxqueues gso_max_size gso_max_segs
364                                # These all take some kind of number?
365                                seq 0 50
366                            case add '*'
367                                # We assume if we haven't checked it above, we're back to `ip link add`, with any optionals completed.
368                                # So we now suggest optionals.
369                                printf '%s\t%s\n' \
370                                    link "" \
371                                    type "" \
372                                    txqueuelen PACKETS \
373                                    address LLADDR \
374                                    broadcast LLADDR \
375                                    mtu MTU \
376                                    index IDX \
377                                    numtxqueues QUEUE_COUNT \
378                                    numrxqueues QUEUE_COUNT \
379                                    gso_max_size BYTES \
380                                    gso_max_segs
381                        end
382                    case delete
383                        switch $cmd[-2]
384                            case delete
385                                __fish_ip_device
386                                echo dev
387                                echo group
388                            case dev
389                                __fish_ip_device
390                            case group
391                            case type
392                                __fish_ip_types
393                            case '*'
394                                if not set -q cmd[6]
395                                    echo type
396                                end
397                        end
398                    case set
399                        switch $cmd[-2]
400                            case type
401                                __fish_ip_types
402                                echo bridge_slave
403                                echo bond_slave
404                            case arp dynamic {all,}multicast pro{misc,todown} trailers spoofchk query_rss trust
405                                echo on
406                                echo off
407                            case name alias
408                            case address broadcast
409                            case mtu txqueuelen
410                            case netns
411                                __fish_ip_netns_list
412                            case link-netnsid
413                            case vf mac
414                            case {{max,min}_tx_,}rate
415                            case node_guid port_guid
416                            case state
417                                echo auto
418                                echo enable
419                                echo disable
420                            case master dev # Yes, the word "dev" is allowed here!
421                                __fish_ip_device
422                            case nomaster
423                            case vrf
424                            case xdp'*'
425                            case object section pinned
426                            case addrgenmode
427                            case macaddr
428                            case group
429                            case set '*'
430                                __fish_ip_device
431                                echo group
432                                echo up
433                                echo down
434                        end
435                    case show
436                    case help
437                        __fish_ip_types
438                end
439            end
440        case neighbour
441            if not set -q cmd[3]
442                printf '%s\t%s\n' \
443                    add "Add new neighbour entry" \
444                    delete "Delete neighbour entry" \
445                    change "Change neighbour entry" \
446                    replace "Add or change neighbour entry" \
447                    show "List neighbour entries" \
448                    flush "Flush neighbour entries" \
449                    get "Lookup neighbour entry" \
450                    help "Display help"
451            else
452                switch $cmd[2]
453                    case add del delete change replace
454                        switch $cmd[-2]
455                            case lladdr
456                            case nud
457                                __fish_ip_neigh_states
458                            case proxy
459                            case dev
460                                __fish_ip_device
461                            case '*'
462                                echo lladdr
463                                echo nud
464                                echo proxy
465                                echo dev
466                                echo router
467                                echo use
468                                echo managed
469                                echo extern_learn
470                        end
471                    case show flush
472                        switch $cmd[-2]
473                            case to
474                            case dev
475                                __fish_ip_device
476                            case vrf
477                            case nud
478                                __fish_ip_neigh_states
479                                echo all
480                            case '*'
481                                echo to
482                                echo dev
483                                echo vrf
484                                echo nomaster
485                                echo proxy
486                                echo unused
487                                echo nud
488                        end
489                    case get
490                        switch $cmd[-2]
491                            case to
492                            case dev
493                                __fish_ip_device
494                            case '*'
495                                echo proxy
496                                echo to
497                                echo dev
498                        end
499                end
500            end
501        case route
502            if not set -q cmd[3]
503                printf '%s\t%s\n' \
504                    add "Add new route" \
505                    change "Change route" \
506                    append "Append route" \
507                    replace "Change or add new route" \
508                    delete "Delete route" \
509                    show "List routes" \
510                    flush "Flush routing tables" \
511                    get "Get a single route" \
512                    save "Save routing table to stdout" \
513                    showdump "Show saved routing table from stdin" \
514                    restore "Restore routing table from stdin" \
515                    help "Display help"
516            else
517                # TODO: switch on $cmd[2] and complete subcommand specific arguments
518                # for now just complete most useful arguments for the last token
519                switch $cmd[-2]
520                    case dev iif oif
521                        __fish_ip_device
522                    case table
523                        __fish_ip_routing_tables
524                end
525            end
526        case rule
527            if not set -q cmd[3]
528                printf '%s\t%s\n' \
529                    add "Add new rule" \
530                    delete "Delete rule" \
531                    flush "Flush rules" \
532                    show "List rules" \
533                    save "Save rules to stdout" \
534                    restore "Restore rules from stdin" \
535                    help "Display help"
536            else
537                # TODO: switch on $cmd[2] and complete subcommand specific arguments
538                # for now just complete most useful arguments for the last token
539                switch $cmd[-2]
540                    case iif oif
541                        __fish_ip_device
542                    case table
543                        __fish_ip_routing_tables
544                end
545            end
546        case netns
547            if not set -q cmd[3]
548                printf '%s\t%s\n' add "Add network namespace" \
549                    attach "Attach process to network namespace" \
550                    delete "Delete network namespace" \
551                    set "Change network namespace attributes" \
552                    identify "Display network namespace for a process ID" \
553                    pids "Display process IDs of processes running in network namespace" \
554                    monitor "Report as network namespace names are added and deleted" \
555                    exec "Execute command in network namespace" \
556                    help "Display help" \
557                    list "List network namespaces"
558            else
559                switch $cmd[2]
560                    case delete
561                        if not set -q cmd[4]
562                            __fish_ip_netns_list
563                        end
564                    case exec
565                        if not set -q cmd[4]
566                            __fish_ip_netns_list
567                        else
568                            __fish_complete_subcommand --commandline $cmd[4..-1]
569                        end
570                    case pids
571                        if not set -q cmd[4]
572                            __fish_ip_netns_list
573                        end
574                    case set
575                        if not set -q cmd[4]
576                            __fish_ip_netns_list
577                        end
578                    case attach
579                        if not set -q cmd[4]
580                            __fish_ip_netns_list
581                        else
582                            __fish_complete_pids
583                        end
584                    case identify
585                        if not set -q cmd[4]
586                            __fish_complete_pids
587                        end
588                end
589            end
590        case tuntap
591            if not set -q cmd[3]
592                printf '%s\t%s\n' \
593                    add "Add a new tun or tap device" \
594                    del "Delete a tun or tap device" \
595                    show "Show tun and tap devices" \
596                    help "Display help"
597            else
598                switch $cmd[-2]
599                    case dev
600                        __fish_ip_device
601                    case mode
602                        printf '%s\n' tun tap
603                    case user
604                        __fish_complete_users
605                    case group
606                        __fish_complete_groups
607                    case '*'
608                        printf '%s\n' dev mode user group one_queue pi vnet_hdr multi_queue name
609                end
610            end
611    end
612end
613
614complete -f -c ip
615complete -f -c ip -a '(__fish_complete_ip)'
616complete -f -c ip -n "not __fish_seen_subcommand_from $ip_all_commands" -a "$ip_commands"
617# Yes, ip only takes options before "objects"
618complete -c ip -s h -l human -d "Output statistics with human readable values" -n "not __fish_seen_subcommand_from $ip_commands"
619complete -c ip -s b -l batch -d "Read commands from file or stdin" -rF -n "not __fish_seen_subcommand_from $ip_commands"
620complete -c ip -l force -d "Don't terminate on errors in batch mode" -n "not __fish_seen_subcommand_from $ip_commands"
621complete -c ip -s V -l Version -d "Print the version" -n "not __fish_seen_subcommand_from $ip_commands"
622complete -c ip -f -s s -l stats -d "Output more information" -n "not __fish_seen_subcommand_from $ip_commands"
623complete -c ip -f -s d -l details -d "Output more detailed information" -n "not __fish_seen_subcommand_from $ip_commands"
624complete -c ip -f -s l -l loops -d "Specify maximum number of loops for 'ip addr flush'" -x -n "not __fish_seen_subcommand_from $ip_commands"
625complete -c ip -f -s f -l family -d "The protocol family to use" -xa "inet inet6 bridge ipx dnet mpls link any" -n "not __fish_seen_subcommand_from $ip_commands"
626complete -c ip -f -s 4 -d "Short for --family inet" -n "not __fish_seen_subcommand_from $ip_commands"
627complete -c ip -f -s 6 -d "Short for --family inet6" -n "not __fish_seen_subcommand_from $ip_commands"
628complete -c ip -f -s B -d "Short for --family bridge" -n "not __fish_seen_subcommand_from $ip_commands"
629complete -c ip -f -s M -d "Short for --family mpls" -n "not __fish_seen_subcommand_from $ip_commands"
630complete -c ip -f -s 0 -d "Short for --family link" -n "not __fish_seen_subcommand_from $ip_commands"
631complete -c ip -f -s o -l oneline -d "Output on one line" -n "not __fish_seen_subcommand_from $ip_commands"
632complete -c ip -f -s r -l resolve -d "Resolve names and print them instead of addresses" -n "not __fish_seen_subcommand_from $ip_commands"
633complete -c ip -f -s n -l netns -d "Use specified network namespace" -xa "(__fish_ip_netns_list)" -n "not __fish_seen_subcommand_from $ip_commands"
634complete -c ip -f -s a -l all -d "Execute command for all objects" -n "not __fish_seen_subcommand_from $ip_commands"
635complete -c ip -f -s c -l color -d "Configure color output" -n "not __fish_seen_subcommand_from $ip_commands"
636complete -c ip -f -s t -l timestamp -d "Display current time when using monitor" -n "not __fish_seen_subcommand_from $ip_commands"
637complete -c ip -f -o ts -l tshort -d "Like -timestamp, but shorter format" -n "not __fish_seen_subcommand_from $ip_commands"
638complete -c ip -f -o rc -l rcvbuf -d "Set the netlink socket receive buffer size" -n "not __fish_seen_subcommand_from $ip_commands"
639complete -c ip -f -o iec -d "Print human readable rates in IEC units" -n "not __fish_seen_subcommand_from $ip_commands"
640complete -c ip -f -o br -l brief -d "Print only basic information in a tabular format" -n "not __fish_seen_subcommand_from $ip_commands"
641complete -c ip -f -s j -l json -d "Output results in JSON" -n "not __fish_seen_subcommand_from $ip_commands"
642complete -c ip -f -s p -l pretty -d "Output results in pretty JSON" -n "not __fish_seen_subcommand_from $ip_commands"