-
Notifications
You must be signed in to change notification settings - Fork 0
/
read-config.janet
82 lines (71 loc) · 2.1 KB
/
read-config.janet
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
(use ./lib)
(import ./futils :as fs)
(defn- keywordize-keys
[tab]
(let [[ks vs] [(keys tab) (values tab)]]
(-> ks
(|(map keyword $))
(interleave vs)
(splice)
(struct))))
(defn- to-lines
"Splits a multi-line string into an array of string, one for each line."
[s]
(if s (string/split "\n" s) []))
(defn- parse-key-value
``
Expects a string in the form `<key>=<value>`, returns `["key" "value"]`.
``
[s]
(->> s
(string/split "=")
(map string/trim))) # NOTE: Should I include whitespace?
(defn- filter-out-malformed-options
``
A proper option tuple looks like `["k" "v"]`.
For some reason nils cause this function to error, though i feel like they
should be taken care of by the first check.
``
[opt-arrays]
(filter
(and
|(indexed? $)
|(= 2 (length $))
(fn [[x y]] (and (string? x) (string? y))))
opt-arrays))
(defn- filter-out-nils
[ind]
(filter |(not (nil? $)) ind))
(defn parse-config-buffer
``
Expected format (for now) is <OPTION>=<value><whitespace><OPTION>=<value>...
Need to fit matching options into table matching options in config-defaults
``
[config]
(->> config
(to-lines)
(map parse-key-value)
(filter-out-nils)
(filter-out-malformed-options)
(from-pairs)
(keywordize-keys)))
# Where else could I search for configs? home? Should I source from multiple?
(def config-location
(let [xdg-loc (os/getenv "XDG_CONFIG_HOME")]
(if
(nil? xdg-loc)
(string (os/getenv "HOME") "/.config/pilot")
(string xdg-loc "/pilot"))))
(def config-defaults
{:script-path (string config-location "/scripts")
:template-path (string config-location "/templates")
:cat-provider "bat"
:pilot-editor (or (os/getenv "VISUAL") (os/getenv "EDITOR") "vi")
:copier-integration false})
(def config-file (string config-location "/config.cfg"))
(def settings
"Settings derived from the config file"
(merge
config-defaults
(parse-config-buffer (fs/read-all config-file))))
(def script-path (settings :script-path))