aboutsummaryrefslogtreecommitdiff
path: root/deepenv.scm
blob: fb7bae272c99cd33bf68e37b95e247caae967e9a (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
(import scheme
        (chicken base)
        (chicken pathname)
        (chicken file)
        (chicken io)
        (chicken string)
        (chicken process-context)
        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"
  (with-input-from-file ".env"
    (lambda () (read-list (current-input-port) read-env-line))))

(define (path-parents 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)))))



(let ((path-hierarchy (path-parents (current-directory))))
  path-hierarchy)