From a1b60d6059da1f9862071f12441d2eb16f0b75e7 Mon Sep 17 00:00:00 2001 From: Gene Pasquet Date: Thu, 5 Dec 2024 16:34:22 +0000 Subject: Support empty lines and comment lines --- .env | 3 +++ deepenv.scm | 29 ++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/.env b/.env index ca809d6..7e9f4de 100644 --- a/.env +++ b/.env @@ -1,2 +1,5 @@ FOO=123 + +# A shitty comment + # a comment a bit away BAR="KOIN" diff --git a/deepenv.scm b/deepenv.scm index 0f31827..98c9400 100644 --- a/deepenv.scm +++ b/deepenv.scm @@ -36,19 +36,38 @@ (list->char-set (list #\") char-set:whitespace)))) (cons key value))) +(define (line-empty? line) + (string=? "" (string-trim-both line))) + +(define (line-comment? line) + (let ((clean-line (string-trim-both line))) + (and (> (string-length clean-line) 0) + (eq? (string-ref clean-line 0) #\#)))) + +(define (line-eof? line) + (eq? line #!eof)) + +(define (line-ignore? line) + (or (line-empty? line) + (line-comment? line))) + (define (read-env-line port) "Read an env-definition line from PORT and return as a cons cell" (let ((line (read-line port))) - (if (eq? line #!eof) - #!eof - (split-env-line line)))) + (cond + ((line-eof? line) #!eof) + ((line-ignore? line) '()) + (else (split-env-line line))))) (define (load-dir-.env dir) "Load a .env file present in `.dir` and return its environment definitions as a alist" (let ((.env-file (conc dir "/.env"))) (if (file-exists? .env-file) - (with-input-from-file .env-file - (lambda () (read-list (current-input-port) read-env-line))) + (filter + (lambda (item) + (not (null? item))) + (with-input-from-file .env-file + (lambda () (read-list (current-input-port) read-env-line)))) '()))) (define (path-parts path) -- cgit v1.2.3