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 {
37 if global_json || local_json {
38 OutputFormat::Json
39 } else {
40 OutputFormat::Text
41 }
42 }
43}
44
45#[derive(Parser, Debug)]
51#[command(
52 name = "romm-cli",
53 version,
54 about = "Rust CLI and TUI for the ROMM API",
55 infer_subcommands = true,
56 arg_required_else_help = true
57)]
58pub struct Cli {
59 #[arg(short, long, global = true)]
61 pub verbose: bool,
62
63 #[arg(long, global = true)]
65 pub json: bool,
66
67 #[command(subcommand)]
68 pub command: Commands,
69}
70
71#[derive(Subcommand, Debug)]
73pub enum Commands {
74 #[command(visible_alias = "setup")]
76 Init(init::InitCommand),
77 #[cfg(feature = "tui")]
79 Tui,
80 #[cfg(not(feature = "tui"))]
82 Tui,
83 #[command(visible_alias = "call")]
85 Api(api::ApiCommand),
86 #[command(visible_aliases = ["platform", "p", "plats"])]
88 Platforms(platforms::PlatformsCommand),
89 #[command(visible_aliases = ["rom", "r"])]
91 Roms(Box<roms::RomsCommand>),
92 Scan(scan::ScanCommand),
94 #[command(visible_aliases = ["dl", "get"])]
96 Download(download::DownloadCommand),
97 Cache(cache::CacheCommand),
99 Auth(auth::AuthCommand),
101 Update,
103}
104
105pub async fn run(cli: Cli, config: Config) -> Result<()> {
106 let client = RommClient::new(&config, cli.verbose)?;
107
108 match cli.command {
109 Commands::Init(_) => {
110 anyhow::bail!("internal error: init must be handled before load_config");
111 }
112 #[cfg(feature = "tui")]
113 Commands::Tui => {
114 anyhow::bail!("internal error: TUI must be started via run_interactive from main");
115 }
116 #[cfg(not(feature = "tui"))]
117 Commands::Tui => anyhow::bail!("this feature requires the tui"),
118 command => crate::frontend::cli::run(command, &client, cli.json).await?,
119 }
120
121 Ok(())
122}