-
Notifications
You must be signed in to change notification settings - Fork 28
/
platform.clj
75 lines (58 loc) · 1.39 KB
/
platform.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
71
72
73
74
(ns cli-matic.platform
"
## Platform-specific functions for the JVM.
If running on ClojureScript, we can have a different file for JS.
BTW, in this NS, we avoid using Spec / Orchestra.
**DO NOT** define macros in this namespace - see [[cli-matic.platform-macros]]
"
(:require [clojure.edn :as edn]))
(defn read-env
"Reads an environment variable.
If undefined, returns nil."
[var]
(System/getenv var))
(defn exit-script
"Terminates execution with a return value."
[retval]
(System/exit retval))
(defn add-shutdown-hook
"Add a shutdown hook. If `nil`, simply ignores it.
The shutdown hook is run in a new thread.
"
[fnToCallOnShutdown]
(if (ifn? fnToCallOnShutdown)
(.addShutdownHook
(Runtime/getRuntime)
(Thread. fnToCallOnShutdown))))
(defn slurp-file
"No slurping in JavaScript. So we have to move this to
platform."
[f]
(slurp f))
;
; Conversions
;
(defn parseInt
"Converts a string to an integer. "
[s]
(Integer/parseInt s))
(defn parseFloat
"Converts a string to a float."
[s]
(Float/parseFloat s))
(defn asDate
"Converts a string in format yyyy-mm-dd to a
Date object; if conversion
fails, returns nil."
[s]
(try
(.parse
(java.text.SimpleDateFormat. "yyyy-MM-dd") s)
(catch Throwable t
nil)))
(defn parseEdn
"
Decodes EDN through clojure.edn.
"
[edn-in]
(edn/read-string edn-in))