From 2a2adb2aadb0d01ed45cdd16c953b6acadd58aad Mon Sep 17 00:00:00 2001 From: Florian Anderiasch Date: Wed, 20 Aug 2014 10:44:46 +0200 Subject: [PATCH] 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 --- src/leiningen/repl.clj | 27 ++++++++++++++++++++------- test/leiningen/test/repl.clj | 9 ++++++++- test/sample-connect-string | 1 + test/sample-connect-string-http | 1 + 4 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 test/sample-connect-string create mode 100644 test/sample-connect-string-http diff --git a/src/leiningen/repl.clj b/src/leiningen/repl.clj index f86b4b37..3c8cf5ed 100644 --- a/src/leiningen/repl.clj +++ b/src/leiningen/repl.clj @@ -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 #":") - (remove s/blank? x) - (-> (drop-last (count x) [(repl-host project) (client-repl-port project)]) - (concat x)) - (s/join ":" x) - (ensure-port 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))))) (defn options-for-reply [project & {:keys [attach port]}] (as-> (:repl-options project) opts diff --git a/test/leiningen/test/repl.clj b/test/leiningen/test/repl.clj index 2a5f1ab9..7a0864d7 100644 --- a/test/leiningen/test/repl.clj +++ b/test/leiningen/test/repl.clj @@ -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"]))) diff --git a/test/sample-connect-string b/test/sample-connect-string new file mode 100644 index 00000000..7e39b5e8 --- /dev/null +++ b/test/sample-connect-string @@ -0,0 +1 @@ +myhost:23 diff --git a/test/sample-connect-string-http b/test/sample-connect-string-http new file mode 100644 index 00000000..dd279366 --- /dev/null +++ b/test/sample-connect-string-http @@ -0,0 +1 @@ +http://localhost:23/repl