feat(sterni/mblog): implement environment based config mechanism

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
This commit is contained in:
sterni 2023-03-17 17:36:57 +01:00 committed by clbot
parent fee0c83915
commit 80e168b22d
6 changed files with 54 additions and 14 deletions

View file

@ -1,5 +1,5 @@
;; SPDX-License-Identifier: GPL-3.0-only
;; SPDX-FileCopyrightText: Copyright (C) 2022 by sterni
;; SPDX-FileCopyrightText: Copyright (C) 2022-2023 by sterni
(in-package :cli)
(declaim (optimize (safety 3)))
@ -49,6 +49,7 @@
(defun main ()
"Dispatch to correct main function based on arguments and UIOP:ARGV0."
(config:init-from-env)
(multiple-value-bind (flags args)
(partition-by (lambda (x) (starts-with #\- x))
(uiop:command-line-arguments))

View file

@ -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)

View file

@ -1,5 +1,5 @@
# SPDX-License-Identifier: GPL-3.0-only
# SPDX-FileCopyrightText: Copyright (C) 2022 by sterni
# SPDX-FileCopyrightText: Copyright (C) 2022-2023 by sterni
{ depot, pkgs, ... }:
(depot.nix.buildLisp.program {
@ -7,6 +7,7 @@
srcs = [
./packages.lisp
./config.lisp
./maildir.lisp
./transformer.lisp
./note.lisp

View file

@ -1,5 +1,5 @@
;; SPDX-License-Identifier: GPL-3.0-only
;; SPDX-FileCopyrightText: Copyright (C) 2022 by sterni
;; SPDX-FileCopyrightText: Copyright (C) 2022-2023 by sterni
;; SPDX-FileCopyrightText: Copyright (C) 2006-2010 by Walter C. Pelissero
(in-package :mblog)
@ -26,12 +26,10 @@
:if-does-not-exist :create)
,@body))
(defvar *copy-buffer-size* 4096)
(defun redirect-stream (in out)
"Consume input stream IN and write all its content to output stream OUT.
The streams' element types need to match."
(let ((buf (make-array *copy-buffer-size* :element-type (stream-element-type in))))
(let ((buf (make-array config:*general-buffer-size* :element-type (stream-element-type in))))
(loop for pos = (read-sequence buf in)
while (> pos 0)
do (write-sequence buf out :end pos))))

View file

@ -1,18 +1,15 @@
;; SPDX-License-Identifier: GPL-3.0-only
;; SPDX-FileCopyrightText: Copyright (C) 2022 by sterni
;; SPDX-FileCopyrightText: Copyright (C) 2022-2023 by sterni
(in-package :note)
(declaim (optimize (safety 3)))
;;; util
;; TODO(sterni): merge this with mblog::*copy-buffer-size*
(defvar *copy-buffer-size* 4096)
(defun html-escape-stream (in out)
"Escape characters read from stream IN and write them to
stream OUT escaped using WHO:ESCAPE-STRING-MINIMAL."
(let ((buf (make-string *copy-buffer-size*)))
(let ((buf (make-string config:*general-buffer-size*)))
(loop for len = (read-sequence buf in)
while (> len 0)
do (write-string (who:escape-string-minimal (subseq buf 0 len)) out))))

View file

@ -1,5 +1,5 @@
;; SPDX-License-Identifier: GPL-3.0-only
;; SPDX-FileCopyrightText: Copyright (C) 2022 by sterni
;; SPDX-FileCopyrightText: Copyright (C) 2022-2023 by sterni
(defpackage :maildir
(:use :common-lisp)
@ -8,12 +8,22 @@
(:documentation
"Very incomplete package for dealing with maildir(5)."))
(defpackage :config
(:use
:common-lisp)
(:import-from :uiop :getenv)
(:import-from :alexandria :when-let)
(:export
:init-from-env
:*general-buffer-size*))
(defpackage :note
(:use
:common-lisp
:closure-html
:cl-date-time-parser
:mime4cl)
:mime4cl
:config)
(:import-from
:alexandria
:when-let*
@ -36,7 +46,8 @@
:klatre
:who
:maildir
:note)
:note
:config)
(:export :build-mblog)
(:import-from :local-time :universal-to-timestamp)
(:shadowing-import-from :common-lisp :list))
@ -46,6 +57,7 @@
:common-lisp
:uiop
:note
:config
:mblog)
(:import-from :alexandria :starts-with)
(:export :main))