forked from mighty-gerbils/gerbil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkvstorec.ss
89 lines (80 loc) · 2.75 KB
/
kvstorec.ss
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
;;; -*- Gerbil -*-
;;; (C) vyzo at hackzen.org
;;; kvstore command line client
package: tutorial/kvstore
(import :gerbil/gambit
:std/sugar
:std/getopt
:std/actor
:std/text/json
:tutorial/kvstore/proto)
(export main)
(def (kvstore-connect opt)
(let (rpcd (start-rpc-server! proto: (rpc-cookie-proto)))
(rpc-connect rpcd 'kvstore (hash-get opt 'server) kvstore::proto)))
(def (kvstore-get opt)
(let* ((remote (kvstore-connect opt))
(val (!!kvstore.get remote (hash-get opt 'key))))
(write-json val)
(newline)))
(def (kvstore-ref opt)
(let* ((remote (kvstore-connect opt))
(val (!!kvstore.ref remote (hash-get opt 'key))))
(write-json val)
(newline)))
(def (kvstore-put! opt)
(let* ((val (call-with-input-file (hash-get opt 'file) read-json))
(remote (kvstore-connect opt)))
(!!kvstore.put! remote (hash-get opt 'key) val)))
(def (kvstore-remove! opt)
(let (remote (kvstore-connect opt))
(!!kvstore.remove! remote (hash-get opt 'key))))
(def (main . args)
(def getcmd
(command 'get help: "get the json object associated with key or false if none is"
(argument 'key help: "object key, a string")))
(def refcmd
(command 'ref help: "get the json object associated with key or error"
(argument 'key help: "object key, a string")))
(def putcmd
(command 'put help: "put a json object to store"
(argument 'key help: "object key, a string")
(argument 'file help: "json file")))
(def delcmd
(command 'remove help: "remove an object from the store"
(argument 'key help: "object key, a string")))
(def helpcmd
(command 'help help: "display usage help"
(optional-argument 'command value: string->symbol)))
(def gopt
(getopt (option 'server "-s" "--server"
default: "127.0.0.1:9999"
help: "server rpc address")
getcmd
refcmd
putcmd
delcmd
helpcmd))
(try
(let ((values cmd opt) (getopt-parse gopt args))
(case cmd
((get) (kvstore-get opt))
((ref) (kvstore-ref opt))
((put) (kvstore-put! opt))
((remove) (kvstore-remove! opt))
((help)
(let (topic
(hash-get
(hash-eq (get getcmd)
(ref refcmd)
(put putcmd)
(remove delcmd)
(help helpcmd))
(hash-get opt 'command)))
(getopt-display-help (or topic gopt) "kvstorec")))))
(catch (getopt-error? exn)
(getopt-display-help exn "kvstorec" (current-error-port))
(exit 1))
(catch (remote-error? exn)
(displayln (error-message exn))
(exit 1))))