feat(scaffold): overwrite argument (#63)
This commit is contained in:
@@ -9,8 +9,14 @@ use crate::template::Day;
|
||||
const MODULE_TEMPLATE: &str =
|
||||
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/template.txt"));
|
||||
|
||||
fn safe_create_file(path: &str) -> Result<File, std::io::Error> {
|
||||
OpenOptions::new().write(true).create_new(true).open(path)
|
||||
fn safe_create_file(path: &str, overwrite: bool) -> Result<File, std::io::Error> {
|
||||
let mut file = OpenOptions::new();
|
||||
if overwrite {
|
||||
file.create(true);
|
||||
} else {
|
||||
file.create_new(true);
|
||||
}
|
||||
file.truncate(true).write(true).open(path)
|
||||
}
|
||||
|
||||
fn create_file(path: &str) -> Result<File, std::io::Error> {
|
||||
@@ -21,12 +27,12 @@ fn create_file(path: &str) -> Result<File, std::io::Error> {
|
||||
.open(path)
|
||||
}
|
||||
|
||||
pub fn handle(day: Day) {
|
||||
pub fn handle(day: Day, overwrite: bool) {
|
||||
let input_path = format!("data/inputs/{day}.txt");
|
||||
let example_path = format!("data/examples/{day}.txt");
|
||||
let module_path = format!("src/bin/{day}.rs");
|
||||
|
||||
let mut file = match safe_create_file(&module_path) {
|
||||
let mut file = match safe_create_file(&module_path, overwrite) {
|
||||
Ok(file) => file,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to create module file: {e}");
|
||||
|
||||
Reference in New Issue
Block a user