From d09c1d60f9bd6d3d1ab46327c591969e71ab9d6a Mon Sep 17 00:00:00 2001 From: Andreas Mieke Date: Thu, 2 Nov 2023 11:47:23 +0100 Subject: [PATCH] feat(config): Add --first-run flag Add flag to run the first run wizard again, even if config already exists. --- src/config.rs | 16 ++++++++++++---- src/main.rs | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) 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);