aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/tools/completion/wg-quick.bash-completion
blob: 3474120db4bcde14e2d05ec8323c03e81ca59a87 (plain) (blame)
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
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2015-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.

_wg_quick_completion() {
	local p i a search_paths
	search_paths=( /etc/wireguard )

	[[ $OSTYPE == *freebsd* || $OSTYPE == *darwin* ]] && search_paths+=( /usr/local/etc/wireguard )

	if [[ $COMP_CWORD -eq 1 ]]; then
		COMPREPLY+=( $(compgen -W "up down" -- "${COMP_WORDS[1]}") )
	elif [[ $COMP_CWORD -eq 2 ]]; then
		if [[ ${COMP_WORDS[1]} == up ]]; then
			local old_glob="$(shopt -p nullglob)"
			shopt -s nullglob
			for p in "${search_paths[@]}"; do
				for i in "$p"/*.conf; do
					i="${i##*/}"; i="${i%.conf}"
					mapfile -t a < <(compgen -W "$i" -- "${COMP_WORDS[2]}")
					COMPREPLY+=( "${a[@]}" )
				done
			done
			eval "$old_glob"
			mapfile -t a < <(compgen -f -X '!*.conf' -- "${COMP_WORDS[2]}")
			COMPREPLY+=( "${a[@]}" )
			mapfile -t a < <(compgen -d -- "${COMP_WORDS[2]}")
			COMPREPLY+=( "${a[@]}" )
		elif [[ ${COMP_WORDS[1]} == down ]]; then
			COMPREPLY+=( $(compgen -W "$(wg show interfaces)" -- "${COMP_WORDS[2]}") )
		fi
	fi
}

complete -o filenames -o nosort -F _wg_quick_completion wg-quick