Fork me on GitHub
#babashka
<
2020-12-05
>
kwrooijen12:12:21

Any way to get the contents of a jar in bb?

borkdude12:12:01

@kevin.van.rooijen yes, look at examples/ls_jar.clj

👍 1
kwrooijen12:12:32

Works as a little sun. Thanks

👍 1
kwrooijen13:12:06

I could use edamame.core/parse-string in bb. Any chance we can include this in the clojure.edn NS? Or is there another way to include this?

kwrooijen13:12:33

Currently tripping over the #() reader with clojure.edn. Which was fixed in edamame

borkdude13:12:50

@kevin.van.rooijen you can use read-string in bb as well

borkdude13:12:59

which is basically edamame

borkdude13:12:32

$ bb -e '(read-string "#()")'
(fn* [] ())

kwrooijen13:12:54

Let me try that 🙂

kwrooijen13:12:49

clojure.lang.ExceptionInfo: Read-eval not allowed. Use the :read-eval` option`

kwrooijen13:12:34

but read-string in bb doesn't accept 3 args?

kwrooijen13:12:46

*read-eval* is also undefined

borkdude13:12:40

currently read-string is a bit under-developed. but read-eval is turned off by default in edamame

borkdude13:12:57

you could post an issue about what you expect to work, but I expect read-string to be safe by default in bb currently

kwrooijen13:12:04

Yeah I don't actually want to eval anything. I just want the form

kwrooijen13:12:09

I guess it makes sense for this to crash (read-string "#=(+ 1 2 3))")

kwrooijen13:12:02

Actually I think I'd expect (eval '(+ 1 2 3))

borkdude13:12:48

edamame currently doesn't even support this, nobody has ever asked for this before. edamame just returns (read-eval (+ 1 2 3)) when you pass :read-eval true but doesn't have a callback for evaluating it, which we of course can include

borkdude13:12:25

so currently bb crashes when you try to do this

borkdude13:12:10

So maybe the sane behavior would be to do it exactly as clojure. PR welcome

borkdude13:12:39

1) The first step would be to fix edamame to support a read-eval function which gets the expression and then can eval it

borkdude13:12:43

2) Fix read-string in bb or sci (not sure where it is currently). I think in the case of sci it could make sense to disallow it by default unless someone passes a setting. In bb we can enable this setting by default.

borkdude13:12:07

Does it hurt anyone if we allow it in sci by default?

borkdude13:12:10

security-wise

borkdude13:12:20

you're evaluating already anyway, so maybe not

kwrooijen13:12:31

For my use case I don't actually want anything evalled though

borkdude13:12:52

Yes, so you can set *read-eval* to false, like in clj.

borkdude13:12:04

3) Include *read-eval* in sci

borkdude13:12:10

yes. so you want to parse code which contains it but not eval it? why?

kwrooijen13:12:22

Currently I just want to get all the functions in a namespace, without having to require it

kwrooijen13:12:32

Not sure if edamame has any magic for that

borkdude13:12:53

you are parsing defn etc manually?

borkdude13:12:22

I think clj-kondo is a much better tool for this.

$ clj-kondo --config '{:output {:analysis true :format :edn}}' --lint - <<< '(defn foo [])' | puget
{:analysis {:namespace-definitions [],
            :namespace-usages [],
            :var-definitions [{:col 1,
                               :filename "<stdin>",
                               :fixed-arities #{0},
                               :name foo,
                               :ns user,
                               :row 1}],
            :var-usages [{:arity 2,
                          :col 2,
                          :filename "<stdin>",
                          :from user,
                          :macro true,
                          :name defn,
                          :row 1,
                          :to clojure.core,
                          :varargs-min-arity 2}]},
 :findings [],
 :summary {:duration 35,
           :error 0,
           :files 1,
           :info 0,
           :type :summary,
           :warning 0}}

kwrooijen13:12:45

Ahh, that might be exactly what I need

borkdude13:12:02

you can use clj-kondo as a babashka pod as well, for scripting

kwrooijen13:12:53

Very cool, I'll try that. Thanks

borkdude14:12:33

@kevin.van.rooijen if you're using this from emacs, you might also want to look at anakondo: https://github.com/didibus/anakondo

kwrooijen14:12:38

I did not know about this

kwrooijen17:12:53

Private functions can't be called in bb? Trying to figure out what this function results in (#'babashka.curl/curl-command {})

borkdude17:12:54

that function just isn't exposed in the bb sci config (if you have watched the internals talk, you will know what I mean)

borkdude17:12:19

but if you run babashka.curl from source, you should be able to do that

kwrooijen17:12:19

Haven't watched that yet 😛

kwrooijen17:12:29

Yeah I'll just clone it

kwrooijen17:12:45

Trying to pass in a cookie but not sure how / if it's possible

borkdude17:12:00

@kevin.van.rooijen if you want to see the command, you can also do (:command (curl/get "foobar" {:debug true}))

borkdude17:12:48

$ bb -e "(:command (curl/get \"\" {:debug true}))"
["curl" "--silent" "--show-error" "--location" "--dump-header" "/var/folders/2m/h3cvrr1x4296p315vbk7m32c0000gp/T/babashka.curl8448783184890828476.headers" "--compressed" ""]

borkdude17:12:55

Cookies are passed using :headers

borkdude17:12:00

they are just that

kwrooijen17:12:28

Ahh ok, I assumed I have to use the --cookie arg in curl

borkdude17:12:33

That might be better, I haven't used it that way yet. But Cookie is just an HTTP header, so something like this should also work:

$ bb -e '(org.httpkit.server/run-server (fn [req] (clojure.pprint/pprint req) {:body "hello"}) {:port 3000}) (curl/post "" {:headers {"Cookie" "name=dude; foo=bar"}}) nil'
{:remote-addr "0:0:0:0:0:0:0:1",
 :headers
 {"accept" "*/*",
  "accept-encoding" "deflate, gzip",
  "cookie" "name=dude; foo=bar",
  "host" "localhost:3000",

👍 1