From 8bb7c4c07d30ce5b5c63daaf0fe38db9817b9d5e Mon Sep 17 00:00:00 2001 From: Pihkaal Date: Sat, 20 Jan 2024 16:32:40 +0100 Subject: [PATCH] feat: allow user to recreate config from defaults --- src/config.rs | 13 +++++++++++-- src/main.rs | 45 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/config.rs b/src/config.rs index a8f7abe..4231a30 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,5 @@ use core::panic; +use std::{fs, path::PathBuf}; use crossterm::style::Color; use ini::configparser::ini::Ini; @@ -33,9 +34,11 @@ pub struct Config { pub date_format: String, } -pub fn load_from_file(path: &str) -> Config { +const DEFAULT_CONFIG: &str = include_str!("default_config"); + +pub fn load_from_file(path: PathBuf) -> Config { let mut ini = Ini::new(); - ini.load(path).unwrap(); + ini.load(&path.to_str().unwrap()).unwrap(); let config = Config { be_polite: ini.getbool("general", "polite").unwrap().unwrap(), @@ -48,6 +51,12 @@ pub fn load_from_file(path: &str) -> Config { return config; } +pub fn write_default_config(path: PathBuf) -> () { + let parent = path.parent().unwrap(); + let _ = fs::create_dir_all(parent); + let _ = fs::write(path, DEFAULT_CONFIG); +} + fn load_color(ini: &Ini) -> ComputableColor { let color_mode = ini.get("styling", "color_mode").unwrap(); diff --git a/src/main.rs b/src/main.rs index 8fe0ae0..e8a6678 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ use core::panic; use std::{ - fs, io::{self, Write}, path::PathBuf, thread, @@ -9,7 +8,7 @@ use std::{ use chrono::Local; use clap::Parser; -use config::Config; +use config::{write_default_config, Config}; use crossterm::{ cursor, event::{self, Event, KeyCode, KeyModifiers}, @@ -27,32 +26,58 @@ mod symbols; struct Args { #[arg(short, long, value_name = "FILE")] config: Option, -} -const DEFAULT_CONFIG: &str = include_str!("default_config"); + #[arg(short, long, action)] + regenerate_default: bool, + + #[arg(short, long, action)] + yes: bool, +} fn main() -> io::Result<()> { let args = Args::parse(); // Load config + let mut default_generated = false; let config_file = if let Some(custom_config) = args.config { PathBuf::from(custom_config) } else { - let config_dir = config_dir().unwrap().join("tlock"); - let config_file = config_dir.clone().join("config"); + let config_file = config_dir().unwrap().join("tlock").join("config"); if !config_file.exists() { - // Generate default config - let _ = fs::create_dir(config_dir); - let _ = fs::write(config_file.clone(), DEFAULT_CONFIG); + write_default_config(config_file.clone()); + default_generated = true; } config_file }; + + if args.regenerate_default { + if !default_generated && config_file.exists() && !args.yes { + println!("A config file is already located at {:?}", config_file); + print!("Do you really want to recreate it ? [y/N] "); + + let _ = io::stdout().flush(); + + let mut input = String::new(); + let _ = io::stdin().read_line(&mut input); + + let response = input.trim().to_lowercase(); + if response != "y" { + println!("Cancelled."); + return Ok(()); + } + } + + write_default_config(config_file.clone()); + println!("Done."); + return Ok(()); + } + if !config_file.exists() { panic!("ERROR: Configuration file not found"); } - let mut config = config::load_from_file(&config_file.to_str().unwrap()); + let mut config = config::load_from_file(config_file); let mut stdout = io::stdout();