-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathconfig.lisp
48 lines (43 loc) · 1.77 KB
/
config.lisp
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
(defpackage #:aws-sdk/utils/config
(:use #:cl)
(:import-from #:aws-sdk/utils
#:getenv)
(:import-from #:parser.ini
#:*include-empty-sections?*
#:parse)
(:export #:parse-file
#:read-from-file
#:*aws-profile*))
(in-package #:aws-sdk/utils/config)
(defvar *aws-profile* (or (getenv "AWS_PROFILE") "default"))
(defvar *cached-ini* (make-hash-table :test 'equal))
(defun %parse-file (file)
(let* ((parser.ini:*include-empty-sections?* t)
(data (parser.ini:parse file 'list)))
(mapcar (lambda (section)
(cons (first (getf section :name))
(mapcar (lambda (option)
(cons (first (getf (first option) :name))
(getf (first option) :value)))
(getf (getf section :section) :section-option))))
data)))
(defun parse-file (file)
(let ((filename (namestring file)))
(when (and (gethash filename *cached-ini*)
(<= (file-write-date filename)
(car (gethash filename *cached-ini*))))
(return-from parse-file (cdr (gethash filename *cached-ini*))))
(let ((data (%parse-file file)))
(setf (gethash filename *cached-ini*)
(cons (file-write-date file) data))
data)))
(defun read-from-file (file &key (profile *aws-profile*) allow-no-profile)
(let* ((data (parse-file file))
(section
(or (assoc profile data :test 'equal)
;; Fallback to 'profile <name>'
(assoc (format nil "profile ~A" profile) data :test 'equal))))
(when (and (null section)
(not allow-no-profile))
(error "Profile '~A' doesn't exist in '~A'." profile file))
(cdr section)))