Skip to content

Instantly share code, notes, and snippets.

@Techcable
Created August 4, 2022 02:33
Show Gist options
  • Save Techcable/b2c68eb7a2699dcc6f18d1f3f89676f8 to your computer and use it in GitHub Desktop.
Save Techcable/b2c68eb7a2699dcc6f18d1f3f89676f8 to your computer and use it in GitHub Desktop.
Janet subprocess execute: `exec-slurp` by the great @bakpakin
# See also andrewchambers/janet-sh for a more complete (and awesome) solution:
# https://acha.ninja/blog/dsl_for_shell_scripting/
#
# Source: https://github.com/janet-lang/jpm/blob/07b0c18daf75cfdf098eff5526a17ad0436e3dc0/jpm/shutil.janet#L123-L137
(defn exec-slurp
"Read stdout of subprocess and return it trimmed in a string."
[& args]
(when (dyn :verbose)
(flush)
(print ;(interpose " " args)))
(def env (patch-env))
(put env :out :pipe)
(def proc (os/spawn args :epx env))
(def out (get proc :out))
(def buf @"")
(ev/gather
(:read out :all buf)
(:wait proc))
(string/trimr buf))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment