zssh.fish

 1function zssh --description "SSH to a host and attach to a remote zmx session"
 2    argparse 'h/help' 'install' -- $argv
 3    or return 1
 4
 5    if set -q _flag_help; or test (count $argv) -lt 1
 6        echo "Usage: zssh [--install] [user@]host [session]"
 7        echo "Example: zssh devbox term"
 8        return 0
 9    end
10
11    set -l destination $argv[1]
12    set -l session main
13
14    if test (count $argv) -ge 2
15        set session $argv[2]
16    end
17
18    if not string match -qr '^[A-Za-z0-9._:-]+$' -- $session
19        echo "zssh: session names may only contain letters, numbers, dots, underscores, colons, and hyphens" >&2
20        return 2
21    end
22
23    mkdir -p "$HOME/.ssh/controlmasters"
24
25    if set -q _flag_install
26        zmx-remote-install $destination
27        or return $status
28    else
29        zmx-remote-install --upgrade-only $destination
30        or return $status
31    end
32
33    set -l remote_command "sh -c 'PATH=\"\$HOME/.local/bin:\$PATH\"; command -v zmx >/dev/null 2>&1 || { echo \"zssh: zmx is not installed on the remote host or is not on PATH; retry with zssh --install\" >&2; exit 127; }; exec zmx attach \"\$1\"' sh '$session'"
34
35    command ssh \
36        -o ControlMaster=auto \
37        -o ControlPersist=10m \
38        -o ControlPath="$HOME/.ssh/controlmasters/%C" \
39        -o RequestTTY=yes \
40        -o RemoteCommand="$remote_command" \
41        $destination
42end