This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-03-02
Channels
- # aleph (2)
- # announcements (3)
- # babashka (12)
- # beginners (55)
- # calva (11)
- # clj-http (12)
- # cljs-dev (41)
- # cljtogether (2)
- # clojure (51)
- # clojure-denmark (2)
- # clojure-europe (32)
- # clojure-nl (17)
- # clojure-norway (2)
- # clojure-switzerland (1)
- # clojure-uk (3)
- # clojurescript (34)
- # cursive (20)
- # data-science (3)
- # datahike (23)
- # datomic (3)
- # events (1)
- # fulcro (1)
- # honeysql (4)
- # inf-clojure (2)
- # interop (38)
- # java (3)
- # kaocha (8)
- # lsp (51)
- # luminus (2)
- # malli (2)
- # nextjournal (5)
- # off-topic (21)
- # pedestal (2)
- # polylith (12)
- # re-frame (4)
- # reagent (8)
- # reitit (4)
- # releases (1)
- # ring (4)
- # shadow-cljs (179)
- # spacemacs (2)
- # specter (1)
- # xtdb (13)
does anyone have a handy bash snippet or function so that i can call clj -J$(socket-repl 50505)
and it would expand to the correct incantation for a socket on that port? Aliases can’t quite do it, and i’m unsure how to “return” a string such that it works exactly right
of course i have a few aliases but those are statically tied to a few ports. So for when using a jar or wanting a one-off port it would be quite helpful
socket-repl() {
echo -n "-Dclojure.server.repl=\"{:port $1 :accept clojure.core.server/repl}\""
}
like that?
ah, wait, I think I see what you mean
yeah. it’s easy to get the string correct but I just don’t know how to have it return a string to the original shell. keeps seemingly evaling it
Anything that you put in stdout in a function will be that function's return value.
I'm not sure what you mean by "keeps seemingly evaling it". Cora's answer LGTM - the string will be put on the stdout, where $(...)
will pick it up and substitute.
To demonstrate, I replaced clj
with echo
just to print its args:
[email protected]:~$ socket-repl() {
echo -n "-Dclojure.server.repl=\"{:port $1 :accept clojure.core.server/repl}\""
}
[email protected]:~$ echo -J$(socket-repl 50505)
-J-Dclojure.server.repl="{:port 50505 :accept clojure.core.server/repl}"
[email protected]:~$
Seems correct.❯ socket-repl() {
echo -n "-Dclojure.server.repl=\"{:port $1 :accept clojure.core.server/repl}\""
}
/tmp/socket
❯ clj -J$(socket-repl 6000)
WARNING: Implicit use of clojure.main with options is deprecated, use -M
Exception in thread "main" java.lang.RuntimeException: EOF while reading string
at clojure.lang.Util.runtimeException(Util.java:221)
so i agree with you that it is trivial, but for some reason it isn’t working. Not sure if there’s some escaping somewhere that needs to be done
That's exactly why I hate shell languages and try to avoid them whenever possible. Bloody whitespace keeps messing things up.
I think there should be double quotes around $(...)
.
❯ clj -J”$(socket-repl 6000)” Exception in thread “main” java.lang.ClassCastException: class java.lang.Character cannot be cast to class java.util.Map$Entry (java.lang.Character and java.util.Map$Entry are in module java.base of loader ‘bootstrap’)
Maybe those \"
in the function aren't needed? No clue what clojure.server.repl
needs.
Right, this works:
[email protected]:~$ socket-repl() {
> echo -n "-Dclojure.server.repl={:port $1 :accept clojure.core.server/repl}"
> }
[email protected]:~$ clj -J"$(socket-repl 6000)"
Clojure 1.10.3
user=>
quoting -- it's never very fun
and works with java "$(socket-repl 6000)" -jar ~/projects/work/jars/0.42.0-rc1.jar
amazing!
@U11BV7MTK late to the party, but see if this approach feels better. Using a sort of higher-order function like this sidesteps the problem of shell expansion-and-eval at the command line.
with_socket_repl() {
local port=${1}
local cmd=${2}
local args=${@:3}
${cmd} \
-Dclojure.server.repl={:port ${port} :accept clojure.core.server/repl} \
${args}
}
Test
with_socket_repl 1337 echo -jar ~/projects/work/jars/0.42.0-rc1.jar
If you've not used bash functions like standalone shell tools, https://www.evalapply.org/posts/shell-aint-a-bad-place-to-fp-part-1-doug-mcilroys-pipeline/#compose-again-semantics-functions-play-grand-new-pipeline... source
'em and tab-tab-complete your way to cmd-line glory 🤓