Add support for facts.

This commit is contained in:
Ray Miller 2025-01-13 20:15:41 +00:00
parent 5376ce9f19
commit 7f507c8e6d
Signed by: ray
GPG key ID: 043F786C4CD681B8
4 changed files with 64 additions and 10 deletions

18
modules/ordo/facts.scm Normal file
View file

@ -0,0 +1,18 @@
(define-module (ordo facts)
#:use-module (ordo facts user)
#:export (gather-facts
get-fact))
(define (get-fact facts . keys)
(cond
((null? keys) facts)
((list? facts) (let ((facts (assoc-ref facts (car keys))))
(apply get-fact facts (cdr keys))))
(else #f)))
(define (gather-facts conn)
(let* ((id (fact:id conn))
(user-name (get-fact id #:user #:name))
(pwent (fact:pwent conn user-name)))
`((#:id . ,id)
(#:pwent . ,pwent))))

View file

@ -0,0 +1,31 @@
(define-module (ordo facts user)
#:use-module (rx irregex)
#:use-module (srfi srfi-1) ; list utils
#:use-module (srfi srfi-2) ; and-let*
#:use-module (ordo connection)
#:export (fact:id
fact:pwent))
(define (parse-id-output s)
(let ((data (reverse (irregex-fold (irregex '(seq (=> id integer) "(" (=> name (+ alphanumeric)) ")"))
(lambda (_ m accum)
(cons `((#:id . ,(string->number (irregex-match-substring m 'id)))
(#:name . ,(irregex-match-substring m 'name)))
accum))
'()
s))))
`((#:user . ,(first data))
(#:group . ,(second data))
(#:groups . ,(drop data 2)))))
(define (fact:id conn)
(run conn "id" #:check? #t #:return (compose parse-id-output car)))
(define (parse-passwd-entry s)
(map cons
'(#:user-name #:password #:user-id #:group-id #:gecos #:home-dir #:shell)
(string-split s #\:)))
(define (fact:pwent conn user-name)
(run conn "getent" "passwd" user-name
#:check? #t #:return (compose parse-passwd-entry car)))