Fork me on GitHub
#babashka
<
2020-05-26
>
steveb8n09:05:23

Q: is there an example of how to use GZIPOutputStream in bb? I get a ClassCastException when I try to create one

steveb8n09:05:58

nvm: I had the wrong type in the constructor

borkdude10:05:51

@steveb8n Now I'm really curious what you're doing 🙂

steveb8n10:05:32

I’m uploading static assets to s3. I want to gzip them so that they are served to browsers fast

steveb8n10:05:58

I could use shell zip but I’m not sure it will be the same on osx vs linux (in CI)

steveb8n10:05:22

(good morning!)

borkdude10:05:23

cool! that class was included to support clj-http-lite, but good to know it's also being used in other ways

steveb8n10:05:11

since you are here: I’m stuck on the final step. I have gzipped into a byte array but can’t work out how to write bytes out to a file

steveb8n10:05:24

is there an example of this somewhere? all I can find is spit

borkdude10:05:04

maybe using FileOutputStream?

borkdude10:05:49

hmm, that's not there it seems in bb

steveb8n10:05:52

scratch that: it works with io/output-stream

steveb8n10:05:04

(as they say in NL)

borkdude10:05:09

cool! can you maybe provide a small example of how to use this? would be helpful

borkdude10:05:41

I mean, I can do this:

bb '(.write (java.util.zip.GZIPOutputStream. (io/output-stream "/tmp/foo.zip")) (.getBytes "dude"))'
but then what?

steveb8n10:05:02

here’s what’s working for me..

steveb8n10:05:04

(let [out-dir "public/editor-js"
      in-file-name (str out-dir "/" (base-file-name out-dir))
      out-file-name (str in-file-name ".gz")]
  (with-open [out-stream (io/output-stream out-file-name)
              gzip (GZIPOutputStream. out-stream)]
    (io/copy (io/file in-file-name) gzip)
    (.finish gzip))

  [out-file-name (.length (io/file out-file-name))])

borkdude10:05:24

right, very cool

steveb8n10:05:49

all the file name stuff is just to find the output file from shadow-cljs advanced compile

steveb8n10:05:13

all linked up now. this will save me some build minutes 🙂

borkdude10:05:29

$ bb '(with-open [os (java.util.zip.GZIPOutputStream. (io/output-stream "/tmp/foo.zip"))] (.write os (.getBytes "dude")) (.finish os))'
$ bb '(slurp (java.util.zip.GZIPInputStream. (io/input-stream "/tmp/foo.zip")))'
"dude"

steveb8n10:05:57

(as they say in NZ)

steveb8n10:05:36

that might save someone some time if you add it to the readme

steveb8n10:05:57

(no doubt that’s your plan already)

borkdude10:05:58

yeah, made an issue for it.

borkdude10:05:05

Is that similar to "sweet af"?

steveb8n10:05:32

no “as” is not an acronym 🙂

steveb8n10:05:55

but the sentiment is the same

borkdude10:05:15

so I already learned two things today, going well 🙂

steveb8n10:05:13

thanks again. I’m gonna stop work now. time to wind down.

steveb8n10:05:15

have a good day

đź‘‹ 8
plins20:05:07

hello everyone, is there a preferred way of parsing cli arguments? lets say run `$ ./my-bb-script.clj --foo bar --x y` , is there and a symbol like *`command-line-args*`  that would hold a map of args in babashka?

borkdude20:05:20

@plins yes:

$ bb -e '*command-line-args*' -x -y -z
("-x" "-y" "-z")

borkdude20:05:02

You can also use clojure.tools.cli to parse args.

plins20:05:03

thanks 🙂