aboutsummaryrefslogtreecommitdiff
path: root/tools/obu-config
blob: d6358346e905598e78de05bb2cfdf35a102c938e (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
65
#!/usr/bin/env gosh

(use text.edn)
(use file.util)
(use scheme.mapping.hash)
(use gauche.parseopt)

(define (show-help progname)
  (display (format
"~a: tracktrain's onboard config-and-state manager.

Usage: ~a [options] key [value]

This is a very simple key-value store. Give both to set,
give just the key to look something up

Options:
 -s --statefile: state file
 -d --delete: explicitly delete an entry
 -h --help: display this help
" progname progname))
  (exit 0))

(define (main args)
  (let-args
   (cdr args)
   ((statefile "s|state=s")
    (delete "d|delete")
    (help "h|help" => (cut show-help (car args)))
   . restargs)

  (define file
    (if (equal? statefile #f)
        "./obu-state.edn"
        statefile))

  (if (or delete (= (length restargs) 2))
      (set file
           (list-ref restargs 0)
           (if delete #f (list-ref restargs 1))
           delete)
      (display
       (get file
            (list-ref restargs 0))))
  (exit 0)))

(define (set file key value delete)
  (define data
    (if (file-exists? file)
        (call-with-input-file file parse-edn)
        (edn-map)))
  (define data2
    (if delete
        (hashmap-delete data (string->symbol key))
        (hashmap-set data (string->symbol key) value)))
  (call-with-output-file file
    (cut construct-edn data2 <>)))

(define (get file key)
  (if (file-exists? file)
      (guard (e [else #f])
        (hashmap-ref
       (call-with-input-file file parse-edn)
       (string->symbol key)))
      #f))