1use crate::error::RommError;
2
3use crate::client::RommClient;
4use crate::commands::{
5 api, auth, cache, download, platforms, roms, scan, sync, Commands, OutputFormat,
6};
7use crate::core::interrupt::InterruptContext;
8
9fn map_anyhow<T>(result: Result<T, anyhow::Error>) -> Result<T, RommError> {
10 result.map_err(|e| RommError::Other(e.to_string()))
11}
12
13pub async fn run(
15 command: Commands,
16 client: &RommClient,
17 global_json: bool,
18) -> Result<(), RommError> {
19 match command {
20 Commands::Api(cmd) => {
21 let format = OutputFormat::from_flags(global_json, false);
22 map_anyhow(api::handle(cmd, client, format).await)
23 }
24 Commands::Platforms(cmd) => {
25 let format = OutputFormat::from_flags(global_json, cmd.json);
26 map_anyhow(platforms::handle(cmd, client, format).await)
27 }
28 Commands::Roms(cmd) => {
29 let format = OutputFormat::from_flags(global_json, cmd.json);
30 map_anyhow(roms::handle(*cmd, client, format).await)
31 }
32 Commands::Scan(cmd) => {
33 let format = OutputFormat::from_flags(global_json, false);
34 let interrupt = InterruptContext::new();
35 map_anyhow(scan::handle(cmd, client, format, Some(interrupt)).await)
36 }
37 Commands::Sync(cmd) => {
38 let format = OutputFormat::from_flags(global_json, cmd.json);
39 map_anyhow(sync::handle(cmd, client, format).await)
40 }
41 Commands::Download(cmd) => {
42 let interrupt = InterruptContext::new();
43 download::handle(cmd, client, Some(interrupt)).await
44 }
45 Commands::Cache(cmd) => map_anyhow(cache::handle(cmd)),
46 Commands::Auth(cmd) => {
47 let format = OutputFormat::from_flags(global_json, false);
48 map_anyhow(auth::handle(cmd, client, format).await)
49 }
50 Commands::Init(_) => Err(RommError::Other(
51 "internal routing error: init command in CLI frontend".into(),
52 )),
53 Commands::Tui { .. } => Err(RommError::Other(
54 "internal routing error: TUI command in CLI frontend".into(),
55 )),
56 Commands::Update => {
57 let interrupt = InterruptContext::new();
58 map_anyhow(crate::commands::update::handle(Some(interrupt)).await)
59 }
60 Commands::Completions(_) => Err(RommError::Other(
61 "internal routing error: completions command in CLI frontend".into(),
62 )),
63 }
64}