-
Notifications
You must be signed in to change notification settings - Fork 0
/
wire.sh
66 lines (52 loc) · 1.3 KB
/
wire.sh
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
#!/usr/bin/env bash
wire() {
profile=${1:?profile required}
action=${2:-smart}
case "$action" in
up)
sudo wg-quick up "$profile"
;;
down)
sudo wg-quick down "$profile"
;;
toggle)
if ip link show dev "$profile" &>/dev/null; then
sudo wg-quick down "$profile"
else
sudo wg-quick up "$profile"
fi
;;
smart)
if ip link show dev "$profile" &>/dev/null; then
echo "err: link is already up ..." >/dev/stderr
return 1
else
sudo wg-quick up "$profile" && \
sudo bash -c "watch --color WG_COLOR_MODE=always wg show ${profile@Q}; wg-quick down ${profile@Q}";
fi
;;
*)
echo "usage: wire <profile> {up|down|toggle|smart}" >&2
return 1
;;
esac
}
_wire() {
# shellcheck disable=SC2034
local cur prev words cword
_init_completion || return
# find profiles with shell glob
local wp
wp=(/etc/wireguard/*.conf)
# shellcheck disable=SC2206
wp=(${wp[@]##*/})
# shellcheck disable=SC2206
wp=(${wp[@]%%.conf})
# current = 1 -> profile, 2 -> action
case "$cword" in
1) COMPREPLY=($(compgen -W "${wp[*]}" -- "$cur")) ;;
2) COMPREPLY=($(compgen -W "up down toggle smart" -- "$cur")) ;;
*) COMPREPLY=() ;;
esac
}
complete -F _wire wire