Skip to content

Commit fd61d25

Browse files
committed
Add :config-file cli option
1 parent 16d255c commit fd61d25

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
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 `:config-file` cli option to pass in config.
6+
57
## 0.72.2
68

79
- Run `preToolCall` hook before user approval if any. #170

src/eca/config.clj

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
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 `:config-file` from cli option is passed, it uses that instead of searching default locations."
810
(:require
911
[camel-snake-kebab.core :as csk]
1012
[cheshire.core :as json]
@@ -23,11 +25,14 @@
2325
(def ^:private logger-tag "[CONFIG]")
2426

2527
(def ^:dynamic *env-var-config-error* false)
28+
(def ^:dynamic *cli-file-config-error* false)
2629
(def ^:dynamic *global-config-error* false)
2730
(def ^:dynamic *local-config-error* false)
2831

2932
(def ^:private listen-idle-ms 3000)
3033

34+
(def config-file-path-from-cli* (atom nil))
35+
3136
(def initial-config
3237
{:providers {"openai" {:api "openai-responses"
3338
:url "https://api.openai.com"
@@ -148,6 +153,14 @@
148153

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

156+
(defn ^:private config-from-cli* []
157+
(when-some [path @config-file-path-from-cli*]
158+
(let [config-file (io/file path)]
159+
(when (.exists config-file)
160+
(safe-read-json-string (slurp config-file) (var *cli-file-config-error*))))))
161+
162+
(def ^:private config-from-cli (memoize config-from-cli*))
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"))]
@@ -262,6 +275,13 @@
262275
[:behavior :ANY :toolCall :approval :deny :ANY :argsMatchers]
263276
[:otlp]]})
264277

278+
(defn ^:private config-from-cli-or-default-location [pure-config? db]
279+
(if-some [config-from-cli (config-from-cli)]
280+
(when-not pure-config? config-from-cli)
281+
(deep-merge
282+
(when-not pure-config? (config-from-global-file))
283+
(when-not pure-config? (config-from-local-file (:workspace-folders db))))))
284+
265285
(defn all [db]
266286
(let [initialization-config @initialization-config*
267287
pure-config? (:pureConfig initialization-config)]
@@ -270,8 +290,7 @@
270290
eca-config-normalization-rules
271291
(deep-merge initialization-config
272292
(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))))))))
293+
(config-from-cli-or-default-location pure-config? db))))))
275294

276295
(defn validation-error []
277296
(cond

src/eca/main.clj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
(def log-levels #{"error" "warn" "info" "debug"})
4343

4444
(def cli-spec
45-
{:order [:help :version :verbose]
45+
{:order [:help :version :verbose :config-file]
4646
:spec {:help {:alias :h
4747
:desc "Print the available commands and its options"}
4848
:version {:desc "Print eca version"}
@@ -52,6 +52,10 @@
5252
:validate {:pred log-levels
5353
:ex-msg (fn [{:keys [_option _value]}]
5454
(format "Must be in %s" log-levels))}}
55+
:config-file {:ref "<FILE>"
56+
:desc "Path to a JSON config <FILE> to use instead of searching default locations"
57+
:coerce :string
58+
:default nil}
5559
:verbose {:desc "Use stdout for eca logs instead of default log settings"}}})
5660

5761
(defn ^:private parse-opts
@@ -92,6 +96,8 @@
9296
[action options]
9397
(proxy/load!)
9498
(when (= "server" action)
99+
(when-some [cfg-file (:config-file options)]
100+
(reset! config/config-file-path-from-cli* cfg-file))
95101
(alter-var-root #'logger/*level* (constantly (keyword (:log-level options))))
96102
(let [finished @(server/run-io-server! (:verbose options))]
97103
{:result-code (if (= :done finished) 0 1)})))

test/eca/main_test.clj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
[] {:options {:verbose m/absent}}
2323
["--verbose"] {:options {:verbose true}}
2424
["-v"] {:options {:verbose m/absent}}
25+
;; config-file
26+
[] {:options {:config-file m/absent}}
27+
["--config-file"] {:options {:config-file m/absent}}
28+
["--config-file" "/dev/config.json"] {:options {:config-file "/dev/config.json"}}
2529
#_()))
2630

2731
(deftest parse

0 commit comments

Comments
 (0)