unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Brian Cully via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: 58899@debbugs.gnu.org
Subject: bug#58899: [PATCH v1] Add the "doas" alias to eshell.
Date: Sun, 30 Oct 2022 12:42:41 -0400	[thread overview]
Message-ID: <87pme92rge.fsf@ditto.jhoto.spork.org> (raw)

[-- Attachment #1: Type: text/plain, Size: 760 bytes --]


This patch adds a “doas” command alias to eshell for users who prefer
doas, similar to the existing “su” and “sudo” command aliases.

I'm unsare whether to use “TRAMP” or “Tramp” spelling. The existing
routines in the file use the former, but I've been informed the latter
is correct. A future patch should make all spellings (at least within
this module) consistent.

Additionaly, I've added a function for calculating the remote-path for
files accessed with doas. This function is currently limited to *only*
doas, because I didn't want to mess with the other su and sudo
functions, but there's no reason it can't be shared, and a future patch
should implement that sharing if this patch is considered desirable.

-bjc


[-- Attachment #2: v1-0001-Add-the-doas-alias-to-eshell.patch --]
[-- Type: text/x-patch, Size: 3345 bytes --]

From d31226fd4867c4af8a77e2a90771c9c927f02f88 Mon Sep 17 00:00:00 2001
From: Brian Cully <bjc@kublai.com>
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


             reply	other threads:[~2022-10-30 16:42 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-30 16:42 Brian Cully via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2022-10-30 21:51 ` bug#58899: [PATCH v1] Add the "doas" alias to eshell Jim Porter
2022-10-30 22:06   ` Jim Porter
2022-10-31 12:36     ` Eli Zaretskii
2022-10-31  8:16   ` Michael Albinus
2022-10-31 16:01   ` Brian Cully via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-10-31 18:01     ` Eli Zaretskii
2022-10-31 18:29       ` Brian Cully via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-03 17:09         ` Jim Porter
2022-11-03 20:24           ` Brian Cully via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-05 19:10             ` Jim Porter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87pme92rge.fsf@ditto.jhoto.spork.org \
    --to=bug-gnu-emacs@gnu.org \
    --cc=58899@debbugs.gnu.org \
    --cc=bjc@spork.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).