summaryrefslogtreecommitdiff
path: root/shroud/secret.scm
blob: f9f499aaf920a2be29cb037643133565131dbda7 (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
;;; Shroud
;;; Copyright © 2015 David Thompson <davet@gnu.org>
;;;
;;; Shroud is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Shroud is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Shroud.  If not, see <http://www.gnu.org/licenses/>.

(define-module (shroud secret)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-9)
  #:export (make-secret
            secret?
            secret-id
            secret-content
            alist->secret
            secret->alist
            load-secrets
            save-secrets))

(define-record-type <secret>
  (make-secret id content)
  secret?
  (id      secret-id)
  (content secret-content))

(define (alist->secret alist)
  "Convert ALIST into a <secret> record."
  (make-secret (assq-ref alist 'id) (assq-ref alist 'content)))

(define (secret->alist secret)
  "Convert SECRET into an alist."
  (match secret
    (($ <secret> id content)
     `((id . ,id)
       (content . ,content)))))

(define (load-secrets file)
  "Load secrets from FILE, or return '() if FILE does not exist."
  (if (file-exists? file)
      (map alist->secret (call-with-input-file file read))
      '()))

(define (save-secrets secrets file)
  "Write SECRETS to FILE."
  (call-with-output-file file
    (lambda (port)
      (write (map secret->alist secrets) port))))