Added Auto Config Generation
This commit is contained in:
63
src/main.rs
63
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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user