2a91e2b083
Pure move: cli definition, per-group handlers (session, bulk, bench, events, galaxy, smoke, batch) and shared output/parse/connect helpers now live in their own modules. No behaviour or CLI surface change; --help of all 33 commands is byte-identical, clippy clean, tests unchanged. Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
86 lines
3.3 KiB
Rust
86 lines
3.3 KiB
Rust
//! `batch` command: stdin-driven multi-command session.
|
|
|
|
use std::io::{self, BufRead, Write};
|
|
|
|
use clap::Parser;
|
|
use zb_mom_ww_mxgateway_client::Error;
|
|
|
|
use crate::cli::*;
|
|
use crate::dispatch;
|
|
|
|
/// End-of-result sentinel written to stdout after every batch command.
|
|
pub(crate) const BATCH_EOR: &str = "__MXGW_BATCH_EOR__";
|
|
|
|
/// Run the batch loop: read one command line at a time from stdin, dispatch
|
|
/// each through the normal [`dispatch`] path, and write [`BATCH_EOR`] to
|
|
/// stdout after every result. Errors are serialised as JSON to stdout so
|
|
/// the harness can parse them without interleaving stderr. The loop never
|
|
/// terminates on command error or accidental blank lines; only stdin EOF
|
|
/// ends the session — empty lines log an empty-EOR-bracketed result and
|
|
/// continue.
|
|
///
|
|
/// `std::io::Stdin::lock().lines()` is a blocking iterator and the dispatch
|
|
/// future is spawned on a separate tokio task so the runtime's main worker
|
|
/// stays free. When the runtime is multi-threaded the blocking read keeps
|
|
/// one worker parked on `ReadFile`; that is acceptable here because no other
|
|
/// future on the main task needs to run while we wait for the next command.
|
|
pub(crate) async fn run_batch() -> Result<(), Error> {
|
|
let stdin = io::stdin();
|
|
let stdout = io::stdout();
|
|
|
|
for line in stdin.lock().lines() {
|
|
let line = line.map_err(|e| Error::InvalidArgument {
|
|
name: "stdin".to_owned(),
|
|
detail: e.to_string(),
|
|
})?;
|
|
|
|
let parts: Vec<&str> = line.split_ascii_whitespace().collect();
|
|
if parts.is_empty() {
|
|
// Empty / whitespace-only line: log an empty-EOR-bracketed
|
|
// result and continue so accidental blank lines from the
|
|
// PowerShell e2e harness do not silently end the session.
|
|
println!("{BATCH_EOR}");
|
|
stdout.lock().flush().ok();
|
|
continue;
|
|
}
|
|
|
|
// Re-parse the split arguments under a fresh Cli, prepending the
|
|
// program-name placeholder so clap sees a complete argv[].
|
|
let parse_result =
|
|
Cli::try_parse_from(std::iter::once("mxgw-cli").chain(parts.iter().copied()));
|
|
|
|
let outcome: Result<(), Error> = match parse_result {
|
|
Ok(cli) => {
|
|
// Spawn on a new tokio task so each command runs with a fresh
|
|
// stack, avoiding stack overflow from the large dispatch future.
|
|
tokio::task::spawn(dispatch(cli.command))
|
|
.await
|
|
.unwrap_or_else(|join_err| {
|
|
Err(Error::InvalidArgument {
|
|
name: "task".to_owned(),
|
|
detail: join_err.to_string(),
|
|
})
|
|
})
|
|
}
|
|
Err(clap_err) => Err(Error::InvalidArgument {
|
|
name: "args".to_owned(),
|
|
detail: clap_err.to_string(),
|
|
}),
|
|
};
|
|
|
|
if let Err(err) = outcome {
|
|
// Write error as JSON to stdout so the harness sees it in the
|
|
// same stream as normal output, framed by the EOR sentinel.
|
|
println!(
|
|
"{}",
|
|
serde_json::json!({ "error": err.to_string(), "type": "error" })
|
|
);
|
|
}
|
|
|
|
println!("{BATCH_EOR}");
|
|
stdout.lock().flush().ok();
|
|
}
|
|
|
|
Ok(())
|
|
}
|