Skip to content

Instantly share code, notes, and snippets.

@abishek
Created January 19, 2020 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abishek/3372b088ebc2f0196dfcd33e2ab9d0e7 to your computer and use it in GitHub Desktop.
Save abishek/3372b088ebc2f0196dfcd33e2ab9d0e7 to your computer and use it in GitHub Desktop.
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)
(remove-nils
(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