diff --git a/test/test-core.lisp b/test/test-core.lisp index 3a0e812..87c587d 100644 --- a/test/test-core.lisp +++ b/test/test-core.lisp @@ -85,7 +85,10 @@ (== (util:plist-pairs '(:a "a" :b "b")) '((:a "a") (:b "b"))) (let ((pl '(: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 () (let ((s1 (crypt:create-secret)) diff --git a/util/util.lisp b/util/util.lisp index 484cfab..b07d63f 100644 --- a/util/util.lisp +++ b/util/util.lisp @@ -13,7 +13,9 @@ #:from-bytes #:to-bytes #:b64-decode #:b64-encode #:from-b64 #:to-b64 #:absolute-dir #:check-dir #:ensure-dir #:home-path #:path-from-string #:relative-path #:runtime-path #:system-path - #:add-package-local-nickname)) + #:getenv #:load-dotenv + #:add-package-local-nickname + )) (in-package :scopes/util) @@ -162,3 +164,15 @@ (defun ensure-dir (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))