diff --git a/README.md b/README.md index c1322ba..1e8cfc3 100644 --- a/README.md +++ b/README.md @@ -115,20 +115,23 @@ Usage: gpgbridge.rb [options] ## Example bash helper functions -Copy the [`gpgbridge_helper.sh`](gpgbridge_helper.sh) script to a directory -accessible from WSL and make it (`chmod +x path/to/gpgbridge_helper.sh`). +Unpack the release file to a directory accessible from WSL. -Then add `path/to/gpgbridge_helper.sh` to your `~/.bash_profile`, -`~/.bashrc`, `~/.zshrc` or similar. Add `--ssh` to enable SSH forwarding and -`--wsl2` if you are running WSL2 as arguments to `gpgbridge_helper.sh`. +Edit the PATHS section in the [`gpgbridge_helper.sh`](gpgbridge_helper.sh) +file (or set the variables before sourcing the file.) -Make sure to edit the paths in the start of the script according to your -setup. +Add the following commands to your `~/.bash_profile`, `~/.bashrc`, +`~/.zshrc` or similar. Add `--ssh` to enable SSH forwarding and `--wsl2` if +you are running WSL2.. + + 1. Source the file: `source path/to/gpgbridge_helper.sh`. + 2. Call the start function: `start_gpgbridge [ --ssh ] [ --wsl2 ]`. This will start the WSL-bridge in WSL, which will in turn start the Win-bridge in Windows. Note that only one WSL-bridge will be started per WSL distribution, and they will all share the same single Win-bridge -running in Windows. +running in Windows. If you edit the `SCRIPT_DIR` path in the file, it can +be sourced and used from all WSL distributions. ## Known and handled problems diff --git a/ReleaseNotes.md b/ReleaseNotes.md index 3703a45..b6491e3 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -1,4 +1,4 @@ -## Changes in v1.1 +## Changes in v1.2 -* Added a helper script (contributed by @tetov) +* Convert the helper script into a file with shell functions to be sourced. * Updated Readme documentation diff --git a/gpgbridge_helper.sh b/gpgbridge_helper.sh index e6adfd3..4a2c3a4 100755 --- a/gpgbridge_helper.sh +++ b/gpgbridge_helper.sh @@ -2,71 +2,69 @@ #-------------------------------------------------------------------------- # GPG bridging from WSL gpg to gpg4win gpg-agent.exe # (needed to use a Yubikey, since WSL cannot access USB devices) - -set -eu - -usage() { - echo "Usage: gpgbridge_helper [ --ssh ] [ --wsl2 ] [ -q | --quiet ] start|stop|restart" -} +# +# 1. Edit the PATHS section below (or set the variables before sourcing this file.) +# 2. Source this file +# 3. Call "start_gpgbridge [ --ssh ] [ --wsl2 ]". # PATHS -SCRIPT_DIR=/mnt/c/Program1/gpgbridge +SCRIPT_DIR_WSL="${SCRIPT_DIR_WSL:-/mnt/c/Program1/gpgbridge}" -PIDFILE_WSL="$HOME/.gpgbridge_wsl.pid" -LOGFILE_WSL="$HOME/.gpgbridge_wsl.log" +PIDFILE_WSL="${PIDFILE_WSL:-$HOME/.gpgbridge_wsl.pid}" +LOGFILE_WSL="${LOGFILE_WSL:-$HOME/.gpgbridge_wsl.log}" -_pidfile_win="$SCRIPT_DIR/gpgbridge_win.pid" -_logfile_win="$SCRIPT_DIR/gpgbridge_win.log" +PIDFILE_WIN="${PIDFILE_WIN:-$SCRIPT_DIR_WSL/gpgbridge_win.pid}" +LOGFILE_WIN="${LOGFILE_WIN:-$SCRIPT_DIR_WSL/gpgbridge_win.log}" -touch "$_pidfile_win" "$_logfile_win" # Needs to be created otherwise wslpath complains -PIDFILE_WIN=$(wslpath -wa $_pidfile_win) -LOGFILE_WIN=$(wslpath -wa $_logfile_win) +#--------------------------------------------------------------------------- +# Do not edit below this line -SCRIPT_FILE_NAME=gpgbridge.rb - -SCRIPT_PATH_WSL="$SCRIPT_DIR/$SCRIPT_FILE_NAME" +touch "$PIDFILE_WIN" "$LOGFILE_WIN" # Needs to be created otherwise wslpath complains +PIDFILE_WIN="$(wslpath -wa "$PIDFILE_WIN")" +LOGFILE_WIN="$(wslpath -wa "$LOGFILE_WIN")" start_gpgbridge() { - if ! command -v ruby.exe >/dev/null 2>&1 ; then - echo 'No ruby.exe found in path' - return 1 - fi - - args="$(_quote "$SCRIPT_PATH_WSL") --daemon" - - if [ -n "${PIDFILE_WSL:-}" ] ; then - args="$args --pidfile $(_quote "$PIDFILE_WSL")" - fi - - if [ -n "${LOGFILE_WSL:-}" ] ; then - args="$args --logfile $(_quote "$LOGFILE_WSL")" + if ! command -v ruby.exe >/dev/null + then + echo 'No ruby.exe found in path' + return 1 fi - if [ -n "${PIDFILE_WIN:-}" ] ; then - args="$args --windows-pidfile $(_quote "$PIDFILE_WIN")" - fi - - if [ -n "${LOGFILE_WIN:-}" ] ; then - args="$args --windows-logfile $(_quote "$LOGFILE_WIN")" - fi + # Parse arguments + #local _opts _parsed_args _is_args_valid + _parsed_args=$(getopt -a -n start_gpgbridge -o h --long ssh,wsl2,help -- "$@") + _is_args_valid=$? - if [ "$SSH" -eq 1 ] ; then - args="$args --enable-ssh-support" - SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket) - export SSH_AUTH_SOCK + if [ ! $_is_args_valid ] ; then + echo "Usage: start_gpgbridge [ --ssh ] [ --wsl2 ]" + unset _parsed_args _is_args_valid + exit 1 fi - if [ "$WSL2" -eq 1 ] ; then - wsl_ip="$(ip route | awk '/^default via / {print $3}')" - args="$args --remote-address $wsl_ip" - fi - - if [ "$QUIET" -eq 1 ] ; then - args="$args >/dev/null 2>&1" - fi - - eval ruby "$args" + _opts='' + eval set -- "$_parsed_args" + while : + do + case "$1" in + --ssh) + _opts="$_opts --enable-ssh-support" + SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket) + export SSH_AUTH_SOCK + shift + ;; + --wsl2) + _opts="$_opts --remote-address $(ip route | awk '/^default via / {print $3}')" + shift + ;; + --) + shift + break + esac + done + + ruby "$SCRIPT_DIR_WSL/gpgbridge.rb" --daemon --pidfile "$PIDFILE_WSL" --logfile "$LOGFILE_WSL" --windows-pidfile "$PIDFILE_WIN" --windows-logfile "$LOGFILE_WIN" ${_opts} + unset _parsed_args _is_args_valid _opts } stop_gpgbridge() @@ -79,46 +77,5 @@ restart_gpgbridge() { stop_gpgbridge sleep 1 - start_gpgbridge + start_gpgbridge "$@" } - -# From "Rich's sh (POSIX shell) tricks" https://www.etalabs.net/sh_tricks.html -_quote () { printf %s\\n "$1" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/" ; } - -# Default arguments -SSH=0 -WSL2=0 -QUIET=0 - -# Parse arguments -PARSED_ARGS=$(getopt -a -n gpgbridge_helper -o qh --long quiet,ssh,wsl2,help -- "$@") -IS_ARGS_VALID=$? - -if [ ! $IS_ARGS_VALID ] ; then - usage - exit 1 -fi - -eval set -- "$PARSED_ARGS" -while : -do - case "$1" in - --ssh) SSH=1 ; shift ;; - --wsl2) WSL2=1 ; shift ;; - -q | --quiet) QUIET=1 ; shift ;; - -h | --help) usage && exit 0 ;; - --) shift ; break ;; - esac -done - -if [ -z "${1:-}" ] ; then - usage - exit 1 -fi - -case "$1" in - start) start_gpgbridge ;; - stop) stop_gpgbridge ;; - restart) restart_gpgbridge ;; - *) usage && exit 1 ;; -esac