-
Notifications
You must be signed in to change notification settings - Fork 5
/
_net_bash_completion
executable file
·76 lines (63 loc) · 1.89 KB
/
_net_bash_completion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Only run if `net` is installed
if ! which net > /dev/null ; then
echo "The net tool is not installed" >&2
return
fi
# cache list of connections
_net_init()
{
local IFS
IFS=$'\n'
_net_connections=($(net --internal-list-all))
}
_net_init
_net()
{
COMPREPLY=()
local IFS cur prev opts word code
IFS=$'\n'
cur="${COMP_WORDS[COMP_CWORD]}"
fst="${COMP_WORDS[1]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts=(--help --iface= --config= --verbose --no-vpn)
_net_connections+=($(net --internal-scan-cache))
# the first argument is a command
if [ $COMP_CWORD == 1 ] ; then
opts+=(list scan connect stop dns mac dns vpn)
fi
if [ "${prev}" == "connect" ] ; then
COMPREPLY=( $(compgen -W "$(printf "%s\n" "${_net_connections[@]}")" -- ${cur}) )
return 0
fi
if [ "${prev}" == "stop" ] ; then
COMPREPLY=( $(compgen -W "$(net --internal-list-up)" -- ${cur}) )
return 0
fi
if [ "${prev}" == "dns" ] ; then
COMPREPLY=( $(compgen -W dhcp -- ${cur}) )
return 0
fi
if [ "${prev}" == "mac" ] ; then
return 1
fi
if [ "${prev}" == "vpn" ] ; then
COMPREPLY=( $(compgen -W "$(net --internal-list-vpn)" -- ${cur}) )
return 0
fi
if [ "${fst}" == "vpn" ] && [ $COMP_CWORD == 3 ] ; then
COMPREPLY=( $(compgen -W "$(echo stop ; echo tmpstop)" -- ${cur}) )
return 0
fi
# check if a connection was already chosen
for word in "${COMP_WORDS[@]}" ; do
for conn in ${_net_connections} ; do
if [ "${word}" == "${conn}" ] ; then
COMPREPLY=( $(compgen -W "$(printf "%s\n" "${opts[@]}")" -- ${cur}) )
return 0
fi
done
done
opts+=("$(printf "%s\n" "${_net_connections[@]}")")
COMPREPLY=( $(compgen -W "$(printf "%s\n" "${opts[@]}")" -- ${cur}) )
}
complete -F _net net