diff options
| author | Craig Jennings <c@cjennings.net> | 2026-05-25 18:29:31 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-05-25 18:29:31 -0500 |
| commit | 937be691cd921eeaebac0349782d985edba820cd (patch) | |
| tree | 63d0bb28e5d18e6b7220090330cc88eb3f18e14c | |
| parent | 7ce43f29a754d68db5a3c8683f01e7f9698a32e8 (diff) | |
| download | dotemacs-937be691cd921eeaebac0349782d985edba820cd.tar.gz dotemacs-937be691cd921eeaebac0349782d985edba820cd.zip | |
refactor(eshell): move SSH-jump hosts into a defcustom
The eshell SSH-jump aliases (gocj, gosb, gowolf) were hardcoded inline in the alias setup, which tied the module to my machines. I moved them into a cj/eshell-ssh-hosts defcustom (an alias→remote-path alist, defaulting to my current hosts) and build the aliases by iterating it. A different machine can override the variable or set it to nil instead of editing the module. Extracted a pure cj/--eshell-ssh-alias-commands helper so the alias construction is testable without a live eshell.
| -rw-r--r-- | modules/eshell-config.el | 35 | ||||
| -rw-r--r-- | tests/test-eshell-config-ssh-aliases.el | 50 |
2 files changed, 82 insertions, 3 deletions
diff --git a/modules/eshell-config.el b/modules/eshell-config.el index 64bc88c6..0439a467 100644 --- a/modules/eshell-config.el +++ b/modules/eshell-config.el @@ -26,6 +26,37 @@ (require 'system-utils) +(defgroup cj/eshell nil + "Personal Eshell configuration." + :group 'eshell) + +(defcustom cj/eshell-ssh-hosts + '(("gocj" . "/sshx:cjennings@cjennings.net:/var/cjennings/") + ("gosb" . "/sshx:cjennings@wolf.usbx.me:/home/cjennings/") + ("gowolf" . "/sshx:cjennings@wolf.usbx.me:/home/cjennings/")) + "Alist of Eshell SSH-jump aliases. +Each entry is a cons cell (ALIAS-NAME . REMOTE-PATH). At Eshell +startup an alias named ALIAS-NAME is defined that runs `cd' to the +given TRAMP REMOTE-PATH. Override on a different machine to point at +your own hosts, or set to nil to define no SSH aliases." + :type '(alist :key-type (string :tag "Alias") + :value-type (string :tag "Remote path")) + :group 'cj/eshell) + +(defun cj/--eshell-ssh-alias-commands (hosts) + "Return the SSH alias definitions for HOSTS. +HOSTS is an alist of (ALIAS-NAME . REMOTE-PATH) as in +`cj/eshell-ssh-hosts'. The result is a list of (ALIAS-NAME . COMMAND) +pairs where COMMAND is the `cd' string `eshell/alias' should run." + (mapcar (lambda (entry) + (cons (car entry) (concat "cd " (cdr entry)))) + hosts)) + +(defun cj/--eshell-define-ssh-aliases (hosts) + "Define the Eshell SSH-jump aliases for HOSTS via `eshell/alias'." + (dolist (pair (cj/--eshell-ssh-alias-commands hosts)) + (eshell/alias (car pair) (cdr pair)))) + (use-package eshell :ensure nil ;; built-in :commands (eshell) @@ -79,9 +110,7 @@ (eshell/alias "em" "find-file $1") (eshell/alias "emacs" "find-file $1") (eshell/alias "open" "cj/xdg-open $1") - (eshell/alias "gocj" "cd /sshx:cjennings@cjennings.net:/var/cjennings/") - (eshell/alias "gosb" "cd /sshx:cjennings@wolf.usbx.me:/home/cjennings/") - (eshell/alias "gowolf" "cd /sshx:cjennings@wolf.usbx.me:/home/cjennings/") + (cj/--eshell-define-ssh-aliases cj/eshell-ssh-hosts) (eshell/alias "v" "eshell-exec-visual $*") (eshell/alias "ff" "find-file-other-window $1") (eshell/alias "f" "find-using-dired $1") diff --git a/tests/test-eshell-config-ssh-aliases.el b/tests/test-eshell-config-ssh-aliases.el new file mode 100644 index 00000000..8d30801f --- /dev/null +++ b/tests/test-eshell-config-ssh-aliases.el @@ -0,0 +1,50 @@ +;;; test-eshell-config-ssh-aliases.el --- Tests for eshell SSH alias building -*- lexical-binding: t; -*- + +;;; Commentary: +;; Tests for cj/--eshell-ssh-alias-commands from eshell-config.el. +;; +;; The helper takes the `cj/eshell-ssh-hosts' alist of (ALIAS-NAME . REMOTE-PATH) +;; and returns (ALIAS-NAME . COMMAND) pairs where COMMAND is the `cd' string +;; `eshell/alias' runs. Testing the pure helper avoids needing a live Eshell. + +;;; Code: + +(require 'ert) +(require 'cl-lib) +(require 'package) + +(setq package-user-dir (expand-file-name "elpa" user-emacs-directory)) +(package-initialize) +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'eshell-config) + +(declare-function cj/--eshell-ssh-alias-commands "eshell-config" (hosts)) + +;;; Normal Cases + +(ert-deftest test-eshell-config-ssh-alias-commands-normal-two-hosts () + "Normal: two hosts produce two cd-command alias pairs." + (let* ((hosts '(("gosb" . "/sshx:cjennings@wolf.usbx.me:/home/cjennings/") + ("gowolf" . "/sshx:cjennings@wolf.usbx.me:/home/cjennings/"))) + (result (cj/--eshell-ssh-alias-commands hosts))) + (should (= 2 (length result))) + (should (equal (assoc "gosb" result) + '("gosb" . "cd /sshx:cjennings@wolf.usbx.me:/home/cjennings/"))) + (should (equal (assoc "gowolf" result) + '("gowolf" . "cd /sshx:cjennings@wolf.usbx.me:/home/cjennings/"))))) + +(ert-deftest test-eshell-config-ssh-alias-commands-normal-single-host () + "Normal: a single host produces one pair, order preserved." + (let ((result (cj/--eshell-ssh-alias-commands + '(("gocj" . "/sshx:cjennings@cjennings.net:/var/cjennings/"))))) + (should (equal result + '(("gocj" . "cd /sshx:cjennings@cjennings.net:/var/cjennings/")))))) + +;;; Boundary Cases + +(ert-deftest test-eshell-config-ssh-alias-commands-boundary-empty () + "Boundary: an empty host list produces no SSH aliases." + (should (null (cj/--eshell-ssh-alias-commands '())))) + +(provide 'test-eshell-config-ssh-aliases) +;;; test-eshell-config-ssh-aliases.el ends here. |
