Skip to content

Commit 3657f8c

Browse files
authored
feat(scaffold): overwrite argument (#63)
1 parent e2062e3 commit 3657f8c

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/main.rs

+5-3
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

+10-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ 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> {
@@ -21,12 +27,12 @@ fn create_file(path: &str) -> Result<File, std::io::Error> {
2127
.open(path)
2228
}
2329

24-
pub fn handle(day: Day) {
30+
pub fn handle(day: Day, overwrite: bool) {
2531
let input_path = format!("data/inputs/{day}.txt");
2632
let example_path = format!("data/examples/{day}.txt");
2733
let module_path = format!("src/bin/{day}.rs");
2834

29-
let mut file = match safe_create_file(&module_path) {
35+
let mut file = match safe_create_file(&module_path, overwrite) {
3036
Ok(file) => file,
3137
Err(e) => {
3238
eprintln!("Failed to create module file: {e}");

0 commit comments

Comments
 (0)