Checkpoint some refactoring

This commit is contained in:
Ray Miller 2025-01-12 12:38:47 +00:00
parent b0070af1fd
commit 70543ef7c5
Signed by: ray
GPG key ID: 043F786C4CD681B8
3 changed files with 50 additions and 27 deletions

View file

@ -1,8 +1,11 @@
(define-module (ordo connection)
#:use-module (oop goops)
#:use-module (ice-9 exceptions)
#:use-module (ice-9 popen)
#:use-module (ice-9 rdelim)
#:use-module (srfi srfi-1) ; list operations
#:use-module (logging logger)
#:use-module (srfi srfi-1) ; list operations
#:use-module (srfi srfi-26) ; cut
#:use-module (srfi srfi-71) ; extended let
#:use-module ((srfi srfi-197) #:select (chain-when))
#:use-module (ssh session)
@ -10,6 +13,7 @@
#:use-module (ssh auth)
#:use-module (ssh popen)
#:use-module (ssh sftp)
#:use-module (ordo util flatten)
#:use-module (ordo util shell-quote)
#:export (<connection>
local-connection
@ -20,10 +24,7 @@
connection-call-with-input-file
connection-call-with-output-file
call-with-connection
must
run
must1
run1))
run))
(define-class <connection> ()
(sudo #:getter sudo? #:init-keyword #:sudo))
@ -122,33 +123,26 @@
(lambda () (proc c))
(lambda () (close-connection! c))))
(define (keyword-arg kw args)
(define* (keyword-arg kw args #:optional (default #f))
(cond
((< (length args) 2) #f)
((< (length args) 2) default)
((equal? (first args) kw) (second args))
(else (keyword-arg kw (cddr args)))))
(define (run conn prog . args)
(let* ((args kwargs (break keyword? args))
(pwd (keyword-arg #:pwd kwargs))
(env (keyword-arg #:env kwargs)))
(connection-run conn pwd env prog args)))
(define (run1 . args)
(let ((out rc (apply run args)))
(values (first out) rc)))
(define (must conn prog . args)
(let* ((args kwargs (break keyword? args))
(let* ((args (flatten args))
(args kwargs (break keyword? args))
(args (remove unspecified? args))
(pwd (keyword-arg #:pwd kwargs))
(env (keyword-arg #:env kwargs))
(error-msg (keyword-arg #:error-msg kwargs))
(return (keyword-arg #:return kwargs identity))
(check? (keyword-arg #:check? kwargs #t))
(out rc (connection-run conn pwd env prog args)))
(if (zero? rc)
out
(error (if error-msg
(format #f "~a: ~a" error-msg out)
(format #f "~a error: ~a" prog out))))))
(define (must1 . args)
(first (apply must args)))
(log-msg 'INFO "Command " prog " exited " rc)
(if check?
(if (zero? rc)
(return out)
(raise-exception (make-exception
(make-external-error)
(make-exception-with-message (format #f "Non-zero exit (~a) from ~a" rc prog)))))
(values (return out) rc))))

View file

@ -0,0 +1,10 @@
(define-module (ordo util flatten)
#:export (flatten))
(define (flatten lst)
(cond
((null? lst) '())
((list? (car lst))
(append (flatten (car lst)) (flatten (cdr lst))))
(else
(cons (car lst) (flatten (cdr lst))))))