-
Notifications
You must be signed in to change notification settings - Fork 28
/
presets.cljc
116 lines (93 loc) · 2.87 KB
/
presets.cljc
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
(ns cli-matic.presets
"
### Known presets
"
(:require [clojure.string :as str]
[cli-matic.optionals :as OPT]
[cli-matic.platform :as P]
))
;; Known presets
(defn asSingleString
"Turns a filename into a single string.
If argument is a String, it tries to resolve it first as a URI, then\n as a local file name. URIs with a 'file' protocol are converted to\n local file names.
"
[filename]
(cond
(nil? filename) ""
(empty? filename) ""
:else (slurp filename)))
(defn asLinesString
"Reads a text file and returns it as a collection of lines."
[filename]
(str/split-lines (asSingleString filename)))
(defn asDecodedEdnValue
"Decodes the value as an EDN structure."
[s]
;;(edn/read-string (if (string? s) s (str/join s)))
(P/parseEdn s))
(defn asDecodedEdnFile
"Decodes the contents of a file as a JSON object."
[filename]
(P/parseEdn (asSingleString filename)))
(defn asDecodedJsonValue
"Decodes the value as a JSON object."
[s]
(OPT/json-decode-cheshire s))
(defn asDecodedJsonFile
"Decodes the contents of a file as a JSON object."
[filename]
(OPT/json-decode-cheshire (asSingleString filename)))
(defn asDecodedYamlValue
"Decodes the value as a YAML object."
[s]
(OPT/yaml-decode s))
(defn asDecodedYamlFile
"Decodes the contents of a file as a JSON object."
[filename]
(OPT/yaml-decode (asSingleString filename)))
(defn- replace-double-colon
[s]
(if (str/starts-with? s "::")
(str/replace s "::" ":user/")
s))
(defn asKeyword
[s]
(-> s replace-double-colon P/parseEdn keyword))
;; Remember to add these to
;; ::S/type
(def known-presets
{:int {:parse-fn P/parseInt
:placeholder "N"}
:int-0 {:parse-fn P/parseInt
:placeholder "N"
:default 0}
:float {:parse-fn P/parseFloat
:placeholder "N.N"}
:float-0 {:parse-fn P/parseFloat
:placeholder "N.N"
:default 0.0}
:string {:placeholder "S"}
:keyword {:placeholder "S"
:parse-fn asKeyword}
:slurp {:parse-fn asSingleString
:placeholder "f"}
:slurplines {:parse-fn asLinesString
:placeholder "f"}
:edn {:parse-fn asDecodedEdnValue
:placeholder "edn"}
:ednfile {:parse-fn asDecodedEdnFile
:placeholder "f"}
:json {:parse-fn asDecodedJsonValue
:placeholder "json"}
:jsonfile {:parse-fn asDecodedJsonFile
:placeholder "f"}
:yaml {:parse-fn asDecodedYamlValue
:placeholder "yaml"}
:yamlfile {:parse-fn asDecodedYamlFile
:placeholder "f"}
; dates
:yyyy-mm-dd {:placeholder "YYYY-MM-DD" :parse-fn P/asDate}
;;:validate [#(true)
;; "Must be a date in format YYYY-MM-DD"]
})
(OPT/orchestra-instrument)