Skip to content

Commit 82607f1

Browse files
committed
feat(scaffold): overwrite argument
1 parent 54f3c61 commit 82607f1

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-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: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,24 @@ 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+
if overwrite {
14+
OpenOptions::new().write(true).create(true).truncate(true).open(path)
15+
} else {
16+
OpenOptions::new().write(true).create_new(true).open(path)
17+
}
1418
}
1519

1620
fn create_file(path: &str) -> Result<File, std::io::Error> {
1721
OpenOptions::new().write(true).create(true).open(path)
1822
}
1923

20-
pub fn handle(day: Day) {
24+
pub fn handle(day: Day, overwrite: bool) {
2125
let input_path = format!("data/inputs/{day}.txt");
2226
let example_path = format!("data/examples/{day}.txt");
2327
let module_path = format!("src/bin/{day}.rs");
2428

25-
let mut file = match safe_create_file(&module_path) {
29+
let mut file = match safe_create_file(&module_path, overwrite) {
2630
Ok(file) => file,
2731
Err(e) => {
2832
eprintln!("Failed to create module file: {e}");

0 commit comments

Comments
 (0)