[PATCH] add `files-tree' data source

David Bremner david at tethera.net
Thu Dec 9 01:00:32 GMT 2021


This allows the user to conveniently maintain a set of files in their consfig
tree (or elsewhere) for deployment as data.

Signed-off-by: David Bremner <david at tethera.net>
---

I didn't explicitely talk about the separator since in principle this
is OS agnostic. OTOH, maybe that's silly, so feel free to reword/

 consfigurator.asd        |  3 ++-
 src/data/files-tree.lisp | 56 ++++++++++++++++++++++++++++++++++++++++
 src/package.lisp         |  4 ++-
 3 files changed, 61 insertions(+), 2 deletions(-)
 create mode 100644 src/data/files-tree.lisp

diff --git a/consfigurator.asd b/consfigurator.asd
index 4ba46b1..61f1208 100644
--- a/consfigurator.asd
+++ b/consfigurator.asd
@@ -92,7 +92,8 @@
 	       (:file "src/data/git-snapshot")
 	       (:file "src/data/gpgpubkeys")
                (:file "src/data/ssh-askpass")
-               (:file "src/data/local-file"))
+               (:file "src/data/local-file")
+               (:file "src/data/files-tree"))
   :in-order-to ((test-op (test-op "consfigurator/tests"))))
 
 (defsystem "consfigurator/tests"
diff --git a/src/data/files-tree.lisp b/src/data/files-tree.lisp
new file mode 100644
index 0000000..0c8d1bf
--- /dev/null
+++ b/src/data/files-tree.lisp
@@ -0,0 +1,56 @@
+;;; Consfigurator -- Lisp declarative configuration management system
+
+;;; Copyright (C) 2021  David Bremner <david at tethera.net>
+
+;;; This file 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; either version 3, or (at your option)
+;;; any later version.
+
+;;; This file 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 <http://www.gnu.org/licenses/>.
+
+(in-package :consfigurator.data.files-tree)
+(named-readtables:in-readtable :consfigurator)
+
+(defmethod register-data-source ((type (eql :files-tree)) &key location)
+  "Provide the contents of a local directory on the machine running the root
+Lisp.  Register this data source multiple times to provide multiple trees.
+LOCATION is either a string defining the path to the root of the file tree, or
+a symbol defining an `asdf' package where the tree is contained in the
+subdirectory `data/'.  Individual data items (files) are specified by string
+IDEN1 as one or more levels of subdirectory and IDEN2 as file name.  The
+version of the data provided is the file's last modification time."
+  (let* ((base-path (if (symbolp location)
+                        (asdf:system-relative-pathname location "data/")
+                        (ensure-directory-pathname location)))
+         (base-dir (pathname-directory base-path)))
+    (unless (directory-exists-p base-path)
+      (missing-data-source
+       "~A does not exist, or is not a directory." base-path))
+    (labels ((%make-path (iden1 iden2)
+               ;; cdr drops symbol distinguishing absolute and relative paths.
+               (let ((subdir
+                       (cdr (pathname-directory
+                             (ensure-directory-pathname iden1)))))
+                 (make-pathname
+                  :directory (append base-dir subdir)
+                  :name iden2)))
+             (check (iden1 iden2)
+               (let ((file-path (%make-path iden1 iden2)))
+                 (and (file-exists-p file-path)
+                      (file-write-date file-path))))
+             (extract (iden1 iden2)
+               (let ((file-path (%make-path iden1 iden2)))
+                 (and (file-exists-p file-path)
+                      (make-instance 'file-data
+                                     :file file-path
+                                     :iden1 iden1
+                                     :iden2 iden2
+                                     :version (file-write-date file-path))))))
+      (cons #'check #'extract))))
diff --git a/src/package.lisp b/src/package.lisp
index 051bfab..dbae766 100644
--- a/src/package.lisp
+++ b/src/package.lisp
@@ -998,4 +998,6 @@
   (package :consfigurator.data.ssh-askpass
            (:local-nicknames (#:re   #:cl-ppcre)))
 
-  (package :consfigurator.data.local-file))
+  (package :consfigurator.data.local-file)
+
+  (package :consfigurator.data.files-tree))
-- 
2.33.0




More information about the sgo-software-discuss mailing list