romm_cli/commands/
platforms.rs1use 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#[derive(Args, Debug)]
12pub struct PlatformsCommand {
13 #[command(subcommand)]
14 pub action: Option<PlatformsAction>,
15
16 #[arg(long, global = true)]
18 pub json: bool,
19}
20
21#[derive(Subcommand, Debug)]
23pub enum PlatformsAction {
24 #[command(visible_alias = "ls")]
26 List,
27 #[command(visible_alias = "info")]
29 Get {
30 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}