Fork me on GitHub
#babashka
<
2023-04-19
>
mmer08:04:03

Hi I am sure this has been asked before - what libraries would you use to get the equivalent behaviour of grep, sed, awk and find?

👀 2
👍 1
teodorlu09:04:10

Interesting question! I hope we get lots of answers. find - babashka.fs/glob grep - clojure.core/filter sed and awk - some combination of glob and filter. You can always babashka.process/shell out to find, grep sed and awk. I’ve ported a few bash scripts to babashka, and I find that doing as little as possible in each step is a good idea.

teodorlu09:04:43

Specific examples of using grep, sed, awk and find would help — then we could see (specifically) how those could be implemented in Babashka.

mmer09:04:23

find working/$RELPATH/Lifecycle/ansible -type f -name '*.yml'| xargs grep -liE 'report PROPERTIES'

👀 1
teodorlu09:04:30

oooh, this looks like a nice thread-last (`->>`)-pipe! I’d start with a babashka.fs/glob for the files, then run filters with babashka.fs predicates for files, and file name checks. Xargs would be a map with a new function from each file to a slurp, clojure.string/split-lines and a filter. Not sure what the grep args are doing, but I’d assume that could be a predicate option. I don’t have the time to write an example right now, so you’re (unfortunately) just getting loose ideas (yet)!

teodorlu09:04:53

(perhaps other babashka-ers reading this thread can take that as a call to action)

pesterhazy12:04:27

Too add to what @U3X7174KS, babashka has all you need, and IMO its offerings are better (i.e. more reliable, easier to maintain)

pesterhazy12:04:44

Teodor's answer should give you a few pointers, a few others in https://presumably.de/how-to-do-things-with-babashka.html

pesterhazy12:04:23

It's a bit of a mindset shift, as you're using a real programming language now, which is a bit more verbose but also more reliable, more modular, etc etc

pesterhazy12:04:27

find is just a map over fs/glob

pesterhazy12:04:03

anything in particular you got stuck with?

mmer13:04:04

I will give it a go.

👍 2
mmer13:04:53

It is these unix tools that I got used to that if I could get into babashka mode might mean I don't end up with a mixture of the two.

mmer13:04:13

The other tool I heavily use if Sed.

pesterhazy13:04:01

clojure.string/replace is a good replacement

dev-hartmann12:04:19

hey fellow babashka enthusiasts. I do have a quick question. I’m trying to use a shutdown hook in one of my babashka scripts but It seems it only reacts to SIGINT not SIGTERM I”m using it like I found it in the readme (-> (Runtime/getRuntime) (.addShutdownHook (Thread. (drain-queue))))

borkdude13:04:14

@U0J8XN37Y can you confirm this is different behavior than clojure's?

dev-hartmann13:04:45

I’ll try. Have to say I”m so used to seeing the the java code in a lot of projects at work and knowing that this works with k8s pod lifecycle management I did not second guess it. Will try and report back

borkdude17:04:40

It seems clj / java does execute the shutdown hook on SIGTERM. Maybe this is an issue with native image, I'll run a test

dev-hartmann17:04:32

Yes, that’s what I found out too. I found a way using signals to trigger an system exit which works, but looks a bit hacky

borkdude17:04:04

yeah, I was going to propose that as a workaround :)

dev-hartmann17:04:31

👌:skin-tone-4:

dev-hartmann17:04:02

I think if there’s no signalHandler for TERM it will will be treated as a INT, but might be wrong

borkdude18:04:26

Seems to happen with small repro as well. I asked here in the native image channel now. https://graalvm.slack.com/archives/CN9KSFB40/p1681927638938879

borkdude19:04:43

@U0J8XN37Y I think this fixes it: https://github.com/babashka/babashka/commit/63102eca863579effd8f4b4c639d3f2bfac8e687 Once this build finishes, you can try the dev-build version:

bash <(curl ) --dev-build --dir /tmp

dev-hartmann20:04:12

Wow! Awesome, thx for the fix!

dev-hartmann20:04:01

Was looking into how to fix it myself, but sadly I’m pretty new to graalvm

borkdude20:04:22

If you're not on Windows you should be able to test it

borkdude20:04:31

since the other builds already have finished

Ferdinand Beyer18:04:56

Using babashka.process, a customer reported this error: java.lang.NoSuchMethodError: java.lang.Process.onExit()Ljava/util/concurrent/CompletableFuture; It seems that the problem is that the if-before-jdk8 check is a macro, and we compile our application ahead-of-time and deploy a uberjar, which means that the Java version check is done at compile time, with a potentially different Java version as encountered during runtime. Is this by design?

borkdude18:04:46

is the customer running with jdk8?

borkdude18:04:29

it is by design that jdk8 is supported, but only if you compile on jdk8

Ferdinand Beyer18:04:27

Yep, this one was running Java 8

Ferdinand Beyer18:04:58

I’m wondering if it would be better to do this check at runtime, maybe cache with a delay or similar. For us, this means we would need to exclude babashka.process from AOT compiling (not sure if that’s possible). Not a big deal, as we now added a check to check for a minimum version, but it was surprising nontheless

borkdude18:04:12

I think I've already gone out of my way to support jdk8. If I would make any changes to this, I'd be tempted to drop support for jdk8 altogether

💯 2
👍 1
borkdude19:04:18

@U07M2C8TT are you a colleague of @U922FGW59 maybe?

jacob.maine20:04:51

Yeah, sorry for the duplicate questions… we were both looking at the same thing