Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to helper script example. #1

Merged
merged 4 commits into from
Jul 25, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 82 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,44 +98,95 @@ Usage: gpgbridge.rb [options]

## Example bash helper functions

I have the following in my `~/.bash_profile` in WSL. Note that there is a
line in start_gpgbridge that must be uncommented for WSL2.
Copy the script below to somewhere accessible from WSL, make it
executable (`chmod +x path/to/gpgbridge_helper.sh`).

Then add `source path/to/gpgbridge_helper.sh` to your `~/.bash_profile`, `~/.bashrc` or `~/.zshrc`.

Make sure to edit the variables in the start of the script according to your
setup.

```bash
#!/usr/bin/env bash
#--------------------------------------------------------------------------
# GPG bridging from WSL gpg to gpg4win gpg-agent.exe
# (needed to use a Yubikey, since WSL cannot access USB devices)

if [[ -f /mnt/c/Program1/gpgbridge.rb ]]
# Set this to true if running WSL1
tetov marked this conversation as resolved.
Show resolved Hide resolved
WSL2=false
tetov marked this conversation as resolved.
Show resolved Hide resolved

SCRIPT_DIR_WSL='/mnt/c/Program1/'
# shellcheck disable=SC1003
SCRIPT_DIR_WIN='C:\\Program1\\'

PIDFILE_WSL="$HOME/.gpgbridge.pid"
LOGFILE_WSL="$HOME/.gpgbridge.log"

PIDFILE_WIN="${SCRIPT_DIR_WIN}gpgbridge.pid"
LOGFILE_WIN="${SCRIPT_DIR_WIN}gpgbridge.log"

SCRIPT_FILE_NAME='gpgbridge.rb'

SCRIPT_PATH_WSL="${SCRIPT_DIR_WSL}${SCRIPT_FILE_NAME}"

# Set to true to redirect output to /dev/null
QUIET=false

function start_gpgbridge
{
if ! command -v ruby.exe >/dev/null
then
echo 'No ruby.exe found in path'
return
fi

local cmd=( \
'ruby' \
"$SCRIPT_PATH_WSL" \
'--verbose' \
'--daemon' \
"--pidfile $PIDFILE_WSL" \
"--logfile $LOGFILE_WSL" \
"--windows-pidfile $PIDFILE_WIN" \
"--windows-logfile $LOGFILE_WIN" \
)

if [ "$1" = 'ssh' ]
then
cmd+=('--enable-ssh-support')
SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)
export SSH_AUTH_SOCK
fi

if [ "$WSL2" = true ]
then
cmd+=("--remote-address $(ip route | awk '/^default via / {print $3}')")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be two separate arguments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be, it works as is but that might be more readable?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, maybe it works because of the eval line, that will cause all arguments to be split at whitespace.

fi

if [ "$QUIET" = true ]
then
cmd+=('>/dev/null 2>&1')
fi

printf -v _cmd '%s ' "${cmd[@]}"

eval "$_cmd"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking the reason you are using eval here is so that the redirection to /dev/null will work. Am I right that you don't want to see the message about an existing gpg bridge already running? Maybe it would be better to add a --quiet option to the script to make it silent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using eval because I ran into issues where ruby would either complain about not finding a file named "path/to/bridge arg1 arg2 arg3" or not recognized an argument because it was single-quoted..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I solved the quiet part with a lazy env variable, I didn't find a good way to parse non positional arguments.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK!

}

function stop_gpgbridge
{
pkill -TERM -f 'ruby.*gpgbridge\.rb'
}

function restart_gpgbridge
{
stop_gpgbridge
sleep 1
start_gpgbridge "$@"
}

if [ -f "$SCRIPT_PATH_WSL" ]
then
function start_gpgbridge
{
local opts=''
if ! command -v ruby.exe >/dev/null
then
echo 'No ruby.exe found in path'
return
fi

# Uncomment the following line for WSL2 to set the remote address
#opts="--remote-address $(ip route | awk '/^default via / {print $3}')"

if [[ $1 == ssh ]]; then
opts="$opts --enable-ssh-support"
export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)
fi
ruby /mnt/c/Program1/gpgbridge.rb --daemon --pidfile ~/.gpgbridge.pid --logfile ~/.gpgbridge.log --verbose --windows-logfile 'C:\Program1\gpgbridge.log' --windows-pidfile 'C:\Program1\gpgbridge.pid' $opts
}
function stop_gpgbridge
{
pkill -TERM -f 'ruby.*gpgbridge\.rb'
}
function restart_gpgbridge
{
stop_gpgbridge
sleep 1
start_gpgbridge "$@"
}
start_gpgbridge ssh
fi
```
Expand Down