36 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			Common Lisp
		
	
	
	
	
	
			
		
		
	
	
			36 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			Common Lisp
		
	
	
	
	
	
| ;;;; cl-scopes/config
 | |
| 
 | |
| ;;;; Utilities for configuration of scopes services.
 | |
| 
 | |
| (defpackage :scopes/config
 | |
|   (:use :common-lisp)
 | |
|   (:export #:base 
 | |
|            #:override-keys #:env-prefix #:env-path))
 | |
| 
 | |
| (in-package :scopes/config)
 | |
| 
 | |
| (defclass base () 
 | |
|   ((override-keys :reader override-keys 
 | |
|                   :initform nil 
 | |
|                   :allocation :class)
 | |
|    (env-prefix :reader env-prefix
 | |
|                :initarg :env-prefix
 | |
|                :initform "SCOPES_")
 | |
|    (env-path :reader env-path
 | |
|              :initarg :env-path
 | |
|              :initform nil)))
 | |
| 
 | |
| (defmethod initialize-instance :after ((cfg base) &key &allow-other-keys)
 | |
|   (let* ((prefix (env-prefix cfg))
 | |
|          (ep (env-path cfg))
 | |
|          (dotenv-data (if ep (dotenv:read-env ep))))
 | |
|     (print dotenv-data)
 | |
|     (dolist (sl (override-keys cfg))
 | |
|       (let* ((key (str:concat prefix (string sl)))
 | |
|              (env-val (uiop:getenv key))
 | |
|              (dotenv-val (gethash key dotenv-data)))
 | |
|         (format t "~%key: ~s, env-val: ~s, dotenv-val: ~s  " key env-val dotenv-val)
 | |
|         (if env-val
 | |
|           (setf (slot-value cfg sl) env-val)
 | |
|           (if dotenv-val
 | |
|             (setf (slot-value cfg sl) dotenv-val)))))))
 |