aboutsummaryrefslogtreecommitdiff
path: root/src/connection.lisp
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2021-02-21 09:54:51 -0700
committerSean Whitton <spwhitton@spwhitton.name>2021-02-21 09:54:51 -0700
commit1b14862d157c6ef8b2fa97b4cfa7fc9ef479a3ff (patch)
tree82104c9881be94ef07f92f010808a62b972c2659 /src/connection.lisp
parente6edfd28a88d36cde37c92c7d10649506bcaabd6 (diff)
downloadconsfigurator-1b14862d157c6ef8b2fa97b4cfa7fc9ef479a3ff.tar.gz
add WITH-REMOTE-TEMPORARY-FILE
Signed-off-by: Sean Whitton <spwhitton@spwhitton.name>
Diffstat (limited to 'src/connection.lisp')
-rw-r--r--src/connection.lisp29
1 files changed, 17 insertions, 12 deletions
diff --git a/src/connection.lisp b/src/connection.lisp
index 51fc39d..ce1bdda 100644
--- a/src/connection.lisp
+++ b/src/connection.lisp
@@ -152,7 +152,7 @@ Keyword arguments accepted:
the command.
Returns command's stdout, stderr and exit code."
- (let (cmd input may-fail env (stderr (mktemp)))
+ (let (cmd input may-fail env)
(loop for arg = (pop args)
do (case arg
(:for-exit (setq may-fail t))
@@ -171,17 +171,16 @@ Returns command's stdout, stderr and exit code."
(setq cmd (format nil "env ~A ~A"
(escape-sh-command accum)
cmd))))
- (unwind-protect
- (multiple-value-bind (out exit)
- (connection-run *connection*
- (format nil "( ~A ) 2>~A" cmd stderr)
- input)
- (let ((err (readfile stderr)))
- (if (or may-fail (= exit 0))
- (values out err exit)
- (error 'connection-run-failed
- :stdout out :stderr err :exit-code exit))))
- (connection-run *connection* (format nil "rm -f ~A" stderr)))))
+ (with-remote-temporary-file (stderr)
+ (multiple-value-bind (out exit)
+ (connection-run *connection*
+ (format nil "( ~A ) 2>~A" cmd stderr)
+ input)
+ (let ((err (readfile stderr)))
+ (if (or may-fail (= exit 0))
+ (values out err exit)
+ (error 'connection-run-failed
+ :stdout out :stderr err :exit-code exit)))))))
(defun mktemp ()
"Make a temporary file on the remote side."
@@ -197,6 +196,12 @@ Returns command's stdout, stderr and exit code."
(car (lines out))
(error 'connection-run-failed :exit-code exit))))
+(defmacro with-remote-temporary-file ((file) &body body)
+ `(let ((,file (mktemp)))
+ (unwind-protect
+ (progn ,@body)
+ (connection-run *connection* (format nil "rm -f ~A" ,file)))))
+
(defun runlines (&rest args)
(lines (apply #'run args)))