Fork me on GitHub
#babashka
<
2020-06-06
>
Felipe Marques12:06:09

Hi, guys! I'm starting to play with Babashka and I was trying to do a script similar to kubectl edit deployment <deployment-name>. It spawns a vi for editing, and after the user saves it, it do some stuff with the saved file. I tried doing the following:

(->
                        (ProcessBuilder. [ "vi" temp-file-name])
                        (.redirectOutput ProcessBuilder$Redirect/INHERIT)
                        (.redirectInput ProcessBuilder$Redirect/INHERIT)
                        (.redirectError ProcessBuilder$Redirect/INHERIT)
                        (.start))
following the recommendation of this https://stackoverflow.com/questions/9690702/how-can-i-launch-vi-from-within-java-under-commons-exec. But it opens VI but it does not seem to have the normal capabilities such as visual mode, insert mode, and the possibility of editing the file. Does any one have ever tried something similar?

borkdude12:06:48

That's a pretty interesting use case. When I try this:

(import 'java.lang.ProcessBuilder$Redirect)
(->
 (ProcessBuilder. [ "vi" "README.md"])
 (.redirectOutput ProcessBuilder$Redirect/INHERIT)
 (.redirectInput ProcessBuilder$Redirect/INHERIT)
 (.redirectError ProcessBuilder$Redirect/INHERIT)
 (.start))
I am able to edit a file and leave vi again

borkdude12:06:41

btw you can also use .inheritIO:

(->
 (ProcessBuilder. [ "nano" "README.md"])
 (.inheritIO)
 (.start))

borkdude12:06:14

Nano does seem to have some trouble with arrow keys

borkdude12:06:21

But this might also depend on your terminal, etc

Felipe Marques12:06:59

Cool! Didn't know about the .inheritIO.

Felipe Marques12:06:10

I'm using iterm on a mac. Let me try another terminal.

borkdude12:06:12

Leaving nano also has troubles for me in iterm and http://terminal.app

Felipe Marques12:06:17

just running the process builder works for me. So I guess it is a problem with the code that calls my subcommand. I'm using docopt, so it is probably related with that.

Felipe Marques12:06:21

thanks for the help!

borkdude12:06:29

If you figure this out, please leave a note in an issue, so we can document it somewhere

Felipe Marques13:06:09

so, I found the problem I was having. I'm building a CLI

Felipe Marques13:06:31

and my subcommands are being executed async by wrapping them in an async block, which redirecting the substreams. Once I replaced it by another ProcessBuilder with an .inheritIO it worked as expected. So I must differentiate better which commands a want to run in this async block and which one would run in a "normal" ProcessBuilder.

Felipe Marques13:06:45

Regarding this, what would like me to document on an issue?

borkdude14:06:03

Maybe you can post your script in a gist, so I could try if this also works on my machine - and then I can decide which parts would be interesting to document. E.g. I wrote already a part about Process here: https://book.babashka.org/#_java_lang_processbuilder

Felipe Marques14:06:29

Cool! I'll do that and send it here.

Felipe Marques14:06:32

Thanks for the help!

borkdude12:06:38

cool, this seems to work with nano etc as well. many thanks

Felipe Marques12:06:10

cool! you're welcome! And thanks for the amazing tool. Started using it yesterday and I'm really enjoying it.