-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathutils.lisp
54 lines (48 loc) · 1.58 KB
/
utils.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
49
50
51
52
53
54
(defpackage #:aws-sdk/utils
(:use #:cl)
(:import-from #:ironclad
#:byte-array-to-hex-string
#:digest-sequence
#:ascii-string-to-byte-array)
(:import-from #:kebab
#:to-lisp-case)
(:import-from #:babel
#:octets-to-string)
(:export #:lispify
#:gethash+
#:ensure-string))
(in-package #:aws-sdk/utils)
(defun lispify (value &optional (package *package*))
(intern (string-upcase (kebab:to-lisp-case value)) package))
(defun getenv (var)
(let ((value (uiop:getenv var)))
(when (and (stringp value)
(string/= value ""))
value)))
(defun sha-256 (value)
(ironclad:byte-array-to-hex-string
(etypecase value
(string
(ironclad:digest-sequence :sha256
(ironclad:ascii-string-to-byte-array value)))
((simple-array (unsigned-byte 8) (*))
(ironclad:digest-sequence :sha256 value))
(pathname
(ironclad:digest-file :sha256 value)))))
(defun gethash+ (keys hash)
(reduce (lambda (hash key)
(when hash
(gethash key hash)))
keys
:initial-value hash))
(defun slurp-string-stream (stream)
(with-output-to-string (out)
(loop with buffer = (make-string 1024)
for read-bytes = (read-sequence buffer stream)
collect (write-string buffer out :end read-bytes)
while (= read-bytes 1024))))
(defun ensure-string (value)
(etypecase value
(string value)
(vector (babel:octets-to-string value))
(stream (slurp-string-stream value))))