From e229f5580c4d6a09faa8b7e7b68018ebaf2dcb79 Mon Sep 17 00:00:00 2001 From: cwj3rcb Date: Tue, 17 Feb 2026 10:48:52 -0600 Subject: [PATCH] Added Auto Config Generation --- src/main.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/main.rs b/src/main.rs index e2feb48..4b4106c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -283,6 +283,11 @@ impl App { } 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 enable_raw_mode()?; let mut stdout = io::stdout(); @@ -3656,3 +3661,61 @@ fn render_offline_library_dir( 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(()) +}