Skip to content

Commit 3796051

Browse files
committed
Add a env var ECA_CONFIGFILE option to pass in config
1 parent b631d89 commit 3796051

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Add `ECA_CONFIGFILE` env var to allow point config file explicitly.
6+
57
## 0.71.2
68

79
- Fix regression in `/compact` command. #162

docs/configuration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ There are multiples ways to configure ECA:
4545
```bash
4646
ECA_CONFIG='{"myConfig": "my_value"}' eca server
4747
```
48+
49+
```bash
50+
ECA_CONFIGFILE='/home/alex/dev/testconfig.json' eca server
51+
```
4852

4953
## Providers / Models
5054

src/eca/config.clj

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
1. base: fixed config var `eca.config/initial-config`.
55
2. env var: searching for a `ECA_CONFIG` env var which should contains a valid json config.
66
3. local config-file: searching from a local `.eca/config.json` file.
7-
4. `initializatonOptions` sent in `initialize` request."
7+
4. `initializatonOptions` sent in `initialize` request.
8+
9+
When a `ECA_CONFIGFILE` env var set, it takes precedence and we only deep merge:
10+
1. base: fixed config var `eca.config/initial-config`.
11+
2. env file: searching for a `ECA_CONFIGFILE` env var which points to a json config file."
812
(:require
913
[camel-snake-kebab.core :as csk]
1014
[cheshire.core :as json]
@@ -23,6 +27,7 @@
2327
(def ^:private logger-tag "[CONFIG]")
2428

2529
(def ^:dynamic *env-var-config-error* false)
30+
(def ^:dynamic *env-file-config-error* false)
2631
(def ^:dynamic *global-config-error* false)
2732
(def ^:dynamic *local-config-error* false)
2833

@@ -143,11 +148,19 @@
143148
(logger/warn logger-tag "Error parsing config json:" (.getMessage e)))))
144149

145150
(defn ^:private config-from-envvar* []
146-
(some-> (System/getenv "ECA_CONFIG")
151+
(some-> (get-env "ECA_CONFIG")
147152
(safe-read-json-string (var *env-var-config-error*))))
148153

149154
(def ^:private config-from-envvar (memoize config-from-envvar*))
150155

156+
(defn ^:private config-from-envfile* []
157+
(when-some [path (get-env "ECA_CONFIGFILE")]
158+
(let [config-file (io/file path)]
159+
(when (.exists config-file)
160+
(safe-read-json-string (slurp config-file) (var *env-file-config-error*))))))
161+
162+
(def ^:private config-from-envfile (memoize config-from-envfile*))
163+
151164
(defn global-config-dir ^File []
152165
(let [xdg-config-home (or (get-env "XDG_CONFIG_HOME")
153166
(io/file (get-property "user.home") ".config"))]
@@ -265,17 +278,24 @@
265278
(defn all [db]
266279
(let [initialization-config @initialization-config*
267280
pure-config? (:pureConfig initialization-config)]
268-
(deep-merge initial-config
269-
(normalize-fields
270-
eca-config-normalization-rules
271-
(deep-merge initialization-config
272-
(when-not pure-config? (config-from-envvar))
273-
(when-not pure-config? (config-from-global-file))
274-
(when-not pure-config? (config-from-local-file (:workspace-folders db))))))))
281+
(if-some [envfile-config (config-from-envfile)]
282+
(deep-merge initial-config
283+
(normalize-fields
284+
eca-config-normalization-rules
285+
(deep-merge initialization-config
286+
(when-not pure-config? envfile-config))))
287+
(deep-merge initial-config
288+
(normalize-fields
289+
eca-config-normalization-rules
290+
(deep-merge initialization-config
291+
(when-not pure-config? (config-from-envvar))
292+
(when-not pure-config? (config-from-global-file))
293+
(when-not pure-config? (config-from-local-file (:workspace-folders db)))))))))
275294

276295
(defn validation-error []
277296
(cond
278297
*env-var-config-error* "ENV"
298+
*env-file-config-error* "ENVFILE"
279299
*global-config-error* "global"
280300
*local-config-error* "local"
281301

0 commit comments

Comments
 (0)