From fb2b84f00b7d4b00ffdf31c0d4d910c1051a9e27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Mon, 28 Sep 2026 21:04:18 +0200 Subject: [PATCH] feat: add usage to ssh menu --- bin/ssh-menu | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/bin/ssh-menu b/bin/ssh-menu index 9843de7..d1f933d 100755 --- a/bin/ssh-menu +++ b/bin/ssh-menu @@ -3,6 +3,7 @@ # pick a host from ~/.ssh/config and every file it includes # ssh-menu connect to the selection # ssh-menu -s print the selection: "" or "\tluks" (ctrl-u) +# ssh-menu -h show usage # exported: the preview and the list reload run in child shells spawned by fzf export SSH_MENU_CONFIG="${SSH_MENU_CONFIG:-$HOME/.ssh/config}" @@ -17,6 +18,19 @@ export SSH_MENU_TIMEOUT="${SSH_MENU_TIMEOUT:-1}" export SSH_MENU_PROXY_TIMEOUT="${SSH_MENU_PROXY_TIMEOUT:-3}" [[ -d $SSH_MENU_CACHE ]] || mkdir -m 700 "$SSH_MENU_CACHE" +# fail early with one message instead of a cascade of command-not-found errors +# from inside fzf's preview shells +function _ssh_menu_require { + local cmd missing=() + for cmd in "$@"; do + command -v "$cmd" >/dev/null 2>&1 || missing+=("$cmd") + done + if (( ${#missing[@]} > 0 )); then + printf 'ssh-menu: missing required commands: %s\n' "${missing[*]}" >&2 + exit 1 + fi +} + # config plus every file named on Include lines; globs expanded, relative # paths resolved against ~/.ssh, unreadable files skipped function _ssh_menu_files { @@ -339,9 +353,26 @@ function _ssh_menu_select { fi } +function _ssh_menu_usage { + cat <<'EOF' +usage: ssh-menu [-s | -h] + + (no args) pick a host and ssh into it + -s pick a host and print it ("" or "\tluks") + -h show this help +EOF +} + case "${1:-}" in - -s) _ssh_menu_select ;; - *) + -h | --help) + _ssh_menu_usage + ;; + -s) + _ssh_menu_require fzf ssh awk timeout + _ssh_menu_select + ;; + "") + _ssh_menu_require fzf ssh awk timeout IFS=$'\t' read -r host profile < <(_ssh_menu_select) [[ -z $host ]] && exit 0 if [[ $profile == luks ]]; then @@ -350,4 +381,9 @@ case "${1:-}" in exec ssh "$host" fi ;; + *) + printf 'ssh-menu: unknown argument: %s\n' "$1" >&2 + _ssh_menu_usage >&2 + exit 1 + ;; esac