start: refactor code into functions

This commit is contained in:
Fernando Schauenburg 2022-09-02 09:11:28 +02:00
parent 0a79fc285a
commit ea356d6e2a

View file

@ -5,6 +5,38 @@ usage () {
echo " -h show help and exit" echo " -h show help and exit"
} }
require() {
if ! command -v "$@" >/dev/null 2>&1; then
>&2 echo "ERROR: required command not found: $1"
exit 1
fi
}
start_with_wsl_agent() {
if [ "$#" -gt 0 ]; then
SHLVL=0 exec ssh-agent "$SHELL" -c "$@"
else
SHLVL=0 exec ssh-agent "$SHELL"
fi
}
start_with_windows_agent() {
require socat
require npiperelay
# Forward SSH Agent requests from withitn WSL to OpenSSH for Windows, based on:
# https://stuartleeks.com/posts/wsl-ssh-key-forward-to-windows/
export SSH_AUTH_SOCK=$HOME/.ssh/agent.sock
[ -S "$SSH_AUTH_SOCK" ] && rm "$SSH_AUTH_SOCK"
socat UNIX-LISTEN:$SSH_AUTH_SOCK,fork EXEC:"npiperelay -ei -s //./pipe/openssh-ssh-agent",nofork &
if [ "$#" -gt 0 ]; then
SHLVL=0 exec "$SHELL" -c "$@"
else
SHLVL=0 exec "$SHELL"
fi
}
# Use OpenSSH for Windows by default. # Use OpenSSH for Windows by default.
WSL_SSH_AGENT=false WSL_SSH_AGENT=false
@ -29,30 +61,8 @@ while getopts ":wh" arg; do
done done
if [ "$WSL_SSH_AGENT" = true ]; then if [ "$WSL_SSH_AGENT" = true ]; then
if [ "$#" -gt 0 ]; then start_with_wsl_agent
SHLVL=0 exec ssh-agent "$SHELL" -c "$@"
else else
SHLVL=0 exec ssh-agent "$SHELL" start_with_windows_agent
fi
fi fi
require() {
if ! command -v "$@" >/dev/null 2>&1; then
>&2 echo "ERROR: required command not found: $1"
exit 1
fi
}
require socat
require npiperelay
# Forward SSH Agent requests from withitn WSL to OpenSSH for Windows, based on:
# https://stuartleeks.com/posts/wsl-ssh-key-forward-to-windows/
export SSH_AUTH_SOCK=$HOME/.ssh/agent.sock
[ -S "$SSH_AUTH_SOCK" ] && rm "$SSH_AUTH_SOCK"
socat UNIX-LISTEN:$SSH_AUTH_SOCK,fork EXEC:"npiperelay -ei -s //./pipe/openssh-ssh-agent",nofork &
if [ "$#" -gt 0 ]; then
SHLVL=0 exec "$SHELL" -c "$@"
else
SHLVL=0 exec "$SHELL"
fi