mod audit;
mod deps;
mod detect;
mod quality;
mod run_with;
mod tree;
mod unsafe_code;
mod unused;
use crate::core::error::{ErrorCode, Result, UpkeepError};
use crate::cli::UpkeepCommand;
pub async fn handle(command: UpkeepCommand, json: bool) -> Result<()> {
match command {
UpkeepCommand::Detect => tokio::task::spawn_blocking(move || detect::run(json))
.await
.map_err(|err| {
let reason = if err.is_panic() {
"task panicked"
} else if err.is_cancelled() {
"task was cancelled"
} else {
"task failed"
};
UpkeepError::message(ErrorCode::TaskFailed, format!("detect {reason}: {err}"))
})?,
UpkeepCommand::Audit => audit::run(json).await,
UpkeepCommand::Deps { security } => deps::run(json, security).await,
UpkeepCommand::Quality => quality::run(json).await,
UpkeepCommand::Unused => unused::run(json).await,
UpkeepCommand::UnsafeCode => unsafe_code::run(json).await,
UpkeepCommand::Tree(args) => tree::run(json, args).await,
}
}
#[cfg(test)]
mod tests {
use super::handle;
use crate::cli::UpkeepCommand;
#[tokio::test]
async fn handlers_return_ok() {
let commands = [
UpkeepCommand::Detect,
UpkeepCommand::Quality,
UpkeepCommand::Tree(crate::cli::TreeArgs {
depth: Some(0),
duplicates: false,
invert: None,
features: false,
no_dev: false,
}),
];
for command in commands {
handle(command, false).await.unwrap();
}
}
#[tokio::test]
async fn detect_handler_supports_json_output() {
handle(UpkeepCommand::Detect, true).await.unwrap();
}
}