>From d31226fd4867c4af8a77e2a90771c9c927f02f88 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Wed, 26 Oct 2022 21:10:21 -0400 Subject: [PATCH v1] Add the "doas" alias to eshell. * lisp/eshell/em-tramp.el (eshell/doas): new function. --- lisp/eshell/em-tramp.el | 47 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/lisp/eshell/em-tramp.el b/lisp/eshell/em-tramp.el index aebbc36e71..7969a88a2b 100644 --- a/lisp/eshell/em-tramp.el +++ b/lisp/eshell/em-tramp.el @@ -40,9 +40,10 @@ (defgroup eshell-tramp nil "This module defines commands that use TRAMP in a way that is not transparent to the user. So far, this includes only the - built-in su and sudo commands, which are not compatible with - the full, external su and sudo commands, and require the user - to understand how to use the TRAMP sudo method." + built-in su, sudo and doas commands, which are not compatible + with the full, external su, sudo, and doas commands, and + require the user to understand how to use the TRAMP sudo + method." :tag "TRAMP Eshell features" :group 'eshell-module)) @@ -52,7 +53,7 @@ eshell-tramp-initialize (add-hook 'pcomplete-try-first-hook 'eshell-complete-host-reference nil t)) (setq-local eshell-complex-commands - (append '("su" "sudo") + (append '("su" "sudo" "doas") eshell-complex-commands))) (autoload 'eshell-parse-command "esh-cmd") @@ -127,6 +128,44 @@ eshell/sudo (put 'eshell/sudo 'eshell-no-numeric-conversions t) +(defun eshell--doas-directory (directory &optional user) + "Return DIRECTORY wrapped with a doas method for USER." + (let ((user (or user "root")) + (dir (file-local-name (expand-file-name directory))) + (prefix (file-remote-p directory)) + (host (or (file-remote-p directory 'host) + tramp-default-host)) + (method (file-remote-p directory 'method)) + (ruser (file-remote-p directory 'user))) + (if (and prefix (or (not (string-equal method "doas")) + (not (string-equal ruser user)))) + (format "%s|doas:%s@%s:%s" + (substring prefix 0 -1) user host dir) + (format "/doas:%s@%s:%s" user host dir)))) + +(defun eshell/doas (&rest args) + "Call Tramp’s doas method with ARGS. + +Uses the system doas through Tramp's doas method." + (eshell-eval-using-options + "doas" args + '((?h "help" nil nil "show this usage screen") + (?u "user" t user "execute a command as another USER") + (?s "shell" nil shell "start a shell instead of executing COMMAND") + :show-usage + :parse-leading-options-only + :usage "[(-u | --user) USER] -s | COMMAND +Execute a COMMAND as the superuser or another USER.") + (let ((dir (eshell--doas-directory default-directory user))) + (if shell + (throw 'eshell-replace-command + (eshell-parse-command "cd" (list dir))) + (throw 'eshell-external + (let ((default-directory dir)) + (eshell-named-command (car args) (cdr args)))))))) + +(put 'eshell/doas 'eshell-no-numeric-conversions t) + (provide 'em-tramp) ;; Local Variables: -- 2.38.0