pub mod cli;
pub mod client;
pub mod browse;
pub mod daemon;
pub mod format;

use clap::CommandFactory;
use clap::Parser;
use clap_complete::generate;
use cli::Cli;

pub fn run() -> Result<(), Box<dyn std::error::Error>> {
    let cli = Cli::parse();
    if let Some(shell) = cli.completions {
        let mut cmd = Cli::command();
        let name = cmd.get_name().to_string();
        generate(shell, &mut cmd, name, &mut std::io::stdout());
        return Ok(());
    }
    let cmd = match cli.command {
        Some(c) => c,
        None => {
            let _ = Cli::command().print_help();
            std::process::exit(1);
        }
    };
    match cmd {
        cli::Command::Browse(args) => browse::run(args),
        cli::Command::Daemon(args) => daemon::run(args),
        cli::Command::Health(args) => client::health(args.port, args.json),
        cli::Command::Format(args) => format::run(args),
    }
}
