scop / bash-completion

Programmable completion functions for bash
GNU General Public License v2.0
2.9k stars 380 forks source link

Complete ip link types #1130

Closed yedayak closed 6 months ago

yedayak commented 6 months ago

Split this into a few commits, adding a function and using it in two places.

yedayak commented 6 months ago

I'm not sure why the alpine CI fails? I tried running it on latest alpine myself and it seems to work fine

akinomyoga commented 6 months ago

That typically happens, so I recently try to do debugging on the CI.

With this change,

diff --git a/completions/ip b/completions/ip
index ac004be75..384de08b9 100644
--- a/completions/ip
+++ b/completions/ip
@@ -17,6 +17,27 @@ _comp_cmd_ip__netns()

 _comp_cmd_ip__link_types()
 {
+    {
+        echo '==== test1 ===='
+        ${1-ip} -c=never link help
+        echo '==== test2 ===='
+        ${1-ip} link help
+        echo '==== test3 ===='
+        {
+            ${1-ip} -c=never link help || ${1-ip} link help
+        } 2>/dev/null |
+            command sed '/TYPE := /,/}/!d'
+        echo '==== test4 ===='
+        {
+            ${1-ip} -c=never link help || ${1-ip} link help
+        } 2>/dev/null |
+            command sed '/TYPE := /,/}/!d;s/.*{//;s/}.*//'
+        echo '==== test5 ===='
+        {
+            ${1-ip} -c=never link help || ${1-ip} link help
+        } 2>/dev/null |
+            command sed '/TYPE := /,/}/!d;s/.*{//;s/}.*//;s/|/ /g'
+    } >&2
     _comp_compgen_split -- "$(
         {
             ${1-ip} -c=never link help || ${1-ip} link help

The result is this:

==== test1 ====
BusyBox v1.36.1 (2023-11-07 18:53:09 UTC) multi-call binary.

Usage: ip [OPTIONS] address|route|link|tunnel|neigh|rule [ARGS]

OPTIONS := -f[amily] inet|inet6|link | -o[neline]

ip addr add|del IFADDR dev IFACE | show|flush [dev IFACE] [to PREFIX]
ip route list|flush|add|del|change|append|replace|test ROUTE
ip link set IFACE [up|down] [arp on|off] [multicast on|off]
[promisc on|off] [mtu NUM] [name NAME] [qlen NUM] [address MAC]
[master IFACE | nomaster] [netns PID]
ip tunnel add|change|del|show [NAME]
[mode ipip|gre|sit] [remote ADDR] [local ADDR] [ttl TTL]
ip neigh show|flush [to PREFIX] [dev DEV] [nud STATE]
ip rule [list] | add|del SELECTOR ACTION
==== test2 ====
ip: invalid argument 'help' to 'ip'
==== test3 ====
==== test4 ====
==== test5 ====
yedayak commented 6 months ago

Thanks @akinomyoga, the issue was ip link help doesn't work in busybox ip. I added a skip to check for that and rebased, hopefully should work now

scop commented 6 months ago

Thanks!