]> git.ipfire.org Git - thirdparty/wireguard-tools.git/commitdiff
wg-quick: support FreeBSD/Darwin search path
authorJason A. Donenfeld <Jason@zx2c4.com>
Wed, 23 May 2018 13:08:30 +0000 (15:08 +0200)
committerJason A. Donenfeld <Jason@zx2c4.com>
Wed, 23 May 2018 13:24:07 +0000 (15:24 +0200)
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
src/completion/wg-quick.bash-completion
src/man/wg-quick.8
src/wg-quick/darwin.bash
src/wg-quick/freebsd.bash

index e5e3983f28298395e4ce3c2956e984fc0e88f83e..3474120db4bcde14e2d05ec8323c03e81ca59a87 100644 (file)
@@ -2,17 +2,23 @@
 # Copyright (C) 2015-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
 
 _wg_quick_completion() {
-       local i a
+       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 i in /etc/wireguard/*.conf; do
-                               i="${i##*/}"; i="${i%.conf}"
-                               mapfile -t a < <(compgen -W "$i" -- "${COMP_WORDS[2]}")
-                               COMPREPLY+=( "${a[@]}" )
+                       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]}")
index 5e27d10212fc3f43fd4b8d9da67f1ee39d2c5fdd..d97fec95d1ff104a55298aa9c5318e67518e6708 100644 (file)
@@ -31,7 +31,8 @@ interface without bringing the interface down.
 
 \fICONFIG_FILE\fP is a configuration file, whose filename is the interface name
 followed by `.conf'. Otherwise, \fIINTERFACE\fP is an interface name, with configuration
-found at `/etc/wireguard/\fIINTERFACE\fP.conf'.
+found at `/etc/wireguard/\fIINTERFACE\fP.conf', searched first, followed by distro-specific
+search paths.
 
 Generally speaking, this utility is just a simple script that wraps invocations to
 .BR wg (8)
index 17cf2b81aab28b2e3cb284996d70921d102b993f..d1c83c04ec5d6975b1d39621fe7999c993530cd1 100755 (executable)
@@ -40,10 +40,16 @@ die() {
 
 [[ ${BASH_VERSINFO[0]} -ge 4 ]] || die "Version mismatch: bash ${BASH_VERSINFO[0]} detected, when bash 4+ required"
 
+CONFIG_SEARCH_PATHS=( /etc/wireguard /usr/local/etc/wireguard )
+
 parse_options() {
-       local interface_section=0 line key value stripped
+       local interface_section=0 line key value stripped path
        CONFIG_FILE="$1"
-       [[ $CONFIG_FILE =~ ^[a-zA-Z0-9_=+.-]{1,15}$ ]] && CONFIG_FILE="/etc/wireguard/$CONFIG_FILE.conf"
+       if [[ $CONFIG_FILE =~ ^[a-zA-Z0-9_=+.-]{1,15}$ ]]; then
+               for path in "${CONFIG_SEARCH_PATHS[@]}"; do
+                       [[ -e $path/$CONFIG_FILE.conf ]] && { CONFIG_FILE="$path/$CONFIG_FILE.conf"; break; }
+               done
+       fi
        [[ -e $CONFIG_FILE ]] || die "\`$CONFIG_FILE' does not exist"
        [[ $CONFIG_FILE =~ (^|/)([a-zA-Z0-9_=+.-]{1,15})\.conf$ ]] || die "The config file must be a valid interface name, followed by .conf"
        CONFIG_FILE="$(cd "${CONFIG_FILE%/*}" && pwd -P)/${CONFIG_FILE##*/}"
@@ -380,9 +386,11 @@ cmd_usage() {
 
          CONFIG_FILE is a configuration file, whose filename is the interface name
          followed by \`.conf'. Otherwise, INTERFACE is an interface name, with
-         configuration found at /etc/wireguard/INTERFACE.conf. It is to be readable
-         by wg(8)'s \`setconf' sub-command, with the exception of the following additions
-         to the [Interface] section, which are handled by $PROGRAM:
+         configuration found at:
+         ${CONFIG_SEARCH_PATHS[@]/%//INTERFACE.conf}.
+         It is to be readable by wg(8)'s \`setconf' sub-command, with the exception
+         of the following additions to the [Interface] section, which are handled
+         by $PROGRAM:
 
          - Address: may be specified one or more times and contains one or more
            IP addresses (with an optional CIDR mask) to be set for the interface.
index 6d698e2012e1b3e15efa394154d3a165053c2520..cc3b8e62d7e3935c1527796874f3b1eeaa4e1e9a 100755 (executable)
@@ -38,10 +38,16 @@ die() {
        exit 1
 }
 
+CONFIG_SEARCH_PATHS=( /etc/wireguard /usr/local/etc/wireguard )
+
 parse_options() {
-       local interface_section=0 line key value stripped
+       local interface_section=0 line key value stripped path
        CONFIG_FILE="$1"
-       [[ $CONFIG_FILE =~ ^[a-zA-Z0-9_=+.-]{1,15}$ ]] && CONFIG_FILE="/etc/wireguard/$CONFIG_FILE.conf"
+       if [[ $CONFIG_FILE =~ ^[a-zA-Z0-9_=+.-]{1,15}$ ]]; then
+               for path in "${CONFIG_SEARCH_PATHS[@]}"; do
+                       [[ -e $path/$CONFIG_FILE.conf ]] && { CONFIG_FILE="$path/$CONFIG_FILE.conf"; break; }
+               done
+       fi
        [[ -e $CONFIG_FILE ]] || die "\`$CONFIG_FILE' does not exist"
        [[ $CONFIG_FILE =~ (^|/)([a-zA-Z0-9_=+.-]{1,15})\.conf$ ]] || die "The config file must be a valid interface name, followed by .conf"
        CONFIG_FILE="$(readlink -f "$CONFIG_FILE")"
@@ -334,9 +340,11 @@ cmd_usage() {
 
          CONFIG_FILE is a configuration file, whose filename is the interface name
          followed by \`.conf'. Otherwise, INTERFACE is an interface name, with
-         configuration found at /etc/wireguard/INTERFACE.conf. It is to be readable
-         by wg(8)'s \`setconf' sub-command, with the exception of the following additions
-         to the [Interface] section, which are handled by $PROGRAM:
+         configuration found at:
+         ${CONFIG_SEARCH_PATHS[@]/%//INTERFACE.conf}.
+         It is to be readable by wg(8)'s \`setconf' sub-command, with the exception
+         of the following additions to the [Interface] section, which are handled
+         by $PROGRAM:
 
          - Address: may be specified one or more times and contains one or more
            IP addresses (with an optional CIDR mask) to be set for the interface.