[PATCH consfigurator 1/3] create package consfigurator.data.util

David Bremner david at tethera.net
Sat Feb 19 19:43:41 GMT 2022


This package is intended to provide a home for utility functions used by
multiple data sources. Initially move a local function from
consfigurator.data.files-tree, and slightly generalize it to support an
extension or "type" argument.
---
 consfigurator.asd        |  1 +
 src/data/files-tree.lisp | 14 +++-----------
 src/data/util.lisp       | 30 ++++++++++++++++++++++++++++++
 src/package.lisp         |  6 +++++-
 4 files changed, 39 insertions(+), 12 deletions(-)
 create mode 100644 src/data/util.lisp

diff --git a/consfigurator.asd b/consfigurator.asd
index 3531ada..d159ba0 100644
--- a/consfigurator.asd
+++ b/consfigurator.asd
@@ -88,6 +88,7 @@
 	       (:file "src/connection/setuid")
 	       (:file "src/connection/as")
                (:file "src/connection/linux-namespace")
+               (:file "src/data/util")
                (:file "src/data/asdf")
                (:file "src/data/pgp")
 	       (:file "src/data/git-snapshot")
diff --git a/src/data/files-tree.lisp b/src/data/files-tree.lisp
index 993d9aa..9c95445 100644
--- a/src/data/files-tree.lisp
+++ b/src/data/files-tree.lisp
@@ -37,20 +37,12 @@ IDEN2 an an absolute or relative path are all supported."
     (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)
-               (merge-pathnames
-                (uiop:relativize-pathname-directory
-                 iden2)
-                (merge-pathnames
-                 (uiop:relativize-pathname-directory
-                  (ensure-directory-pathname iden1))
-                 base-path)))
-             (check (iden1 iden2)
-               (let ((file-path (%make-path iden1 iden2)))
+    (labels ((check (iden1 iden2)
+               (let ((file-path (util:data-file-path base-path iden1 iden2)))
                  (and (file-exists-p file-path)
                       (file-write-date file-path))))
              (extract (iden1 iden2)
-               (let ((file-path (%make-path iden1 iden2)))
+               (let ((file-path (util:data-file-path base-path iden1 iden2)))
                  (and (file-exists-p file-path)
                       (make-instance 'file-data
                                      :file file-path
diff --git a/src/data/util.lisp b/src/data/util.lisp
new file mode 100644
index 0000000..2c9c0d1
--- /dev/null
+++ b/src/data/util.lisp
@@ -0,0 +1,30 @@
+;;; Consfigurator -- Lisp declarative configuration management system
+
+;;; Copyright (C) 2022 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.util)
+(named-readtables:in-readtable :consfigurator)
+
+(defun data-file-path (base-path iden1 iden2 &key type)
+  "Generate a path from BASE-PATH, IDEN1 and IDEN2 by concatentation,
+optionally adding extension TYPE."
+  (merge-pathnames
+   (uiop:relativize-pathname-directory
+    (uiop:parse-unix-namestring iden2 :type type))
+   (merge-pathnames
+    (uiop:relativize-pathname-directory
+     (ensure-directory-pathname iden1))
+    base-path)))
diff --git a/src/package.lisp b/src/package.lisp
index 202477f..effac88 100644
--- a/src/package.lisp
+++ b/src/package.lisp
@@ -1004,6 +1004,9 @@
            (:local-nicknames (#:user #:consfigurator.property.user)
                              (#:lxc  #:consfigurator.property.lxc)))
 
+  (package :consfigurator.data.util
+           (:export #:data-file-path))
+
   (package :consfigurator.data.asdf)
 
   (package :consfigurator.data.pgp
@@ -1018,4 +1021,5 @@
 
   (package :consfigurator.data.local-file)
 
-  (package :consfigurator.data.files-tree))
+  (package :consfigurator.data.files-tree
+           (:local-nicknames (#:util  #:consfigurator.data.util))))
-- 
2.34.1




More information about the sgo-software-discuss mailing list