added safety for overwriting configs

This commit is contained in:
JOLIMAITRE Matthieu 2022-10-05 01:52:39 +02:00
parent de3974725a
commit 5a7e5fae94
2 changed files with 10 additions and 2 deletions

View file

@ -1,12 +1,13 @@
use std::{ use std::{
env, fs, env, fs,
io::stdin,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use ron::ser::PrettyConfig; use ron::ser::PrettyConfig;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::utils::{log_success, Apply}; use crate::utils::{log_process, log_success, Apply};
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Config { pub struct Config {
@ -33,6 +34,11 @@ impl Config {
path.extend([Self::CONFIG_FILE_NAME]); path.extend([Self::CONFIG_FILE_NAME]);
let content = let content =
ron::ser::to_string_pretty(self, PrettyConfig::default().struct_names(true)).unwrap(); ron::ser::to_string_pretty(self, PrettyConfig::default().struct_names(true)).unwrap();
if path.exists() {
log_process("config already exists, overwrite it?");
let mut buff = String::new();
stdin().read_line(&mut buff).unwrap();
}
fs::write(path, content).unwrap(); fs::write(path, content).unwrap();
} }

View file

@ -89,6 +89,7 @@ fn compilation_args() -> Vec<String> {
"-Wall".to_string(), "-Wall".to_string(),
"-Wextra".to_string(), "-Wextra".to_string(),
"-std=c99".to_string(), "-std=c99".to_string(),
"-pedantic".to_string(),
]; ];
if Config::get_local_or_default().strict_mode() { if Config::get_local_or_default().strict_mode() {
args.push("-Werror".to_string()); args.push("-Werror".to_string());
@ -138,7 +139,8 @@ fn main() {
} => { } => {
let path = let path =
path.unwrap_or_else(|| env::current_dir().unwrap().to_str().unwrap().to_string()); path.unwrap_or_else(|| env::current_dir().unwrap().to_str().unwrap().to_string());
config::create(path.clone(), prefix); let prefix = prefix.trim().trim_end_matches('*');
config::create(path.clone(), prefix.to_string());
if tests { if tests {
config::create_test(path); config::create_test(path);
} }