Commit 1bbf3df460b55c4d0a0c212aaf73df60984430ef

Thomas de Grivel 2020-04-18T14:56:50

Export run-as-root. Support doas on OpenBSD.

diff --git a/package.lisp b/package.lisp
index fecb545..5ee0cd2 100644
--- a/package.lisp
+++ b/package.lisp
@@ -123,6 +123,8 @@
    #:host-run
    #:localhost
    #:run
+   #:run-as-root
+   #:run-as-root-command
    #:ssh-host
    #:with-connected-host
    #:with-host
diff --git a/unix/commands.lisp b/unix/commands.lisp
index b5018a9..b76ef99 100644
--- a/unix/commands.lisp
+++ b/unix/commands.lisp
@@ -54,3 +54,9 @@
 
 (defun sudo (&rest command)
   (run (apply #'sudo_ command)))
+
+(defun doas_ (&rest command)
+  (join-str " " "doas" command))
+
+(defun doas (&rest command)
+  (run (apply #'doas_ command)))
diff --git a/unix/openbsd.lisp b/unix/openbsd.lisp
index 64908d3..6ae8cee 100644
--- a/unix/openbsd.lisp
+++ b/unix/openbsd.lisp
@@ -20,6 +20,9 @@
 
 (in-re-readtable)
 
+(defmethod run-as-root-command ((host t) (os os-openbsd))
+  "doas ")
+
 (defmethod probe-hostname ((host host) (os os-openbsd))
   (list :hostname (run-1 "hostname -s")))
 
diff --git a/unix/operations.lisp b/unix/operations.lisp
index 189f58b..2257313 100644
--- a/unix/operations.lisp
+++ b/unix/operations.lisp
@@ -18,11 +18,16 @@
 
 (in-package :adams)
 
+(defgeneric run-as-root-command (host os))
+
+(defmethod run-as-root-command ((host t) (os os-unix))
+  "sudo ")
+
 (defun run-as-root (&rest command)
-  (apply #'run
-         (unless (equal "root" (get-probed (current-host) :user))
-           "sudo ")
-         command))
+  (let* ((host (current-host))
+         (prefix (unless (equal "root" (get-probed host :user))
+                   (run-as-root-command host (get-probed host :os)))))
+    (apply #'run prefix command)))
 
 ;;  Host operations