From 3d089e07e2683576563fa8f163ed0d51543db91a Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Wed, 23 May 2018 15:17:41 +0200 Subject: [PATCH] wg-quick: better bash completion for non-renaming OSes Signed-off-by: Jason A. Donenfeld --- src/completion/wg-quick.bash-completion | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/completion/wg-quick.bash-completion b/src/completion/wg-quick.bash-completion index 3474120..7b14b75 100644 --- a/src/completion/wg-quick.bash-completion +++ b/src/completion/wg-quick.bash-completion @@ -2,17 +2,18 @@ # Copyright (C) 2015-2018 Jason A. Donenfeld . All Rights Reserved. _wg_quick_completion() { - local p i a search_paths + local p i a search_paths old_glob search_paths=( /etc/wireguard ) + old_glob="$(shopt -p nullglob)" + shopt -s nullglob + [[ $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}" @@ -20,15 +21,23 @@ _wg_quick_completion() { 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]}") ) + if [[ $OSTYPE == *openbsd* || $OSTYPE == *darwin* ]]; then + for i in /var/run/wireguard/*.name; do + i="${i##*/}"; i="${i%.name}" + mapfile -t a < <(compgen -W "$i" -- "${COMP_WORDS[2]}") + COMPREPLY+=( "${a[@]}" ) + done + else + COMPREPLY+=( $(compgen -W "$(wg show interfaces)" -- "${COMP_WORDS[2]}") ) + fi fi fi + eval "$old_glob" } complete -o filenames -o nosort -F _wg_quick_completion wg-quick