use anyhow::Result;
use clap::{Parser, Subcommand};
use crate::client::RommClient;
use crate::config::Config;
pub mod api;
pub mod cache;
pub mod download;
pub mod init;
pub mod library_scan;
pub mod platforms;
pub mod print;
pub mod roms;
pub mod scan;
pub mod update;
#[derive(Clone, Copy, Debug)]
pub enum OutputFormat {
Text,
Json,
}
impl OutputFormat {
pub fn from_flags(global_json: bool, local_json: bool) -> Self {
if global_json || local_json {
OutputFormat::Json
} else {
OutputFormat::Text
}
}
}
#[derive(Parser, Debug)]
#[command(
name = "romm-cli",
version,
about = "Rust CLI and TUI for the ROMM API",
infer_subcommands = true,
arg_required_else_help = true
)]
pub struct Cli {
#[arg(short, long, global = true)]
pub verbose: bool,
#[arg(long, global = true)]
pub json: bool,
#[command(subcommand)]
pub command: Commands,
}
#[derive(Subcommand, Debug)]
pub enum Commands {
#[command(visible_alias = "setup")]
Init(init::InitCommand),
#[cfg(feature = "tui")]
Tui,
#[cfg(not(feature = "tui"))]
Tui,
#[command(visible_alias = "call")]
Api(api::ApiCommand),
#[command(visible_aliases = ["platform", "p", "plats"])]
Platforms(platforms::PlatformsCommand),
#[command(visible_aliases = ["rom", "r"])]
Roms(roms::RomsCommand),
Scan(scan::ScanCommand),
#[command(visible_aliases = ["dl", "get"])]
Download(download::DownloadCommand),
Cache(cache::CacheCommand),
Update,
}
pub async fn run(cli: Cli, config: Config) -> Result<()> {
let client = RommClient::new(&config, cli.verbose)?;
match cli.command {
Commands::Init(_) => {
anyhow::bail!("internal error: init must be handled before load_config");
}
#[cfg(feature = "tui")]
Commands::Tui => {
anyhow::bail!("internal error: TUI must be started via run_interactive from main");
}
#[cfg(not(feature = "tui"))]
Commands::Tui => anyhow::bail!("this feature requires the tui"),
command => crate::frontend::cli::run(command, &client, cli.json).await?,
}
Ok(())
}