romm_cli/commands/
roms.rs1use 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#[derive(Args, Debug)]
12pub struct RomsCommand {
13 #[command(subcommand)]
14 pub action: Option<RomsAction>,
15
16 #[arg(long, global = true, visible_aliases = ["query", "q"])]
18 pub search_term: Option<String>,
19
20 #[arg(long, global = true, visible_alias = "id")]
22 pub platform_id: Option<u64>,
23
24 #[arg(long, global = true)]
26 pub limit: Option<u32>,
27
28 #[arg(long, global = true)]
30 pub offset: Option<u32>,
31
32 #[arg(long, global = true)]
34 pub json: bool,
35}
36
37#[derive(Subcommand, Debug)]
38pub enum RomsAction {
39 #[command(visible_alias = "ls")]
41 List,
42 #[command(visible_alias = "info")]
44 Get {
45 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 println!("{}", serde_json::to_string_pretty(&rom)?);
89 }
90 }
91 }
92 }
93
94 Ok(())
95}