1use anyhow::Result;
8use clap::{Parser, Subcommand};
9
10use crate::client::RommClient;
11use crate::config::Config;
12
13pub mod api;
14pub mod auth;
15pub mod cache;
16pub mod download;
17pub mod init;
18pub mod library_scan;
19pub mod platforms;
20pub mod print;
21pub mod roms;
22pub mod scan;
23pub mod update;
24
25#[derive(Clone, Copy, Debug)]
27pub enum OutputFormat {
28 Text,
30 Json,
32}
33
34impl OutputFormat {
35 pub fn from_flags(global_json: bool, local_json: bool) -> Self {
40 if global_json || local_json {
41 OutputFormat::Json
42 } else {
43 OutputFormat::Text
44 }
45 }
46}
47
48#[derive(Parser, Debug)]
53#[command(
54 name = "romm-cli",
55 version,
56 about = "Rust CLI and TUI for the ROMM API",
57 infer_subcommands = true,
58 arg_required_else_help = true
59)]
60pub struct Cli {
61 #[arg(short, long, global = true)]
63 pub verbose: bool,
64
65 #[arg(long, global = true)]
67 pub json: bool,
68
69 #[command(subcommand)]
71 pub command: Commands,
72}
73
74#[derive(Subcommand, Debug)]
76pub enum Commands {
77 #[command(visible_alias = "setup")]
79 Init(init::InitCommand),
80 #[cfg(feature = "tui")]
82 Tui,
83 #[cfg(not(feature = "tui"))]
85 Tui,
86 #[command(visible_alias = "call")]
88 Api(api::ApiCommand),
89 #[command(visible_aliases = ["platform", "p", "plats"])]
91 Platforms(platforms::PlatformsCommand),
92 #[command(visible_aliases = ["rom", "r"])]
94 Roms(Box<roms::RomsCommand>),
95 Scan(scan::ScanCommand),
97 #[command(visible_aliases = ["dl", "get"])]
99 Download(download::DownloadCommand),
100 Cache(cache::CacheCommand),
102 Auth(auth::AuthCommand),
104 Update,
106}
107
108pub async fn run(cli: Cli, config: Config) -> Result<()> {
113 let client = RommClient::new(&config, cli.verbose)?;
114
115 match cli.command {
116 Commands::Init(_) => {
117 anyhow::bail!("internal error: init must be handled before load_config");
118 }
119 #[cfg(feature = "tui")]
120 Commands::Tui => {
121 anyhow::bail!("internal error: TUI must be started via run_interactive from main");
122 }
123 #[cfg(not(feature = "tui"))]
124 Commands::Tui => anyhow::bail!("this feature requires the tui"),
125 command => crate::frontend::cli::run(command, &client, cli.json).await?,
126 }
127
128 Ok(())
129}