pub mod audio;
pub mod catalog;
pub mod cli;
pub mod commands;
pub mod config;
pub mod engine;
pub mod error;
pub mod gpu;
pub mod i18n;
pub mod media_helper;
pub mod model;
pub mod output;
pub mod paths;
pub mod schema;
pub mod signal;
pub mod surface;
pub mod transcribe;
pub mod verbs;
use std::io::Write;
use std::process::ExitCode;
use clap::{CommandFactory, Parser};
use tracing::level_filters::LevelFilter;
use crate::cli::{Cli, Commands};
use crate::commands::Ctx;
use crate::error::Error;
use crate::i18n::UiLang;
use crate::paths::AppPaths;
struct TempGuard;
impl Drop for TempGuard {
fn drop(&mut self) {
signal::cleanup_temps();
}
}
pub fn run() -> ExitCode {
signal::reset_sigpipe();
signal::install_handlers();
let _temps = TempGuard;
let cli = match Cli::try_parse() {
Ok(c) => c,
Err(e) => {
let informational = matches!(
e.kind(),
clap::error::ErrorKind::DisplayHelp
| clap::error::ErrorKind::DisplayVersion
| clap::error::ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand
);
let rendered = e.render().to_string();
let _ = std::io::stderr().write_all(rendered.as_bytes());
if informational {
return ExitCode::SUCCESS;
}
let summary = rendered
.lines()
.find(|l| !l.trim().is_empty())
.unwrap_or("invalid arguments")
.trim_start_matches("error: ")
.to_string();
let err = Error::Usage(summary);
let _ = output::write_error(
&err,
&commands::new_correlation_id(),
&surface::Surface::default(),
None,
);
return err.to_exit_code();
}
};
let correlation_id = commands::new_correlation_id();
let surface = cli.surface();
let ui = match UiLang::resolve(cli.lang.as_deref()) {
Ok(l) => l,
Err(err) => return fail(&err, &correlation_id, &surface, None),
};
if cli.print_schema {
let paths = match AppPaths::resolve(cli.config_dir.as_deref(), cli.cache_dir.as_deref()) {
Ok(p) => p,
Err(err) => return fail(&err, &correlation_id, &surface, None),
};
let store = match commands::load_store(&paths) {
Ok(s) => s,
Err(err) => return fail(&err, &correlation_id, &surface, None),
};
init_tracing(cli.quiet, cli.verbose, Some(&store));
let mut ctx = Ctx {
paths: &paths,
store,
surface: &surface,
correlation_id: &correlation_id,
ui,
no_input: cli.no_input,
offline: cli.offline,
timeout: cli.timeout,
error_reported: false,
};
return match commands::dispatch(Commands::Schema { name: None }, &mut ctx) {
Ok(()) => ExitCode::SUCCESS,
Err(err) => fail(&err, &correlation_id, &surface, None),
};
}
let Some(command) = cli.command else {
let mut buf = Vec::new();
let _ = Cli::command().write_help(&mut buf);
let _ = std::io::stderr().write_all(&buf);
return ExitCode::from(2);
};
if matches!(command, Commands::Completions { .. }) {
init_tracing(cli.quiet, cli.verbose, None);
return match commands::dispatch(
command,
&mut Ctx {
paths: &AppPaths {
config_dir: std::path::PathBuf::from("."),
cache_dir: std::path::PathBuf::from("."),
state_dir: std::path::PathBuf::from("."),
},
store: config::Store::default(),
surface: &surface,
correlation_id: &correlation_id,
ui,
no_input: cli.no_input,
offline: cli.offline,
timeout: cli.timeout,
error_reported: false,
},
) {
Ok(()) => ExitCode::SUCCESS,
Err(err) => fail(&err, &correlation_id, &surface, None),
};
}
let designated = command.target();
let target = designated
.as_ref()
.map(|(resolved, source)| error::TargetRef { resolved, source });
let paths = match AppPaths::resolve(cli.config_dir.as_deref(), cli.cache_dir.as_deref()) {
Ok(p) => p,
Err(err) => return fail(&err, &correlation_id, &surface, target),
};
if let Err(err) = paths.ensure() {
return fail(&err, &correlation_id, &surface, target);
}
let store = match commands::load_store(&paths) {
Ok(s) => s,
Err(err) => return fail(&err, &correlation_id, &surface, target),
};
init_tracing(cli.quiet, cli.verbose, Some(&store));
surface::publish_content_keys(
store
.effective()
.content_keys
.or(config::Store::seeds().content_keys)
.unwrap_or_default(),
);
let ui = match UiLang::resolve(cli.lang.as_deref().or(store.locale.as_deref())) {
Ok(l) => l,
Err(err) => return fail(&err, &correlation_id, &surface, target),
};
let mut ctx = Ctx {
paths: &paths,
store,
surface: &surface,
correlation_id: &correlation_id,
ui,
no_input: cli.no_input,
offline: cli.offline,
timeout: cli.timeout,
error_reported: false,
};
match commands::dispatch(command, &mut ctx) {
Ok(()) => ExitCode::SUCCESS,
Err(err) if ctx.error_reported => {
if !cli.quiet {
if let Some(key) = err.i18n_key() {
eprintln!("{}", i18n::tr(ui, key));
}
}
err.to_exit_code()
}
Err(err) => fail_localised(&err, &correlation_id, &surface, ui, cli.quiet, target),
}
}
fn fail(
err: &Error,
correlation_id: &str,
surface: &surface::Surface,
target: Option<error::TargetRef<'_>>,
) -> ExitCode {
let _ = output::write_error(err, correlation_id, surface, target);
err.to_exit_code()
}
fn fail_localised(
err: &Error,
correlation_id: &str,
surface: &surface::Surface,
ui: UiLang,
quiet: bool,
target: Option<error::TargetRef<'_>>,
) -> ExitCode {
if !quiet {
if let Some(key) = err.i18n_key() {
eprintln!("{}", i18n::tr(ui, key));
}
}
fail(err, correlation_id, surface, target)
}
fn init_tracing(quiet: bool, verbose: u8, store: Option<&config::Store>) {
let configured = store
.map(config::Store::effective)
.and_then(|e| e.log_level.or(config::Store::seeds().log_level))
.and_then(|name| match name.as_str() {
"off" => Some(LevelFilter::OFF),
"error" => Some(LevelFilter::ERROR),
"warn" => Some(LevelFilter::WARN),
"info" => Some(LevelFilter::INFO),
"debug" => Some(LevelFilter::DEBUG),
"trace" => Some(LevelFilter::TRACE),
_ => None,
});
let level = if quiet {
LevelFilter::OFF
} else {
match verbose {
0 => configured.unwrap_or(LevelFilter::WARN),
1 => LevelFilter::INFO,
2 => LevelFilter::DEBUG,
_ => LevelFilter::TRACE,
}
};
let effective = store.map(config::Store::effective);
let seeds = config::Store::seeds();
let json = effective
.as_ref()
.and_then(|e| e.log_format.clone())
.or(seeds.log_format)
.is_some_and(|f| f == "json");
let timestamps = effective
.as_ref()
.and_then(|e| e.log_timestamps)
.or(seeds.log_timestamps)
.unwrap_or(false);
let _ = match (json, timestamps) {
(true, true) => tracing_subscriber::fmt()
.json()
.with_max_level(level)
.with_writer(std::io::stderr)
.with_ansi(false)
.try_init(),
(true, false) => tracing_subscriber::fmt()
.json()
.with_max_level(level)
.with_writer(std::io::stderr)
.with_ansi(false)
.without_time()
.try_init(),
(false, true) => tracing_subscriber::fmt()
.with_max_level(level)
.with_writer(std::io::stderr)
.with_ansi(false)
.try_init(),
(false, false) => tracing_subscriber::fmt()
.with_max_level(level)
.with_writer(std::io::stderr)
.with_ansi(false)
.without_time()
.try_init(),
};
}