about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--users/sterni/mblog/cli.lisp3
-rw-r--r--users/sterni/mblog/config.lisp31
-rw-r--r--users/sterni/mblog/default.nix3
-rw-r--r--users/sterni/mblog/mblog.lisp6
-rw-r--r--users/sterni/mblog/note.lisp7
-rw-r--r--users/sterni/mblog/packages.lisp18
6 files changed, 54 insertions, 14 deletions
diff --git a/users/sterni/mblog/cli.lisp b/users/sterni/mblog/cli.lisp
index d9006f8ed77f..555f8def535f 100644
--- a/users/sterni/mblog/cli.lisp
+++ b/users/sterni/mblog/cli.lisp
@@ -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))
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)
diff --git a/users/sterni/mblog/default.nix b/users/sterni/mblog/default.nix
index 0c4f2527a85e..6ad8a10ce378 100644
--- a/users/sterni/mblog/default.nix
+++ b/users/sterni/mblog/default.nix
@@ -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
diff --git a/users/sterni/mblog/mblog.lisp b/users/sterni/mblog/mblog.lisp
index 36a31376c8df..005850fba8dd 100644
--- a/users/sterni/mblog/mblog.lisp
+++ b/users/sterni/mblog/mblog.lisp
@@ -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))))
diff --git a/users/sterni/mblog/note.lisp b/users/sterni/mblog/note.lisp
index 21257fc7b47a..d44bcc0d0a1a 100644
--- a/users/sterni/mblog/note.lisp
+++ b/users/sterni/mblog/note.lisp
@@ -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))))
diff --git a/users/sterni/mblog/packages.lisp b/users/sterni/mblog/packages.lisp
index ffc9e3512a69..03c33f7efe12 100644
--- a/users/sterni/mblog/packages.lisp
+++ b/users/sterni/mblog/packages.lisp
@@ -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))