Skip to content

Commit 90891fd

Browse files
fix(cli): config.edn reading and writing incorrectly
from a literal '~' directory under current directory because it wasn't expanded
1 parent 6d91d8c commit 90891fd

2 files changed

Lines changed: 29 additions & 23 deletions

File tree

src/main/logseq/cli/config.cljs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
[clojure.string :as string]
55
[goog.object :as gobj]
66
["fs" :as fs]
7-
["os" :as os]
87
["path" :as node-path]
9-
[logseq.cli.output-mode :as output-mode]))
8+
[logseq.cli.output-mode :as output-mode]
9+
[logseq.cli.root-dir :as root-dir]))
1010

1111
(defn- parse-int
1212
[value]
@@ -30,19 +30,15 @@
3030

3131
:else nil))
3232

33-
(defn- default-root-dir
34-
[]
35-
"~/logseq")
36-
3733
(defn- default-config-path
3834
([]
39-
(default-config-path (default-root-dir)))
35+
(default-config-path (root-dir/default-root-dir)))
4036
([root-dir]
4137
(node-path/join root-dir "cli.edn")))
4238

4339
(defn server-list-path
4440
[root-dir]
45-
(node-path/join (or root-dir (node-path/join (.homedir os) "logseq")) "server-list"))
41+
(node-path/join (or root-dir (root-dir/default-root-dir)) "server-list"))
4642

4743
(def ^:private removed-config-keys
4844
#{:auth-token :retries :e2ee-password})
@@ -67,7 +63,7 @@
6763

6864
(defn update-config!
6965
[{:keys [config-path root-dir]} updates]
70-
(let [path (or config-path (default-config-path (or root-dir (default-root-dir))))
66+
(let [path (or config-path (default-config-path (root-dir/normalize-root-dir root-dir)))
7167
current (or (read-config-file path) {})
7268
filtered-current (sanitize-file-config current)
7369
filtered-updates (sanitize-file-config updates)
@@ -115,21 +111,23 @@
115111
:logout-timeout-ms 120000
116112
:list-title-max-display-width list-title-max-display-width-default
117113
:output-format nil
118-
:root-dir (default-root-dir)
114+
:root-dir (root-dir/default-root-dir)
119115
:ws-url "wss://api.logseq.io/sync/%s"
120116
:http-base "https://api.logseq.io"}
121117
env (env-config)
122-
root-dir (or (:root-dir opts)
123-
(:root-dir env)
124-
(:root-dir defaults))
118+
root-dir (root-dir/normalize-root-dir
119+
(or (:root-dir opts)
120+
(:root-dir env)
121+
(:root-dir defaults)))
125122
config-path (or (:config-path opts)
126123
(:config-path env)
127124
(default-config-path root-dir))
128125
file-config (or (read-config-file config-path) {})
129-
root-dir (or (:root-dir opts)
130-
(:root-dir env)
131-
(:root-dir file-config)
132-
root-dir)
126+
root-dir (root-dir/normalize-root-dir
127+
(or (:root-dir opts)
128+
(:root-dir env)
129+
(:root-dir file-config)
130+
root-dir))
133131
output-format (or (output-mode/parse (:output-format opts))
134132
(output-mode/parse (:output opts))
135133
(output-mode/parse (:output-format env))

src/test/logseq/cli/config_test.cljs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
result (with-env env #(config/resolve-config opts))]
4949
(is (= cfg-path (:config-path result)))
5050
(is (= "cli-repo" (:graph result)))
51-
(is (= "cli-root" (:root-dir result)))
51+
(is (= (node-path/resolve "cli-root") (:root-dir result)))
5252
(is (= 333 (:timeout-ms result)))
5353
(is (= 888 (:login-timeout-ms result)))
5454
(is (= 999 (:logout-timeout-ms result)))
@@ -65,7 +65,7 @@
6565
"LOGSEQ_CLI_ROOT_DIR" "env-root"}
6666
result (with-env env #(config/resolve-config {:config-path cfg-path}))]
6767
(is (= "env-repo" (:graph result)))
68-
(is (= "env-root" (:root-dir result)))))
68+
(is (= (node-path/resolve "env-root") (:root-dir result)))))
6969

7070
(deftest test-output-format-env-overrides-file
7171
(let [dir (node-helper/create-tmp-dir)
@@ -110,7 +110,7 @@
110110
"LOGSEQ_CLI_CONFIG" nil}
111111
#(config/resolve-config {:config-path cfg-path}))]
112112
(is (= cfg-path (:config-path result)))
113-
(is (= "~/logseq" (:root-dir result)))
113+
(is (= (node-path/join (.homedir os) "logseq") (:root-dir result)))
114114
(is (= "wss://api.logseq.io/sync/%s" (:ws-url result)))
115115
(is (= "https://api.logseq.io" (:http-base result)))
116116
(is (= 10000 (:timeout-ms result)))
@@ -127,8 +127,8 @@
127127
"LOGSEQ_CLI_OUTPUT" nil
128128
"LOGSEQ_CLI_CONFIG" nil}
129129
#(config/resolve-config {:root-dir "~/custom-logseq"}))]
130-
(is (= "~/custom-logseq" (:root-dir result)))
131-
(is (= (node-path/join "~/custom-logseq" "cli.edn")
130+
(is (= (node-path/join (.homedir os) "custom-logseq") (:root-dir result)))
131+
(is (= (node-path/join (.homedir os) "custom-logseq" "cli.edn")
132132
(:config-path result)))))
133133

134134
(deftest test-explicit-config-path-does-not-change-root-dir-derived-defaults
@@ -137,7 +137,7 @@
137137
result (config/resolve-config {:config-path cfg-path
138138
:root-dir "~/custom-logseq"})]
139139
(is (= cfg-path (:config-path result)))
140-
(is (= "~/custom-logseq" (:root-dir result)))))
140+
(is (= (node-path/join (.homedir os) "custom-logseq") (:root-dir result)))))
141141

142142
(deftest test-server-list-path-follows-root-dir
143143
(let [root-dir (node-path/join (node-helper/create-tmp-dir "cli-root") "nested-root")
@@ -202,3 +202,11 @@
202202
parsed (reader/read-string contents)]
203203
(is (= "old" (:graph parsed)))
204204
(is (not (contains? parsed :auth-token)))))
205+
206+
(deftest test-update-config-expands-tilde-in-root-dir
207+
(let [dir (node-helper/create-tmp-dir "cli-tilde")
208+
cfg-path (node-path/join dir "cli.edn")
209+
_ (config/update-config! {:root-dir dir} {:ws-url "wss://example.com"})
210+
contents (.toString (fs/readFileSync cfg-path) "utf8")
211+
parsed (reader/read-string contents)]
212+
(is (= "wss://example.com" (:ws-url parsed)))))

0 commit comments

Comments
 (0)