Skip to content

Commit

Permalink
Merge pull request #1090 from hellodword/patch-1
Browse files Browse the repository at this point in the history
fix: prefer using iproute2 instead of ifconfig
  • Loading branch information
scop authored Nov 24, 2024
2 parents d4a1c56 + c2cafae commit 3b1586b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
11 changes: 7 additions & 4 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -1708,7 +1708,7 @@ _comp_compgen_mac_addresses()
# - ip link: link/ether
_comp_compgen -v addresses split -- "$(
{
LC_ALL=C ifconfig -a || ip -c=never link show || ip link show
ip -c=never link show || ip link show || LC_ALL=C ifconfig -a
} 2>/dev/null | command sed -ne \
"s/.*[[:space:]]HWaddr[[:space:]]\{1,\}\($_re\)[[:space:]].*/\1/p" -ne \
"s/.*[[:space:]]HWaddr[[:space:]]\{1,\}\($_re\)[[:space:]]*$/\1/p" -ne \
Expand Down Expand Up @@ -1779,7 +1779,7 @@ _comp_compgen_ip_addresses()
local PATH=$PATH:/sbin
local addrs
_comp_compgen -v addrs split -- "$({
LC_ALL=C ifconfig -a || ip -c=never addr show || ip addr show
ip -c=never addr show || ip addr show || LC_ALL=C ifconfig -a
} 2>/dev/null |
command sed -e 's/[[:space:]]addr:/ /' -ne \
"s|.*inet${_n}[[:space:]]\{1,\}\([^[:space:]/]*\).*|\1|p")" ||
Expand Down Expand Up @@ -1813,9 +1813,12 @@ _comp_compgen_available_interfaces()
if [[ ${1-} == -w ]]; then
iwconfig
elif [[ ${1-} == -a ]]; then
ifconfig || ip -c=never link show up || ip link show up
# Note: we prefer ip (iproute2) to ifconfig (inetutils) since long
# interface names will be truncated by ifconfig [1].
# [1]: https://github.com/scop/bash-completion/issues/1089
ip -c=never link show up || ip link show up || ifconfig
else
ifconfig -a || ip -c=never link show || ip link show
ip -c=never link show || ip link show || ifconfig -a
fi
} 2>/dev/null | _comp_awk \
'/^[^ \t]/ { if ($1 ~ /^[0-9]+:/) { print $2 } else { print $1 } }')" &&
Expand Down
Empty file.
Empty file.
36 changes: 36 additions & 0 deletions test/fixtures/shared/bin/ip
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/sh

# Dummy "ip addr show" and "ip link show up" emulator

for arg in "$@"; do
case "$arg" in
link)
cat <<EOF
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default
link/ether 33:33:33:33:33:33 brd ff:ff:ff:ff:ff:ff link-netnsid 0
EOF
exit 0
;;
addr)
cat <<EOF
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 33:33:33:33:33:33 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 192.168.80.11/24 brd 192.168.80.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::000:0000:0000:0000/64 scope link
valid_lft forever preferred_lft forever
EOF
exit 0
;;
esac
done

exit 1
2 changes: 1 addition & 1 deletion test/t/unit/test_unit_compgen_xinetd_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_env_non_pollution(self, bash):
def test_basic(self, bash):
output = assert_bash_exec(
bash,
"foo() { local _comp__test_xinetd_dir=$PWD/shared/bin; unset -v COMPREPLY; "
"foo() { local _comp__test_xinetd_dir=$PWD/_comp_compgen_xinetd_services/xinetd.d; unset -v COMPREPLY; "
'_comp_compgen_xinetd_services; printf "%s\\n" "${COMPREPLY[@]}"; }; foo; unset -f foo',
want_output=True,
)
Expand Down

0 comments on commit 3b1586b

Please sign in to comment.