util: provide basic dotenv handling

This commit is contained in:
Helmut Merz 2025-07-04 11:40:06 +02:00
parent 0bc84cc02e
commit 357fe3e20c
2 changed files with 19 additions and 2 deletions

View file

@ -85,7 +85,10 @@
(== (util:plist-pairs '(:a "a" :b "b")) '((:a "a") (:b "b"))) (== (util:plist-pairs '(:a "a" :b "b")) '((:a "a") (:b "b")))
(let ((pl '(:a 0))) (let ((pl '(:a 0)))
(== (util:plist-add pl :b 1) '(:b 1 :a 0)) (== (util:plist-add pl :b 1) '(:b 1 :a 0))
(== pl '(:b 1 :a 0)))) (== pl '(:b 1 :a 0)))
(util:load-dotenv (t:test-path ".test.env"))
(== (util:getenv "SCOPES_USER") "user-from-env-file")
(== (util:getenv "SCOPES_PASSWORD") "very_secret"))
(deftest test-util-crypt () (deftest test-util-crypt ()
(let ((s1 (crypt:create-secret)) (let ((s1 (crypt:create-secret))

View file

@ -13,7 +13,9 @@
#:from-bytes #:to-bytes #:b64-decode #:b64-encode #:from-b64 #:to-b64 #:from-bytes #:to-bytes #:b64-decode #:b64-encode #:from-b64 #:to-b64
#:absolute-dir #:check-dir #:ensure-dir #:home-path #:path-from-string #:absolute-dir #:check-dir #:ensure-dir #:home-path #:path-from-string
#:relative-path #:runtime-path #:system-path #:relative-path #:runtime-path #:system-path
#:add-package-local-nickname)) #:getenv #:load-dotenv
#:add-package-local-nickname
))
(in-package :scopes/util) (in-package :scopes/util)
@ -162,3 +164,15 @@
(defun ensure-dir (p) (defun ensure-dir (p)
(ensure-directories-exist (directory-namestring p))) (ensure-directories-exist (directory-namestring p)))
;;;; environment and dotenv file handling
(defun load-dotenv (ep)
(maphash
(lambda (k v)
(unless (uiop:getenv k)
(setf (uiop:getenv k) v)))
(dotenv:read-env ep)))
(defun getenv (k &optional default)
(or (uiop:getenv k) default))