Fork me on GitHub
#babashka
<
2023-01-26
>
Benjamin10:01:38

;; babashka v1.1.172
(babashka.process/shell {:continue true} "fo")
;; java.io.IOException: error=2, No such file or directory..
I'm confused why it throws an err.

borkdude10:01:33

Don't know, I guess you could debug :)

borkdude10:01:41

It seems process builder throws when the program doesn't exist:

:via
 [{:type java.io.IOException
   :message "Cannot run program \"fo\": error=2, No such file or directory"
   :at [java.lang.ProcessBuilder start "ProcessBuilder.java" 1128]}
  {:type java.io.IOException
   :message "error=2, No such file or directory"
   :at [java.lang.ProcessImpl forkAndExec "ProcessImpl.java" -2]}]

teodorlu16:01:59

How do you guys answer "can I shell out to a program" from babashka scripts? I want to warn users and exit early if they need to install something in order to use my script. In Go, I've used https://pkg.go.dev/os/exec#LookPath. I skimmed the API docs for babashka/process and babashka/fs, but I didn't spot what I was looking for. > LookPath searches for an executable named file in the directories named by the PATH environment variable. If file contains a slash, it is tried directly and the PATH is not consulted. Otherwise, on success, the result is an absolute path.

borkdude16:01:35

@U3X7174KS shell should already do what you want

teodorlu16:01:37

can shell let me know whether there is an fzf binary on the user's path before invoking fzf? (I can confirm that using shell to invoke fzf works perfectly when I have fzf on my system)

borkdude16:01:00

yes, there is the fs/which function for this

💯 2
teodorlu16:01:43

Ah, perfect. fs/which was exactly what I was looking for. Thank you! 🙌

👍 8
teodorlu17:01:33

Very quick and dirty hack to solve "I want to install some library I've used before, but I don't remember the coordinates". 1. Make sure you have babaskha, fzf and bbin installed 2. bbin install io.github.teodorlu/neil-quickadd --latest-sha 3. neil-quickadd rescan (just need to run this once) 4. neil-quickadd 5. Pick a library 6. neil dep add is executed to add the library. Only works for ZSH for now. And there's very little validation. But it's 80 lines total -- all the heavy lifting is done by bbin, babashka/fs, babashka/process and babashka/neil. This works by parsing ~/.zsh_history, which may be a bad idea.

🎉 2
teodorlu17:01:49

feel free to steal / modify / make your own versions if this appeals to you 🙂

borkdude19:01:59

@U3X7174KS https://github.com/ThaddeusJiang/chr is another tool that parses shell history (zsh and bash), maybe it helps to make it more portable :)

💯 2
teodorlu19:01:01

Thanks! I know for a fact that I don't handle multiline shell commands correctly right now.

eval202019:01:37

Would it be valuable to have paths from the https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html in babashka/fs? E.g. (fs/xdg-config-home) ;;=> #object[sun.nio.fs.UnixPath 0x3c79d6e5 "/Users/gert/.config"] (as well as fs/xdg-data-home , fs/xdg-cache-home and fs/xdg-state-home ).

borkdude19:01:33

Hmm maybe. Aren't these just environment variable lookups normally?

eval202019:01:53

with env-vars you can override them - so value of XDG_CONFIG_HOME if present or ~/.config by default.

borkdude19:01:12

right, this makes sense :)

borkdude19:01:16

issue + PR welcome

eval202019:01:07

Thx, will do!

eval202016:01:22

PR coming later this weekend