-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathednq.clj
executable file
·70 lines (61 loc) · 1.83 KB
/
ednq.clj
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
#!/usr/bin/env bb
(require '[cheshire.core :as c])
(require '[babashka.cli :as cli])
(defn json->edn [json-str]
(c/parse-string json-str true))
(defn edn->json [edn]
(c/generate-string edn {:pretty true}))
(defn type->edn
"Convert data of given type to edn."
[data in-type]
(cond
(= :edn in-type) data
(= :json in-type) (json->edn data)
:else (json->edn data)))
(defn edn->type
"Convert edn data to given type."
[data out-type]
(cond
(= :json out-type) (edn->json data)
(= :edn out-type) data
:else (edn->json data)))
(defn transform
"Transform data using the given Clojure function (in a string) with the given data types.
- fnstr: Clojure function string
- data: data to transform
- in-type: format of given data
- out-type: format to convert given data to
Allowed data types:
:edn, :json"
[fnstr data in-type out-type]
(let [f (eval (read-string fnstr))]
(-> data
(type->edn in-type)
f
(edn->type out-type))))
(def usage
(str
"Usage: ednq <fn> <data> <options>\n"
"\n"
"Options:\n"
"-i, --in=TYPE set the input data type\n"
"-o, --out=TYPE set the output data type\n"
"\n"
"TYPE can be: json(default), edn"))
(defn -main
[cli-args]
(let [{:keys [cmds opts]} (cli/parse-args cli-args)
{:keys [i in o out]} opts
[fnstr data] cmds
in-type (keyword (or i in))
out-type (keyword (or o out))
allowed-types #{:edn :json}]
(when (or (empty? data)
(empty? fnstr)
(not (contains? allowed-types in-type))
(not (contains? allowed-types out-type)))
(println usage)
(System/exit 1))
(let [result (transform fnstr data in-type out-type)]
(println result))))
(-main *command-line-args*)