Skip to main content

romm_cli/frontend/
cli.rs

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