Skip to content

feat: input-sync command #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use advent_of_code::template::commands::{all, download, read, scaffold, solve, time};
use advent_of_code::template::commands::{all, download, input_sync, read, scaffold, solve, time};
use args::{parse, AppArguments};

#[cfg(feature = "today")]
@@ -36,6 +36,9 @@ mod args {
day: Option<Day>,
store: bool,
},
InputSync {
replace: bool,
},
#[cfg(feature = "today")]
Today,
}
@@ -74,6 +77,9 @@ mod args {
submit: args.opt_value_from_str("--submit")?,
dhat: args.contains("--dhat"),
},
Some("input-sync") => AppArguments::InputSync {
replace: args.contains("--replace"),
},
#[cfg(feature = "today")]
Some("today") => AppArguments::Today,
Some(x) => {
@@ -122,6 +128,7 @@ fn main() {
dhat,
submit,
} => solve::handle(day, release, dhat, submit),
AppArguments::InputSync { replace } => input_sync::handle(replace),
#[cfg(feature = "today")]
AppArguments::Today => {
match Day::today() {
55 changes: 44 additions & 11 deletions src/template/aoc_cli.rs
Original file line number Diff line number Diff line change
@@ -13,6 +13,41 @@ pub enum AocCommandError {
BadExitStatus(Output),
}

pub enum DownloadMode {
InputOnly,
PuzzleOnly,
InputAndPuzzle,
}

impl DownloadMode {
fn modify_args(&self, input_path: &str, puzzle_path: &str) -> Vec<String> {
let mut args = vec!["--overwrite".into()];
match self {
DownloadMode::InputOnly => {
args.extend(["--input-only", "--input-file", input_path].iter().map(|s| s.to_string()));
}
DownloadMode::PuzzleOnly => {
args.extend(["--puzzle-only", "--puzzle-file", puzzle_path].iter().map(|s| s.to_string()));
}
DownloadMode::InputAndPuzzle => {
args.extend([
"--input-file", input_path,
"--puzzle-file", puzzle_path
].iter().map(|s| s.to_string()));
}
}
args
}

fn downloads_input(&self) -> bool {
matches!(self, DownloadMode::InputOnly | DownloadMode::InputAndPuzzle)
}

fn downloads_puzzle(&self) -> bool {
matches!(self, DownloadMode::PuzzleOnly | DownloadMode::InputAndPuzzle)
}
}

impl Display for AocCommandError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
@@ -49,26 +84,24 @@ pub fn read(day: Day) -> Result<Output, AocCommandError> {
call_aoc_cli(&args)
}

pub fn download(day: Day) -> Result<Output, AocCommandError> {
pub fn download(day: Day, download_variants: DownloadMode) -> Result<Output, AocCommandError> {
let input_path = get_input_path(day);
let puzzle_path = get_puzzle_path(day);

let args = build_args(
let args = &build_args(
"download",
&[
"--overwrite".into(),
"--input-file".into(),
input_path.to_string(),
"--puzzle-file".into(),
puzzle_path.to_string(),
],
&download_variants.modify_args(&input_path, &puzzle_path)[..],
day,
);

let output = call_aoc_cli(&args)?;
println!("---");
println!("🎄 Successfully wrote input to \"{}\".", &input_path);
println!("🎄 Successfully wrote puzzle to \"{}\".", &puzzle_path);
if download_variants.downloads_input() {
println!("🎄 Successfully wrote input to \"{}\".", &input_path);
}
if download_variants.downloads_puzzle() {
println!("🎄 Successfully wrote puzzle to \"{}\".", &puzzle_path);
}
Ok(output)
}

3 changes: 2 additions & 1 deletion src/template/commands/download.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::template::{aoc_cli, Day};
use std::process;
use crate::template::aoc_cli::DownloadMode;

pub fn handle(day: Day) {
if aoc_cli::check().is_err() {
eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it.");
process::exit(1);
}

if let Err(e) = aoc_cli::download(day) {
if let Err(e) = aoc_cli::download(day, DownloadMode::InputAndPuzzle) {
eprintln!("failed to call aoc-cli: {e}");
process::exit(1);
};
23 changes: 23 additions & 0 deletions src/template/commands/input_sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::fs::{exists};
use crate::template::{all_days};
use crate::template::aoc_cli::{download, DownloadMode};

pub fn handle(replace_existing: bool) {
for day in all_days() {
let input_path = format!("data/inputs/{day}.txt");
let existence = exists(input_path);
if existence.is_err() {
eprintln!("Unable to check whether input already exists?");
eprintln!("Hint: Check for listing permissions");
eprintln!("Skipping day: {}", day);
continue;
}
if !replace_existing && existence.unwrap() { continue; }
let result = download(day, DownloadMode::InputOnly);
if result.is_err() {
eprintln!("Unable to download input for day: {}", day);
eprintln!("Halting further downloads!");
return;
}
}
}
1 change: 1 addition & 0 deletions src/template/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -4,3 +4,4 @@ pub mod read;
pub mod scaffold;
pub mod solve;
pub mod time;
pub mod input_sync;