use anyhow::Result;
use clap::{Args, Subcommand};
use crate::cli_presentation::CliPresentation;
use crate::commands::print::print_platforms_table;
use crate::commands::OutputFormat;
use romm_api::client::RommClient;
use romm_api::endpoints::platforms::{GetPlatform, ListPlatforms};
#[derive(Args, Debug)]
pub struct PlatformsCommand {
#[command(subcommand)]
pub action: Option<PlatformsAction>,
#[arg(long, global = true)]
pub json: bool,
}
#[derive(Subcommand, Debug)]
pub enum PlatformsAction {
#[command(visible_alias = "ls")]
List,
#[command(visible_alias = "info")]
Get {
id: u64,
},
}
pub async fn handle(
cmd: PlatformsCommand,
client: &RommClient,
presentation: CliPresentation,
) -> Result<()> {
let format = presentation.format;
let action = cmd.action.unwrap_or(PlatformsAction::List);
match action {
PlatformsAction::List => {
let platforms = client.call(&ListPlatforms).await?;
match format {
OutputFormat::Json => {
println!("{}", serde_json::to_string_pretty(&platforms)?);
}
OutputFormat::Text => {
print_platforms_table(&platforms);
}
}
}
PlatformsAction::Get { id } => {
let platform = client.call(&GetPlatform { id }).await?;
match format {
OutputFormat::Json => {
println!("{}", serde_json::to_string_pretty(&platform)?);
}
OutputFormat::Text => {
println!("{}", serde_json::to_string_pretty(&platform)?);
}
}
}
}
Ok(())
}