diff options
author | sterni <sternenseemann@systemli.org> | 2023-03-17T16·36+0100 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2023-03-24T18·32+0000 |
commit | 80e168b22d602e2d2b8b872beadacc64460bd670 (patch) | |
tree | b87796e4df8a06583feffca6a9a16ff735432be0 /users/sterni/mblog/config.lisp | |
parent | fee0c83915918f60c55c25a9c48c59b4e92eb598 (diff) |
feat(sterni/mblog): implement environment based config mechanism r/6040
Change-Id: I091c0d5decc0a1eb3d24e81b713434ab391c677d Reviewed-on: https://cl.tvl.fyi/c/depot/+/8347 Reviewed-by: sterni <sternenseemann@systemli.org> Autosubmit: sterni <sternenseemann@systemli.org> Tested-by: BuildkiteCI
Diffstat (limited to 'users/sterni/mblog/config.lisp')
-rw-r--r-- | users/sterni/mblog/config.lisp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/users/sterni/mblog/config.lisp b/users/sterni/mblog/config.lisp new file mode 100644 index 000000000000..08e2a872f6a5 --- /dev/null +++ b/users/sterni/mblog/config.lisp @@ -0,0 +1,31 @@ +;; SPDX-License-Identifier: GPL-3.0-only +;; SPDX-FileCopyrightText: Copyright (C) 2023 by sterni + +(in-package :config) + +(eval-when (:compile-toplevel :load-toplevel) + (defun plist-to-alist (lst) + (loop for (name . (default . (parser . nil))) on lst by #'cdddr + collect (cons name (list default parser)))) + + (defun symbol-to-env-var-name (symbol) + (concatenate 'string + "MBLOG_" + (string-upcase + (remove #\* (substitute #\_ #\- (string symbol))))))) + +(defmacro define-configuration-variables (&rest args) + (let ((vars (plist-to-alist args)) + (val-var-sym (gensym))) + `(progn + ,@(loop for (name . (default nil)) in vars + collect `(defvar ,name ,default)) + + (defun init-from-env () + ,@(loop for (name . (nil parser)) in vars + collect + `(when-let ((,val-var-sym (getenv ,(symbol-to-env-var-name name)))) + (setf ,name (funcall ,parser ,val-var-sym)))))))) + +(define-configuration-variables + *general-buffer-size* 4096 #'parse-integer) |