Added Auto Config Generation

This commit is contained in:
2026-02-17 10:48:52 -06:00
parent 647edb94ad
commit e229f5580c

View File

@@ -283,6 +283,11 @@ impl App {
} }
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
if let Err(e) = ensure_config_before_tui() {
eprintln!("{e}");
return Ok(()); // or: return Err(io::Error::new(io::ErrorKind::Other, e));
}
// Terminal setup // Terminal setup
enable_raw_mode()?; enable_raw_mode()?;
let mut stdout = io::stdout(); let mut stdout = io::stdout();
@@ -3656,3 +3661,61 @@ fn render_offline_library_dir(
right, right,
); );
} }
fn ensure_config_before_tui() -> Result<(), String> {
use std::io::{self, Write};
use crate::core::config::{self, Config, ConfigError};
match config::load_config() {
Ok(_) => return Ok(()),
Err(ConfigError::NotFound(_)) => {
// first run, we'll prompt below
}
Err(e) => {
// config exists but is broken; still offer to recreate
eprintln!("Config load error: {e:?}");
eprintln!("We can recreate it now.");
}
}
let path = config::config_path().ok_or_else(|| "Cannot resolve config path (HOME/XDG?)".to_string())?;
println!("No usable config found.");
println!("We will create: {}", path.display());
println!();
let mut url = String::new();
loop {
print!("Jellyfin base URL (example: http://localhost:8096): ");
io::stdout().flush().map_err(|e| e.to_string())?;
url.clear();
io::stdin()
.read_line(&mut url)
.map_err(|e| e.to_string())?;
let u = url.trim().trim_end_matches('/').to_string();
if u.is_empty() {
println!("URL cannot be empty.\n");
continue;
}
if !(u.starts_with("http://") || u.starts_with("https://")) {
println!("Please include http:// or https://\n");
continue;
}
let cfg = Config {
base_url: u,
access_token: None,
user_id: None,
};
config::save_config(&cfg).map_err(|e| format!("save_config failed: {e:?}"))?;
println!("\nWrote config: {}\n", path.display());
break;
}
Ok(())
}