use crate::{Composition, execute, fail, root, tree};
use std::{collections::HashMap, process::ExitCode};
use subspace_agent_tools::ToolEnvironment;
use subspace_product::ProductContext;
use usage::parse::{ParseOutput, ParseValue, TokenRole};
pub fn run(
context: &ProductContext,
boards: &[subspace_mcp::Board],
environment: &ToolEnvironment,
args: &[String],
) -> ExitCode {
match capabilities(context, boards, environment) {
Ok(capabilities) => run_catalog(context, &capabilities, args),
Err(error) => fail(&error, 1),
}
}
pub(crate) fn capabilities(
context: &ProductContext,
boards: &[subspace_mcp::Board],
environment: &ToolEnvironment,
) -> Result<tree::Capabilities, String> {
subspace_mcp::served_catalog(boards, &[], environment)
.map(|grouped| tree::Capabilities::new(context, &grouped))
}
const STANDARD: Composition = Composition::new(root::tree, root::complete_word, unknown_command);
type Fallback = fn(&ProductContext, &[String]) -> ExitCode;
#[cfg(test)]
pub(crate) fn commands(
context: &ProductContext,
capabilities: &tree::Capabilities,
) -> Result<tree::Commands, String> {
STANDARD.commands(context, capabilities)
}
pub(crate) fn run_catalog(
context: &ProductContext,
capabilities: &tree::Capabilities,
args: &[String],
) -> ExitCode {
STANDARD.run_catalog(context, capabilities, args)
}
pub(crate) fn run_commands(
context: &ProductContext,
generated: &tree::Commands,
args: &[String],
complete: fn(&usage::Spec, &[String]) -> Vec<String>,
fallback: Fallback,
) -> ExitCode {
if let Some(words) = args.strip_prefix(&[root::COMPLETE_WORD.into(), "--".into()]) {
for word in complete(&generated.spec, words) {
println!("{word}");
}
return ExitCode::SUCCESS;
}
if args.len() == 1
&& generated
.routes
.iter()
.any(|route| route.path.len() == 2 && route.path[0] == args[0])
{
return parse_and_run(
context,
generated,
&[args[0].clone(), "--help".into()],
fallback,
);
}
parse_and_run(context, generated, args, fallback)
}
fn parse_and_run(
context: &ProductContext,
commands: &tree::Commands,
args: &[String],
fallback: Fallback,
) -> ExitCode {
let argv: Vec<String> = std::iter::once(commands.spec.bin.clone())
.chain(args.iter().cloned())
.collect();
let output = match usage::parse::Parser::new(&commands.spec).explain(&argv) {
Ok(output) => output,
Err(error) => return refused(context, commands, &argv, &error, fallback),
};
if let Some(page) = output.errors.iter().find(|error| {
matches!(
error,
usage::error::UsageErr::Help(_) | usage::error::UsageErr::Version(_)
)
}) {
print!("{page}");
return ExitCode::SUCCESS;
}
if !output.errors.is_empty() {
for error in &output.errors {
eprintln!("{error}");
}
return ExitCode::from(2);
}
let path: Vec<&str> = output
.cmds
.iter()
.skip(1)
.map(|cmd| cmd.name.as_str())
.collect();
if path == [root::COMPLETION] && root::declares_framework_completion(&commands.spec) {
return completion(context, &output);
}
match commands.routes.iter().find(|route| {
route
.path
.iter()
.map(String::as_str)
.eq(path.iter().copied())
}) {
Some(route) => execute(route, &output),
None => fallback(context, args),
}
}
fn refused(
context: &ProductContext,
commands: &tree::Commands,
argv: &[String],
error: &usage::miette::Error,
fallback: Fallback,
) -> ExitCode {
let stop = unexpected_word(error).and_then(|word| root_stop(&commands.spec, argv, &word));
match stop.filter(|&stop| valid_root_prefix(&commands.spec, &argv[..stop])) {
Some(_) => fallback(context, &argv[1..]),
None => fail(&error.to_string(), 2),
}
}
fn unexpected_word(error: &usage::miette::Error) -> Option<String> {
error
.to_string()
.strip_prefix("unexpected word: ")
.map(str::to_owned)
}
fn root_stop(spec: &usage::Spec, argv: &[String], word: &str) -> Option<usize> {
if spec.default_subcommand.is_some() || word.starts_with('-') {
return None;
}
let tokens = usage::parse::Parser::new(spec)
.with_mount_outputs(HashMap::new())
.without_running_commands()
.explain_refused(argv)
.err()?;
let stop = tokens.iter().rev().find(|token| {
token
.roles
.iter()
.any(|role| !matches!(role, TokenRole::Unread))
})?;
let refused = stop
.roles
.iter()
.any(|role| matches!(role, TokenRole::Refused { .. }));
(refused && stop.word == word).then_some(stop.index)
}
fn valid_root_prefix(spec: &usage::Spec, prefix: &[String]) -> bool {
usage::parse::Parser::new(spec)
.explain(prefix)
.is_ok_and(|output| {
output.errors.is_empty()
&& output.cmds.len() == 1
&& output.args.is_empty()
&& !output.double_dash_seen
})
}
pub(crate) fn unknown_command(_: &ProductContext, args: &[String]) -> ExitCode {
fail(&format!("Unknown command: {}", args.join(" ")), 2)
}
fn completion(context: &ProductContext, output: &ParseOutput) -> ExitCode {
let shell = output.args.iter().find_map(|(arg, value)| match value {
ParseValue::String(shell) if arg.name == "shell" => Some(shell.as_str()),
_ => None,
});
match shell.and_then(|shell| root::init_script(context.product.binary_name.as_str(), shell)) {
Some(script) => {
print!("{script}");
ExitCode::SUCCESS
}
None => fail("Choose a completion shell: bash, zsh or fish.", 2),
}
}