blob: ae0b3c30537e1939117a09d3a3a62812983b98d6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
(import scheme
(chicken base)
(chicken pathname)
(chicken file)
(chicken io)
(chicken string)
(chicken process-context)
srfi-1
srfi-13
srfi-14)
(define (dir-has-.env? dir)
(file-exists? (make-pathname dir ".env")))
(define (split-env-line line)
(let* ((definition (string-split line "="))
(key (car definition))
(value (string-trim-both (cadr definition)
(list->char-set (list #\") char-set:whitespace))))
(cons key value)))
(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))))
(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)))
'())))
(define (path-parts path)
(call-with-values
(lambda () (decompose-directory path))
(lambda (origin base elts)
(if (not (eq? origin #f))
(cons origin elts)
(if (not (eq? base #f))
(cons base elts)
elts)))))
(define (path-parents path)
(fold (lambda (item acc)
(if (> (length acc) 0)
(if (string=? (car acc) "/")
(cons (conc "/" item) acc)
(cons (conc (car acc) "/" item) acc))
(list item)))
'()
(path-parts path)))
(define (.env-union alist2 alist1)
(lset-union (lambda (a b) (string=? (car a) (car b)))
alist2 alist1))
(let ((parents (path-parents (current-directory))))
(fold .env-union '()
(reverse (map load-dir-.env parents))))
|