Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A quick piece of code to read .env files. A line beginning with a # is treated as a comment. When this grows, I plan to make a separate project and push into quicklisp.
(defun load-env (pathname)
(with-open-file (stream pathname)
(read-env stream)))
(defun read-env (stream)
(loop for line = (read-line stream nil :eof)
until (eq line :eof)
collect (process-env-string line))))
(defun remove-nils (env-param-list)
(loop for env-param in env-param-list
if env-param
collect env-param))
(defun process-env-string (env-string)
(if (not (equalp (char env-string 0) #\#))
(split-string env-string)))
(defun split-string (env-string)
(let ((pos (position #\= env-string :test #'equal)))
(list (subseq env-string 0 pos) (subseq env-string (+ pos 1)))))
; run as follows
(load-env ".env")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment