use std::io::{self, Write};
use std::path::PathBuf;
use std::process::ExitCode;
use agentty::app::{AGENTTY_WT_DIR, App, AppError, agentty_home};
use agentty::infra::db::{DB_DIR, DB_FILE, Database};
use agentty::infra::git::{GitClient, RealGitClient};
#[tokio::main]
async fn main() -> ExitCode {
match run().await {
Ok(()) => ExitCode::SUCCESS,
Err(error) => {
let _ = writeln!(io::stderr().lock(), "{error}");
ExitCode::FAILURE
}
}
}
async fn run() -> Result<(), AppError> {
let auto_update = !std::env::args().any(|arg| arg == "--no-update");
let home = agentty_home();
let base_path = home.join(AGENTTY_WT_DIR);
let working_dir = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("/"));
let git_client = RealGitClient;
let git_branch = git_client.detect_git_info(working_dir.clone()).await;
let db_path = home.join(DB_DIR).join(DB_FILE);
let db = Database::open(&db_path).await?;
let mut app = App::new(auto_update, base_path, working_dir, git_branch, db).await?;
agentty::runtime::run(&mut app)
.await
.map_err(|error| AppError::Workflow(format!("Failed to run terminal UI: {error}")))
}