subspace_board_command/
lib.rs1mod approval;
4mod cli;
5mod collision;
6mod composition;
7mod input;
8mod root;
9#[cfg(test)]
10mod tests;
11mod tree;
12
13use std::{process::ExitCode, sync::Arc};
14use subspace_agent_tools::{Invocation, Unmonitored};
15
16pub use cli::run;
17pub use composition::Composition;
18pub use root::{complete_word, tree as root_tree};
19pub use usage;
22
23fn execute(route: &tree::Route, output: &usage::parse::ParseOutput) -> ExitCode {
24 let arguments = match input::arguments(route, output) {
25 Ok(arguments) => arguments,
26 Err(error) => return fail(&error, 2),
27 };
28 let yes = input::switch(output, "yes");
29 if route.tool.spec().access == subspace_agent_tools::Access::Write
30 && !yes
31 && !approval::interactive()
32 {
33 return fail(
34 "Confirmation requires a TTY or explicit --yes; no operation was executed.",
35 1,
36 );
37 }
38 let invocation = Invocation {
39 arguments,
40 approval: Arc::new(approval::CliApproval { yes }),
41 monitor: Arc::new(Unmonitored),
42 };
43 finish(call(route, invocation), input::switch(output, "json"))
44}
45
46fn call(route: &tree::Route, invocation: Invocation) -> Result<serde_json::Value, String> {
47 let runtime = tokio::runtime::Builder::new_multi_thread()
48 .enable_all()
49 .build()
50 .map_err(|error| error.to_string())?;
51 runtime.block_on(route.tool.call(invocation))
52}
53
54fn finish(result: Result<serde_json::Value, String>, json: bool) -> ExitCode {
55 match result {
56 Ok(value) => {
57 if json {
58 println!("{value}");
59 } else {
60 println!("{value:#}");
61 }
62 if failed(&value) {
63 ExitCode::FAILURE
64 } else {
65 ExitCode::SUCCESS
66 }
67 }
68 Err(error) => fail(&error, 1),
69 }
70}
71
72fn failed(value: &serde_json::Value) -> bool {
73 value["approved"] == false
74 || value["results"]
75 .as_array()
76 .is_some_and(|results| results.iter().any(|result| result["success"] == false))
77}
78
79fn fail(error: &str, code: u8) -> ExitCode {
80 eprintln!("{error}");
81 ExitCode::from(code)
82}