From f8666bc3776a9c3b02af69b7d78b69f2cb0fa8a7 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Wed, 4 Jan 2017 07:05:56 +0100 Subject: tools: add bash completion for wg(8) --- src/tools/Makefile | 7 ++- src/tools/completion/wg.bash-completion | 91 +++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 src/tools/completion/wg.bash-completion (limited to 'src') diff --git a/src/tools/Makefile b/src/tools/Makefile index 2d2ad03..5b72879 100644 --- a/src/tools/Makefile +++ b/src/tools/Makefile @@ -3,8 +3,10 @@ DESTDIR ?= BINDIR ?= $(PREFIX)/bin LIBDIR ?= $(PREFIX)/lib MANDIR ?= $(PREFIX)/share/man +BASHCOMPDIR ?= $(PREFIX)/share/bash-completion/completions RUNSTATEDIR ?= /var/run PKG_CONFIG ?= pkg-config +WITH_BASHCOMPLETION ?= yes CFLAGS ?= -O3 CFLAGS += -std=gnu11 @@ -25,8 +27,9 @@ clean: rm -f wg *.o *.d install: wg - install -v -d "$(DESTDIR)$(BINDIR)" && install -m 0755 -v wg "$(DESTDIR)$(BINDIR)/wg" - install -v -d "$(DESTDIR)$(MANDIR)/man8" && install -m 0644 -v wg.8 "$(DESTDIR)$(MANDIR)/man8/wg.8" + @install -v -d "$(DESTDIR)$(BINDIR)" && install -m 0755 -v wg "$(DESTDIR)$(BINDIR)/wg" + @install -v -d "$(DESTDIR)$(MANDIR)/man8" && install -m 0644 -v wg.8 "$(DESTDIR)$(MANDIR)/man8/wg.8" + @[ "$(WITH_BASHCOMPLETION)" = "yes" ] && install -v -d "$(BASHCOMPDIR)" && install -m 0644 -v completion/wg.bash-completion "$(DESTDIR)$(BASHCOMPDIR)/wg" check: clean CFLAGS=-g scan-build --view --keep-going $(MAKE) wg diff --git a/src/tools/completion/wg.bash-completion b/src/tools/completion/wg.bash-completion new file mode 100644 index 0000000..76a832a --- /dev/null +++ b/src/tools/completion/wg.bash-completion @@ -0,0 +1,91 @@ +# Copyright (C) 2017 Jason A. Donenfeld . All Rights Reserved. + +_wg_completion() { + if [[ $COMP_CWORD -eq 1 ]]; then + COMPREPLY+=( $(compgen -W "show showconf set setconf addconf genkey genpsk pubkey" -- "${COMP_WORDS[1]}") ) + return + fi + case "${COMP_WORDS[1]}" in + genkey|genpsk|pubkey|help) return; ;; + show|showconf|set|setconf|addconf) ;; + *) return; + esac + + if [[ $COMP_CWORD -eq 2 ]]; then + local extra + [[ ${COMP_WORDS[1]} == show ]] && extra=" all interfaces" + COMPREPLY+=( $(compgen -W "$(wg show interfaces 2>/dev/null)$extra" -- "${COMP_WORDS[2]}") ) + return + fi + + if [[ $COMP_CWORD -eq 3 && ${COMP_WORDS[1]} == show && ${COMP_WORDS[2]} != interfaces ]]; then + COMPREPLY+=( $(compgen -W "public-key private-key preshared-key listen-port peers endpoints allowed-ips latest-handshakes persistent-keepalive transfer" -- "${COMP_WORDS[3]}") ) + return + fi + + if [[ $COMP_CWORD -eq 3 && ( ${COMP_WORDS[1]} == setconf || ${COMP_WORDS[1]} == addconf ) ]]; then + compopt -o filenames + COMPREPLY+=( $(compgen -f -- "${COMP_WORDS[3]}") ) + return + fi + + [[ ${COMP_WORDS[1]} == set ]] || return + + local has_listen_port=0 has_private_key=0 has_preshared_key=0 has_peer=0 has_remove=0 has_endpoint=0 has_persistent_keepalive=0 has_allowed_ips=0 words=() i j + for ((i=3;i/dev/null)" -- "${COMP_WORDS[COMP_CWORD]}") ) + return + fi + + for ((i=has_peer;i