Use ice-9 optargs rather than our own keyword-args

This commit is contained in:
Ray Miller 2025-07-06 14:27:37 +01:00
parent 9068953967
commit e9eb8681e4
Signed by: ray
GPG key ID: 043F786C4CD681B8
4 changed files with 31 additions and 47 deletions

View file

@ -1,27 +1,29 @@
(define-module (ordo action remote-cmd)
#:use-module (ice-9 exceptions)
#:use-module (ice-9 optargs)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-71)
#:use-module (ordo connection)
#:use-module (ordo connection base)
#:use-module (ordo logger)
#:use-module (ordo util flatten)
#:use-module (ordo util keyword-args)
#:export (remote-cmd))
(define (remote-cmd conn prog . args)
(let* ((args options (break keyword? args))
(args (remove unspecified? (flatten args)))
(return (keyword-arg options #:return identity))
(check? (keyword-arg options #:check?))
(command (build-command conn prog args options)))
(log-msg 'DEBUG "Running command: " command " on connection " (describe conn))
(let ((out rc (remote-exec conn command)))
(log-msg 'DEBUG "Command exit code: " 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)))))
(let ((args options (break keyword? args)))
(let-keywords
options #t
((return identity)
(check? #f))
(let ((command (build-command conn prog (remove unspecified? (flatten args)) options)))
(log-msg 'DEBUG "Running command: " command " on connection " (describe conn))
(let ((out rc (remote-exec conn command)))
(log-msg 'DEBUG "Command exit code: " 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))
(make-exception-with-irritants out))))
(values (return out) rc)))))))

View file

@ -17,9 +17,9 @@ this program. If not, see <https://www.gnu.org/licenses/>.
(define-module (ordo connection base)
#:use-module (ice-9 match)
#:use-module (ice-9 optargs)
#:use-module (oop goops)
#:use-module (ordo util flatten)
#:use-module (ordo util keyword-args)
#:use-module (ordo util shell-quote)
#:use-module ((srfi srfi-1) #:select (remove))
#:export (<connection>
@ -46,14 +46,17 @@ this program. If not, see <https://www.gnu.org/licenses/>.
(define-method (teardown (c <connection>)) #t)
(define-method (build-command (c <connection>) (prog-name <string>) (prog-args <list>) (options <list>))
(let* ((pwd (keyword-arg options #:pwd))
(env (keyword-arg options #:env))
(redirect-err? (keyword-arg options #:redirect-err? #t))
(xs (remove unspecified?
(let-keywords
options #t
((pwd #f)
(env #f)
(shell-quote? #t)
(redirect-err? #t))
(let ((xs (remove unspecified?
(flatten (list "env"
(when pwd (list "--chdir" (string-shell-quote pwd)))
(when env (map (match-lambda ((k . v) (string-append k "=" (string-shell-quote v)))) env))
prog-name
(map string-shell-quote prog-args)
(if shell-quote? (map string-shell-quote prog-args) prog-args)
(when redirect-err? "2>&1"))))))
(string-join xs " ")))
(string-join xs " "))))

View file

@ -76,7 +76,8 @@ this program. If not, see <https://www.gnu.org/licenses/>.
(log-msg 'NOTICE (task-name task) " - CHANGED")
(for-each schedule-handler! (task-trigger task)))
(else
(log-msg 'NOTICE (task-name task) " - " result))))
(log-msg 'NOTICE (task-name task) " - " result)))
result)
(log-msg 'NOTICE (task-name task) " - SKIPPED")))
(define-method (execute% (task <task>) (host <host>) (options <list>))
@ -154,6 +155,7 @@ may also be called outside of a blueprint (e.g. when a stand-alone task is run),
in which case it is a no-op."
(let ((triggered (*triggered-handlers*)))
(when triggered
(log-msg 'DEBUG "Scheduling handler: " handler-name)
(hash-table-set! triggered handler-name #t))))
(define-method (execute% (blueprint <blueprint>) (conn <connection>))

View file

@ -1,23 +0,0 @@
#|
This file is part of Ordo.
Copyright (C) 2025 Ray Miller
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, version 3.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
|#
(define-module (ordo util keyword-args)
#:use-module ((srfi srfi-1) #:select (member))
#:export (keyword-arg))
(define* (keyword-arg args kw #:optional (default #f))
(or (and=> (member kw args) cadr) default))