Skip to content

Commit

Permalink
feat(scaffold): overwrite argument (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
AV3RG authored Oct 31, 2024
1 parent e2062e3 commit 3657f8c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod args {
Scaffold {
day: Day,
download: bool,
overwrite: bool,
},
Solve {
day: Day,
Expand Down Expand Up @@ -65,6 +66,7 @@ mod args {
Some("scaffold") => AppArguments::Scaffold {
day: args.free_from_str()?,
download: args.contains("--download"),
overwrite: args.contains("--overwrite"),
},
Some("solve") => AppArguments::Solve {
day: args.free_from_str()?,
Expand Down Expand Up @@ -104,8 +106,8 @@ fn main() {
AppArguments::Time { day, all, store } => time::handle(day, all, store),
AppArguments::Download { day } => download::handle(day),
AppArguments::Read { day } => read::handle(day),
AppArguments::Scaffold { day, download } => {
scaffold::handle(day);
AppArguments::Scaffold { day, download, overwrite } => {
scaffold::handle(day, overwrite);
if download {
download::handle(day);
}
Expand All @@ -120,7 +122,7 @@ fn main() {
AppArguments::Today => {
match Day::today() {
Some(day) => {
scaffold::handle(day);
scaffold::handle(day, false);
download::handle(day);
read::handle(day)
}
Expand Down
14 changes: 10 additions & 4 deletions src/template/commands/scaffold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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}");
Expand Down

0 comments on commit 3657f8c

Please sign in to comment.