You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is similar in spirit to `fs::read_to_string` -- not strictly
necessary, but very convenient for many use-cases.
Often you need to write essentially a "bash script" in Rust which spawns
other processes. This usually happens in `build.rs`, tests, or other
auxiliary code. Using std's Command API for this is inconvenient,
primarily because checking for exit code (and utf8-validity, if you need
output) turns one-liner into a paragraph of boilerplate.
While there are crates.io crates to help with this, using them often is
not convenient. In fact, I maintain one such crate (`xshell`) and, while
I do use it when I am writing something substantial, for smaller things
I tend to copy-paste this *std-only* snippet:
https://github.com/rust-analyzer/rust-analyzer/blob/ae36af2bd43906ddb1eeff96d76754f012c0a2c7/crates/rust-analyzer/build.rs#L61-L73
So, this PR adds two convenience functions to cover two most common
use-cases:
* `run` is like `status`, except that it check that `status` is zero
* `read_stdout` is like `output`, except that it checks status and
decodes to `String`. It also chomps the last newline, which is modeled
after shell substitution behavior (`echo -n "$(git rev-parse HEAD)"`)
and Julia's [`readchomp`](https://docs.julialang.org/en/v1/manual/running-external-programs/)
Note that this is not the ideal API. In particular, error messages do
not include the command line or stderr. So, for user-facing commands,
you'd have to write you own code or use a process-spawning library like
`duct`.
0 commit comments