Allow 'repl :connect' to read args from file

This basically works like in curl, you specify @filename and then the
contents of the file are literally used as if written on the CLI.

@TODO this will not work:
lein repl :connect @./some/file
because there is no realpath resolution
but this works:
lein repl :connect @/full/path
and this as well:
lein repl :connect @some/file
This commit is contained in:
Florian Anderiasch 2014-08-20 10:44:46 +02:00 committed by Florian Anderiasch
parent 2803647bc4
commit 2a2adb2aad
4 changed files with 30 additions and 8 deletions

View file

@ -52,14 +52,27 @@
s
(main/abort "Port is required. See `lein help repl`")))
(defn string-from-file [arg]
(if-let [filename-tmp (and (seq arg) (= "@" (subs arg 0 1)) (seq (subs arg 1)))]
(let [filename (apply str filename-tmp)
errmsg (str "The file '" filename "' can't be read.")]
(if-let [content (try (slurp filename)
(catch Exception e
(main/abort errmsg)))]
(s/trim content)
(main/abort errmsg)))
false))
(defn connect-string [project opts]
(as-> (str (first opts)) x
(s/split x #":")
(let [opt (str (first opts))]
(if-let [sx (string-from-file opt)]
(connect-string project [sx])
(as-> (s/split opt #":") x
(remove s/blank? x)
(-> (drop-last (count x) [(repl-host project) (client-repl-port project)])
(concat x))
(s/join ":" x)
(ensure-port x)))
(ensure-port x)))))
(defn options-for-reply [project & {:keys [attach port]}]
(as-> (:repl-options project) opts

View file

@ -86,7 +86,14 @@
["http://localhost/ham"] {:root "/tmp"}
["foo1234"] {:root "/tmp"}
[] {:root "/tmp"}
[] lthelper/with-resources-project))
[] lthelper/with-resources-project)
(are [in proj]
(is (re-find
#"The file '.+' can't be read."
(lthelper/abort-msg connect-string proj in)))
["@/tmp/please-do-not-create-this-file-it-will-break-my-test"] {}))
(is (= "myhost:23" (connect-string lthelper/sample-project ["@test/sample-connect-string"])))
(is (= "http://localhost:23/repl" (connect-string lthelper/sample-project ["@test/sample-connect-string-http"])))
(is (= "127.0.0.1:4242" (connect-string lthelper/sample-project [])))
(is (= "127.0.0.1:4343" (connect-string lthelper/sample-project ["4343"])))

View file

@ -0,0 +1 @@
myhost:23

View file

@ -0,0 +1 @@
http://localhost:23/repl