Skip to main content

romm_cli/commands/
roms.rs

1use anyhow::Result;
2use clap::{Args, Subcommand};
3
4use crate::client::RommClient;
5use crate::commands::print::print_roms_table;
6use crate::commands::OutputFormat;
7use crate::endpoints::roms::GetRoms;
8use crate::services::RomService;
9
10/// CLI entrypoint for listing/searching ROMs via `/api/roms`.
11#[derive(Args, Debug)]
12pub struct RomsCommand {
13    #[command(subcommand)]
14    pub action: Option<RomsAction>,
15
16    /// Search term to filter roms
17    #[arg(long, global = true, visible_aliases = ["query", "q"])]
18    pub search_term: Option<String>,
19
20    /// Filter by platform ID
21    #[arg(long, global = true, visible_alias = "id")]
22    pub platform_id: Option<u64>,
23
24    /// Page size limit
25    #[arg(long, global = true)]
26    pub limit: Option<u32>,
27
28    /// Page offset
29    #[arg(long, global = true)]
30    pub offset: Option<u32>,
31
32    /// Output as JSON (overrides global --json when set).
33    #[arg(long, global = true)]
34    pub json: bool,
35}
36
37#[derive(Subcommand, Debug)]
38pub enum RomsAction {
39    /// List available ROMs (default)
40    #[command(visible_alias = "ls")]
41    List,
42    /// Get detailed information for a single ROM
43    #[command(visible_alias = "info")]
44    Get {
45        /// The ID of the ROM
46        id: u64,
47    },
48}
49
50pub async fn handle(cmd: RomsCommand, client: &RommClient, format: OutputFormat) -> Result<()> {
51    let action = cmd.action.unwrap_or(RomsAction::List);
52
53    match action {
54        RomsAction::List => {
55            let ep = GetRoms {
56                search_term: cmd.search_term.clone(),
57                platform_id: cmd.platform_id,
58                collection_id: None,
59                smart_collection_id: None,
60                virtual_collection_id: None,
61                limit: cmd.limit,
62                offset: cmd.offset,
63            };
64
65            let service = RomService::new(client);
66            let results = service.search_roms(&ep).await?;
67
68            match format {
69                OutputFormat::Json => {
70                    println!("{}", serde_json::to_string_pretty(&results)?);
71                }
72                OutputFormat::Text => {
73                    print_roms_table(&results);
74                }
75            }
76        }
77        RomsAction::Get { id } => {
78            let service = RomService::new(client);
79            let rom = service.get_rom(id).await?;
80
81            match format {
82                OutputFormat::Json => {
83                    println!("{}", serde_json::to_string_pretty(&rom)?);
84                }
85                OutputFormat::Text => {
86                    // For now, reuse the table printer for a single item
87                    // or just pretty-print the JSON.
88                    println!("{}", serde_json::to_string_pretty(&rom)?);
89                }
90            }
91        }
92    }
93
94    Ok(())
95}