Skip to content

Commit

Permalink
feat: add option to overwrite files if they exist
Browse files Browse the repository at this point in the history
  • Loading branch information
scarvalhojr committed Nov 25, 2022
1 parent 5e19acc commit 0b4e972
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "aoc-cli"
description = "Advent of Code command-line helper tool"
version = "0.4.8"
version = "0.5.0"
authors = ["Sergio de Carvalho <[email protected]>"]
categories = ["command-line-utilities"]
edition = "2021"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Options:
-y, --year <YEAR> Puzzle year [default: year of current or last Advent of Code event]
-s, --session-file <PATH> Path to session cookie file [default: ~/.adventofcode.session]
-w, --width <WIDTH> Width at which to wrap output [default: terminal width]
-o, --overwrite Overwrite file if it already exists
-i, --input-file <PATH> Path where to save puzzle input [default: input]
-p, --puzzle-file <PATH> Path where to save puzzle description [default: puzzle.md]
-h, --help Print help information
Expand Down
26 changes: 20 additions & 6 deletions src/aoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ pub fn download_input(
opt_year: Option<PuzzleYear>,
opt_day: Option<PuzzleDay>,
filename: &str,
overwrite: bool,
) -> Result<(), String> {
let (year, day) = puzzle_year_day(opt_year, opt_day)?;

Expand All @@ -119,9 +120,15 @@ pub fn download_input(
.map_err(|err| err.to_string())?;

eprintln!("Saving puzzle input to \"{}\"...", filename);
OpenOptions::new()
.write(true)
.create_new(true)

let mut file = OpenOptions::new();
if overwrite {
file.create(true);
} else {
file.create_new(true);
};

file.write(true)
.open(filename)
.map_err(|err| format!("Failed to create file: {}", err))?
.write(puzzle_input.as_bytes())
Expand Down Expand Up @@ -173,6 +180,7 @@ pub fn read_puzzle(
opt_day: Option<PuzzleDay>,
col_width: usize,
filename: &str,
overwrite: bool,
) -> Result<(), String> {
let (year, day) = puzzle_year_day(opt_year, opt_day)?;

Expand All @@ -196,9 +204,15 @@ pub fn read_puzzle(
println!("\n{}", from_read(description.as_bytes(), col_width));

println!("Saving puzzle description to \"{}\"...", filename);
OpenOptions::new()
.write(true)
.create_new(true)

let mut file = OpenOptions::new();
if overwrite {
file.create(true);
} else {
file.create_new(true);
};

file.write(true)
.open(filename)
.map_err(|err| format!("Failed to create file: {}", err))?
.write(parse_html(description).as_bytes())
Expand Down
4 changes: 4 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ pub struct Args {
#[arg(short, long, global = true, value_parser = valid_width)]
pub width: Option<usize>,

/// Overwrite file if it already exists
#[arg(short, long, global = true)]
pub overwrite: bool,

/// Path where to save puzzle input
#[arg(
short,
Expand Down
21 changes: 15 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,24 @@ fn main() -> Result<(), String> {
.unwrap_or(DEFAULT_COL_WIDTH);

match &args.command {
Some(Command::Download) => {
download_input(&session, args.year, args.day, &args.input_file)
}
Some(Command::Download) => download_input(
&session,
args.year,
args.day,
&args.input_file,
args.overwrite,
),
Some(Command::Submit { part, answer }) => {
submit_answer(&session, args.year, args.day, part, answer, width)
}
_ => {
read_puzzle(&session, args.year, args.day, width, &args.puzzle_file)
}
_ => read_puzzle(
&session,
args.year,
args.day,
width,
&args.puzzle_file,
args.overwrite,
),
}
}

Expand Down

0 comments on commit 0b4e972

Please sign in to comment.