Skip to content

Commit

Permalink
Implementing comment suggestion, error handling on parse error and re…
Browse files Browse the repository at this point in the history
…moving regex
  • Loading branch information
Glubiz committed May 3, 2024
1 parent 57e7b6f commit a74373b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 29 deletions.
34 changes: 7 additions & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,9 @@ mod run;
mod state;

use {
crate::{format::CodeStr, run::run},
atty::Stream,
byte_unit::Byte,
chrono::Local,
clap::{App, AppSettings, Arg},
env_logger::{fmt::Color, Builder},
log::{Level, LevelFilter},
regex::{Regex, RegexSet},
parse_duration::parse,
std::{
env,
io::{self, Write},
process::exit,
str::FromStr,
sync::{Arc, Mutex},
thread::sleep,
time::Duration,
},
crate::{format::CodeStr, run::run}, atty::Stream, byte_unit::Byte, chrono::Local, clap::{App, AppSettings, Arg}, env_logger::{fmt::Color, Builder}, log::{Level, LevelFilter}, parse_duration::parse, regex::RegexSet, std::{
env, io::{self, Write}, process::exit, str::FromStr, sync::{Arc, Mutex}, thread::sleep, time::Duration
}
};

#[macro_use]
Expand Down Expand Up @@ -235,17 +220,12 @@ fn settings() -> io::Result<Settings> {
None => DEFAULT_DELETION_CHUNK_SIZE,
};

// validates the input argument parsed into --min-age and converts input to a duration
// Determine the minimum age for images to be considered for deletion.
let min_age = match matches.value_of(MIN_AGE_OPTION) {
Some(value) => {
let regex = Regex::new(r"(\d+)([smhd])").unwrap();
if regex.is_match(value) {
match parse(value) {
Ok(duration) => Some(duration),
Err(_) => None
}
} else {
None
match parse(value) {
Ok(duration) => Some(duration),
Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidInput, e)),
}
},
None => None,
Expand Down
3 changes: 1 addition & 2 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,8 +673,7 @@ fn vacuum(

// If the user provided the `--min-age` argument, we need to filter out images which are newer than the provided duration.
if let Some(duration) = min_age {
let now = SystemTime::now();
match (now - *duration).duration_since(UNIX_EPOCH) {
match (SystemTime::now() - *duration).duration_since(UNIX_EPOCH) {
Ok(time_stamp) => {
sorted_image_nodes.retain(|(_, image_node)| {
image_node.last_used_since_epoch < time_stamp
Expand Down

0 comments on commit a74373b

Please sign in to comment.