feat: allow user to recreate config from defaults

This commit is contained in:
Pihkaal
2024-01-20 16:32:40 +01:00
parent 96ec7da7bc
commit 8bb7c4c07d
2 changed files with 46 additions and 12 deletions

View File

@@ -1,4 +1,5 @@
use core::panic; use core::panic;
use std::{fs, path::PathBuf};
use crossterm::style::Color; use crossterm::style::Color;
use ini::configparser::ini::Ini; use ini::configparser::ini::Ini;
@@ -33,9 +34,11 @@ pub struct Config {
pub date_format: String, 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(); let mut ini = Ini::new();
ini.load(path).unwrap(); ini.load(&path.to_str().unwrap()).unwrap();
let config = Config { let config = Config {
be_polite: ini.getbool("general", "polite").unwrap().unwrap(), be_polite: ini.getbool("general", "polite").unwrap().unwrap(),
@@ -48,6 +51,12 @@ pub fn load_from_file(path: &str) -> Config {
return 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 { fn load_color(ini: &Ini) -> ComputableColor {
let color_mode = ini.get("styling", "color_mode").unwrap(); let color_mode = ini.get("styling", "color_mode").unwrap();

View File

@@ -1,6 +1,5 @@
use core::panic; use core::panic;
use std::{ use std::{
fs,
io::{self, Write}, io::{self, Write},
path::PathBuf, path::PathBuf,
thread, thread,
@@ -9,7 +8,7 @@ use std::{
use chrono::Local; use chrono::Local;
use clap::Parser; use clap::Parser;
use config::Config; use config::{write_default_config, Config};
use crossterm::{ use crossterm::{
cursor, cursor,
event::{self, Event, KeyCode, KeyModifiers}, event::{self, Event, KeyCode, KeyModifiers},
@@ -27,32 +26,58 @@ mod symbols;
struct Args { struct Args {
#[arg(short, long, value_name = "FILE")] #[arg(short, long, value_name = "FILE")]
config: Option<String>, config: Option<String>,
}
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<()> { fn main() -> io::Result<()> {
let args = Args::parse(); let args = Args::parse();
// Load config // Load config
let mut default_generated = false;
let config_file = if let Some(custom_config) = args.config { let config_file = if let Some(custom_config) = args.config {
PathBuf::from(custom_config) PathBuf::from(custom_config)
} else { } else {
let config_dir = config_dir().unwrap().join("tlock"); let config_file = config_dir().unwrap().join("tlock").join("config");
let config_file = config_dir.clone().join("config");
if !config_file.exists() { if !config_file.exists() {
// Generate default config write_default_config(config_file.clone());
let _ = fs::create_dir(config_dir); default_generated = true;
let _ = fs::write(config_file.clone(), DEFAULT_CONFIG);
} }
config_file 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() { if !config_file.exists() {
panic!("ERROR: Configuration file not found"); 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(); let mut stdout = io::stdout();