feat(config): Add --first-run flag

Add flag to run the first run wizard again, even if config already
exists.
This commit is contained in:
Andreas Mieke 2023-11-02 11:47:23 +01:00
parent b7a47519b5
commit d09c1d60f9
2 changed files with 13 additions and 5 deletions

View file

@ -1,6 +1,6 @@
use std::{fs, error::Error, path::PathBuf, io::ErrorKind, str::FromStr}; use std::{fs, error::Error, path::PathBuf, io::ErrorKind, str::FromStr};
use inquire::{Text, CustomUserError, Autocomplete, autocompletion::Replacement}; use inquire::{Text, CustomUserError, Autocomplete, autocompletion::Replacement};
use log::warn; use log::{warn, info, error};
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
#[derive(Clone, Serialize, Deserialize, Debug)] #[derive(Clone, Serialize, Deserialize, Debug)]
@ -9,18 +9,26 @@ pub struct Config {
pub plex_library: PathBuf, pub plex_library: PathBuf,
} }
pub fn load(path: &PathBuf) -> Result<Config, Box<dyn Error>> { pub fn load(path: &PathBuf, first: bool) -> Result<Config, Box<dyn Error>> {
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 = fs::read_to_string(path);
let f = match f { let f = match f {
Ok(file) => file, Ok(file) => file,
Err(e) => { Err(e) => {
if e.kind() == ErrorKind::NotFound { if e.kind() == ErrorKind::NotFound {
warn!("Config not found, assuming first run!"); warn!("Config not found, running first run wizard...");
let cfg = first_run()?; let cfg = first_run()?;
save(cfg.clone(), path)?; save(cfg.clone(), path)?;
return Ok(cfg); return Ok(cfg);
} else { } else {
panic!("There was an error reading the config file!"); error!("There was an error reading the config file!");
return Err(Box::new(e));
} }
} }
}; };

View file

@ -51,7 +51,7 @@ fn main() {
info!("Loading config from \"{}\"", config_path.to_str().unwrap()); 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); info!("Found config: {:#?}", cfg);