Skip to main content

romm_cli/commands/
platforms.rs

1use anyhow::Result;
2use clap::{Args, Subcommand};
3
4use crate::cli_presentation::CliPresentation;
5use crate::commands::print::print_platforms_table;
6use crate::commands::OutputFormat;
7use romm_api::client::RommClient;
8use romm_api::endpoints::platforms::{GetPlatform, ListPlatforms};
9
10/// CLI entrypoint for platform-related operations.
11#[derive(Args, Debug)]
12pub struct PlatformsCommand {
13    #[command(subcommand)]
14    pub action: Option<PlatformsAction>,
15
16    /// Output as JSON (overrides global --json when set).
17    #[arg(long, global = true)]
18    pub json: bool,
19}
20
21/// Specific action to perform for `romm-cli platforms`.
22#[derive(Subcommand, Debug)]
23pub enum PlatformsAction {
24    /// List all platforms (default)
25    #[command(visible_alias = "ls")]
26    List,
27    /// Get details for a specific platform
28    #[command(visible_alias = "info")]
29    Get {
30        /// The ID of the platform
31        id: u64,
32    },
33}
34
35pub async fn handle(
36    cmd: PlatformsCommand,
37    client: &RommClient,
38    presentation: CliPresentation,
39) -> Result<()> {
40    let format = presentation.format;
41    let action = cmd.action.unwrap_or(PlatformsAction::List);
42
43    match action {
44        PlatformsAction::List => {
45            let platforms = client.call(&ListPlatforms).await?;
46
47            match format {
48                OutputFormat::Json => {
49                    println!("{}", serde_json::to_string_pretty(&platforms)?);
50                }
51                OutputFormat::Text => {
52                    print_platforms_table(&platforms);
53                }
54            }
55        }
56        PlatformsAction::Get { id } => {
57            let platform = client.call(&GetPlatform { id }).await?;
58
59            match format {
60                OutputFormat::Json => {
61                    println!("{}", serde_json::to_string_pretty(&platform)?);
62                }
63                OutputFormat::Text => {
64                    println!("{}", serde_json::to_string_pretty(&platform)?);
65                }
66            }
67        }
68    }
69
70    Ok(())
71}