diff --git a/src/config.rs b/src/config.rs index b14732f..5348a19 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,6 @@ use std::{fs, error::Error, path::PathBuf, io::ErrorKind, str::FromStr}; use inquire::{Text, CustomUserError, Autocomplete, autocompletion::Replacement}; -use log::warn; +use log::{warn, info, error}; use serde::{Serialize, Deserialize}; #[derive(Clone, Serialize, Deserialize, Debug)] @@ -9,18 +9,26 @@ pub struct Config { pub plex_library: PathBuf, } -pub fn load(path: &PathBuf) -> Result> { +pub fn load(path: &PathBuf, first: bool) -> Result> { + if first { + info!("Running first run wizard..."); + let cfg = first_run()?; + save(cfg.clone(), path)?; + return Ok(cfg); + } + let f = fs::read_to_string(path); let f = match f { Ok(file) => file, Err(e) => { if e.kind() == ErrorKind::NotFound { - warn!("Config not found, assuming first run!"); + warn!("Config not found, running first run wizard..."); let cfg = first_run()?; save(cfg.clone(), path)?; return Ok(cfg); } else { - panic!("There was an error reading the config file!"); + error!("There was an error reading the config file!"); + return Err(Box::new(e)); } } }; diff --git a/src/main.rs b/src/main.rs index f642e73..4e6b3d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,7 +51,7 @@ fn main() { info!("Loading config from \"{}\"", config_path.to_str().unwrap()); - let cfg = config::load(&config_path).unwrap(); + let cfg = config::load(&config_path, args.first_run).unwrap(); info!("Found config: {:#?}", cfg);