Skip to main content

Module zx

Module zx 

Source
Expand description

A zx-compatible API (modelled on google/zx 8.x) for writing shell scripts in Rust.

The core pieces mirror zx:

  • Shell is the $: a command factory carrying Options (cwd, env, shell, prefix, verbose, quiet, nothrow, timeout, …). The zx! macro interpolates arguments with zx quoting ($'...' for bash).
  • ProcessPromise is a configured command; .await (or run) yields Ok(ProcessOutput) on success and Err(ProcessOutput) on failure unless nothrow is set.
  • ProcessOutput holds stdout, stderr, stdall, the exit code, signal and duration, and formats zx-style error messages.
  • within, configure and cd provide scoped settings, and the goods (sleep, retry, spinner, tempdir, parse_argv, …) cover the script helpers.
use command_stream::zx;
use command_stream::zx::Shell;

let name = "hello world";
let out = zx!("echo {}", name).await?;
assert_eq!(out.stdout, "hello world\n");

let failed = zx!(Shell::new().nothrow(true), "exit 3").await?;
assert_eq!(failed.exit_code, Some(3));

Re-exports§

pub use argv::minimist;
pub use argv::parse_argv;
pub use argv::ArgvOptions;
pub use argv::Booleans;
pub use error::ZxError;
pub use goods::echo;
pub use goods::exp_backoff;
pub use goods::glob;
pub use goods::retry;
pub use goods::retry_with_backoff;
pub use goods::retry_with_delay;
pub use goods::sleep;
pub use goods::spinner;
pub use goods::tempdir;
pub use goods::tempfile;
pub use goods::which;
pub use kill::kill;
pub use log::format_cmd;
pub use log::LogEntry;
pub use log::Logger;
pub use md::transform_markdown;
pub use output::ErrorInfo;
pub use output::ProcessOutput;
pub use process::PipeFrom;
pub use process::ProcessPromise;
pub use process::RunningProcess;
pub use process::ZxResult;
pub use shell::cd;
pub use shell::configure;
pub use shell::current_options;
pub use shell::use_bash;
pub use shell::use_powershell;
pub use shell::use_pwsh;
pub use shell::within;
pub use shell::within_sync;
pub use shell::Options;
pub use shell::PreferLocal;
pub use shell::Shell;
pub use util::parse_duration;
pub use util::quote;
pub use util::quote_powershell;
pub use util::IntoZxArg;
pub use util::ZxArg;

Modules§

argv
Command line parsing compatible with minimist (as re-exported by zx) and zx’s parseArgv wrapper.
dotenv
.env parsing and serialization (the dotenv helpers zx re-exports).
error
zx-compatible failure formatting (Fail helpers): exit code and errno descriptions plus the multi-line messages attached to failed commands.
goods
zx “goods”: small helpers for scripts (sleep, retry, spinner, …).
kill
Process-tree termination (zx kill(pid, signal)).
log
Verbose logging (zx log) and the command highlighter (zx formatCmd).
md
Markdown to script transform (zx transformMarkdown).
output
ProcessOutput: the settled result of a zx command.
process
ProcessPromise (a prepared zx command) and RunningProcess (a spawned one), plus the runner that drives the child processes.
shell
The Shell builder (zx $), its options and the scoped defaults used by within, configure and cd.
util
Small helpers shared by the zx-compatible API: quoting, duration parsing, command assembly and name normalisation.