Skip to content

Commit 3800d1b

Browse files
committed
feat(scaffold): overwrite argument
1 parent 54f3c61 commit 3800d1b

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/main.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod args {
2020
Scaffold {
2121
day: Day,
2222
download: bool,
23+
overwrite: bool,
2324
},
2425
Solve {
2526
day: Day,
@@ -65,6 +66,7 @@ mod args {
6566
Some("scaffold") => AppArguments::Scaffold {
6667
day: args.free_from_str()?,
6768
download: args.contains("--download"),
69+
overwrite: args.contains("--overwrite"),
6870
},
6971
Some("solve") => AppArguments::Solve {
7072
day: args.free_from_str()?,
@@ -104,8 +106,8 @@ fn main() {
104106
AppArguments::Time { day, all, store } => time::handle(day, all, store),
105107
AppArguments::Download { day } => download::handle(day),
106108
AppArguments::Read { day } => read::handle(day),
107-
AppArguments::Scaffold { day, download } => {
108-
scaffold::handle(day);
109+
AppArguments::Scaffold { day, download, overwrite } => {
110+
scaffold::handle(day, overwrite);
109111
if download {
110112
download::handle(day);
111113
}
@@ -120,7 +122,7 @@ fn main() {
120122
AppArguments::Today => {
121123
match Day::today() {
122124
Some(day) => {
123-
scaffold::handle(day);
125+
scaffold::handle(day, false);
124126
download::handle(day);
125127
read::handle(day)
126128
}

src/template/commands/scaffold.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,26 @@ use crate::template::Day;
99
const MODULE_TEMPLATE: &str =
1010
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/template.txt"));
1111

12-
fn safe_create_file(path: &str) -> Result<File, std::io::Error> {
13-
OpenOptions::new().write(true).create_new(true).open(path)
12+
fn safe_create_file(path: &str, overwrite: bool) -> Result<File, std::io::Error> {
13+
let mut file = OpenOptions::new();
14+
if overwrite {
15+
file.create(true);
16+
} else {
17+
file.create_new(true);
18+
}
19+
file.truncate(true).write(true).open(path)
1420
}
1521

1622
fn create_file(path: &str) -> Result<File, std::io::Error> {
1723
OpenOptions::new().write(true).create(true).open(path)
1824
}
1925

20-
pub fn handle(day: Day) {
26+
pub fn handle(day: Day, overwrite: bool) {
2127
let input_path = format!("data/inputs/{day}.txt");
2228
let example_path = format!("data/examples/{day}.txt");
2329
let module_path = format!("src/bin/{day}.rs");
2430

25-
let mut file = match safe_create_file(&module_path) {
31+
let mut file = match safe_create_file(&module_path, overwrite) {
2632
Ok(file) => file,
2733
Err(e) => {
2834
eprintln!("Failed to create module file: {e}");

0 commit comments

Comments
 (0)