From cfceb6ca466f3362ac74d0e0d25497018e4ef813 Mon Sep 17 00:00:00 2001 From: Jonathan Solskov <62329544+Glubiz@users.noreply.github.com> Date: Thu, 2 May 2024 06:57:17 +0000 Subject: [PATCH 01/19] Adding older-than flag --- src/main.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/run.rs | 12 ++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/main.rs b/src/main.rs index 255cc82..415a352 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,6 +37,7 @@ const DEFAULT_THRESHOLD: &str = "10 GB"; const DELETION_CHUNK_SIZE_OPTION: &str = "deletion-chunk-size"; const KEEP_OPTION: &str = "keep"; const THRESHOLD_OPTION: &str = "threshold"; +const OLDER_THAN_OPTION: &str = "older-than"; // Size threshold argument, absolute or relative to filesystem size #[derive(Copy, Clone)] @@ -110,6 +111,7 @@ pub struct Settings { threshold: Threshold, keep: Option, deletion_chunk_size: usize, + older_than: Option, } // Set up the logger. @@ -197,6 +199,13 @@ fn settings() -> io::Result { (default: {DEFAULT_DELETION_CHUNK_SIZE})", )), ) + .arg( + Arg::with_name(OLDER_THAN_OPTION) + .value_name("OLDER THAN") + .short("o") + .long(OLDER_THAN_OPTION) + .help("Specifies which images to delete based image creation time"), + ) .get_matches(); // Read the threshold. @@ -225,6 +234,37 @@ fn settings() -> io::Result { None => DEFAULT_DELETION_CHUNK_SIZE, }; + // validates the input argument parsed into --older-than, and converts the &str to a Duration + let older_than = match matches.value_of(OLDER_THAN_OPTION) { + Some(value) => { + let regex = Regex::new(r"(\d+)([smhd])").unwrap(); + if regex.is_match(value) { + if let Some(captures) = regex.captures(value) { + if let Some(matched_number) = captures.get(1) { + let duration = matched_number.as_str().parse::().unwrap(); + + let unit = value.chars().last().unwrap(); + + let duration = match unit { + 'm' => Duration::from_secs(duration * 60), + 'h' => Duration::from_secs(duration * 60 * 60), + 'd' => Duration::from_secs(duration * 60 * 60 * 24), + _ => Duration::from_secs(duration), // unit = s + }; + Some(duration) + } else { + None + } + } else { + None + } + } else { + None + } + }, + None => None, + }; + Ok(Settings { threshold, keep, diff --git a/src/run.rs b/src/run.rs index cb3967c..142ed5d 100644 --- a/src/run.rs +++ b/src/run.rs @@ -628,6 +628,7 @@ fn vacuum( threshold: Byte, keep: &Option, deletion_chunk_size: usize, + older_than: &Option, ) -> io::Result<()> { // Find all images. let image_records = list_image_records(state)?; @@ -670,6 +671,15 @@ fn vacuum( }); } + // If the user provided the `--older-than` argument, we need to filter out images which are newer than the provided duration. + if let Some(duration) = older_than { + let now = SystemTime::now(); + let time_stamp = (now - *duration).duration_since(UNIX_EPOCH).unwrap(); + sorted_image_nodes.retain(|(_, image_node)| { + image_node.last_used_since_epoch < time_stamp + }); + } + // Check if we're over the threshold. let mut deleted_image_ids = HashSet::new(); let space = space_usage()?; @@ -764,6 +774,7 @@ pub fn run( threshold, &settings.keep, settings.deletion_chunk_size, + &settings.older_than, )?; state::save(state)?; *first_run = false; @@ -842,6 +853,7 @@ pub fn run( threshold, &settings.keep, settings.deletion_chunk_size, + &settings.older_than, )?; } From abc76014532f23b6f688ef1192fe59c25f93859e Mon Sep 17 00:00:00 2001 From: Jonathan Solskov <62329544+Glubiz@users.noreply.github.com> Date: Thu, 2 May 2024 06:58:26 +0000 Subject: [PATCH 02/19] Updating README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 8e5e32f..835a533 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,9 @@ OPTIONS: -t, --threshold Sets the maximum amount of space to be used for Docker images (default: 10 GB) + -o, --older-than + Sets the minimum age of images to be considered for deletion + -v, --version Prints version information ``` From 3f62fbaa7d9086836e77e5cf3516f6cd72ba9be4 Mon Sep 17 00:00:00 2001 From: Jonathan Solskov <62329544+Glubiz@users.noreply.github.com> Date: Thu, 2 May 2024 09:10:25 +0000 Subject: [PATCH 03/19] Adding missing Regex to use in main.rs and add missing older_than to settings --- src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 415a352..229f707 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ use { clap::{App, AppSettings, Arg}, env_logger::{fmt::Color, Builder}, log::{Level, LevelFilter}, - regex::RegexSet, + regex::{Regex, RegexSet}, std::{ env, io::{self, Write}, @@ -269,6 +269,7 @@ fn settings() -> io::Result { threshold, keep, deletion_chunk_size, + older_than, }) } From 3230de3af9f85cd98b113dfc8cd3a1580c2a3bf1 Mon Sep 17 00:00:00 2001 From: Jonathan Solskov Jensen Date: Thu, 2 May 2024 14:26:16 +0200 Subject: [PATCH 04/19] Bumping version, adding parse_duration crate, updating changelog, renaming flag --- CHANGELOG.md | 5 ++++ Cargo.lock | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 3 +- README.md | 2 +- src/main.rs | 36 +++++++---------------- src/run.rs | 20 +++++++------ 6 files changed, 111 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca2844e..9ad8d75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.25.0] - 2024-05-02 + +### Fixed +- Added `--min-age` argument. + ## [0.24.0] - 2024-04-05 ### Fixed diff --git a/Cargo.lock b/Cargo.lock index f2bb73e..040d538 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -218,7 +218,7 @@ dependencies = [ [[package]] name = "docuum" -version = "0.24.0" +version = "0.25.0" dependencies = [ "atty", "byte-unit", @@ -229,6 +229,7 @@ dependencies = [ "dirs", "env_logger", "log", + "parse_duration", "regex", "serde", "serde_json", @@ -437,6 +438,73 @@ dependencies = [ "winapi", ] +[[package]] +name = "num" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8536030f9fea7127f841b45bb6243b27255787fb4eb83958aa1ef9d2fdc0c36" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "090c7f9998ee0ff65aa5b723e4009f7b217707f1fb5ea551329cc4d6231fb304" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6b19411a9719e753aff12e5187b74d60d3dc449ec3f4dc21e3989c3f554bc95" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d869c01cc0c455284163fd0092f1f93835385ccab5a98a0dcc497b2f8bf055a9" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c000134b5dbf44adc5cb772486d335293351644b801551abe8f75c84cfa4aef" +dependencies = [ + "autocfg", + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.16" @@ -462,6 +530,17 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" +[[package]] +name = "parse_duration" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7037e5e93e0172a5a96874380bf73bc6ecef022e26fa25f2be26864d6b3ba95d" +dependencies = [ + "lazy_static", + "num", + "regex", +] + [[package]] name = "proc-macro2" version = "1.0.66" diff --git a/Cargo.toml b/Cargo.toml index ab75564..8070427 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "docuum" -version = "0.24.0" +version = "0.25.0" authors = ["Stephan Boyer "] edition = "2021" description = "LRU eviction of Docker images." @@ -28,6 +28,7 @@ regex = { version = "1.5.5", default-features = false, features = ["std", "unico serde_json = "1.0" serde_yaml = "0.8" tempfile = "3" +parse_duration = "2.1.1" [target.'cfg(target_os = "linux")'.dependencies] sysinfo = "0.23.5" diff --git a/README.md b/README.md index 835a533..0e86459 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ OPTIONS: -t, --threshold Sets the maximum amount of space to be used for Docker images (default: 10 GB) - -o, --older-than + -m, --min-age Sets the minimum age of images to be considered for deletion -v, --version diff --git a/src/main.rs b/src/main.rs index 229f707..5fa7616 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ use { env_logger::{fmt::Color, Builder}, log::{Level, LevelFilter}, regex::{Regex, RegexSet}, + parse_duration::parse, std::{ env, io::{self, Write}, @@ -37,7 +38,7 @@ const DEFAULT_THRESHOLD: &str = "10 GB"; const DELETION_CHUNK_SIZE_OPTION: &str = "deletion-chunk-size"; const KEEP_OPTION: &str = "keep"; const THRESHOLD_OPTION: &str = "threshold"; -const OLDER_THAN_OPTION: &str = "older-than"; +const MIN_AGE_OPTION: &str = "min-age"; // Size threshold argument, absolute or relative to filesystem size #[derive(Copy, Clone)] @@ -111,7 +112,7 @@ pub struct Settings { threshold: Threshold, keep: Option, deletion_chunk_size: usize, - older_than: Option, + min_age: Option, } // Set up the logger. @@ -200,10 +201,10 @@ fn settings() -> io::Result { )), ) .arg( - Arg::with_name(OLDER_THAN_OPTION) + Arg::with_name(MIN_AGE_OPTION) .value_name("OLDER THAN") .short("o") - .long(OLDER_THAN_OPTION) + .long(MIN_AGE_OPTION) .help("Specifies which images to delete based image creation time"), ) .get_matches(); @@ -234,29 +235,14 @@ fn settings() -> io::Result { None => DEFAULT_DELETION_CHUNK_SIZE, }; - // validates the input argument parsed into --older-than, and converts the &str to a Duration - let older_than = match matches.value_of(OLDER_THAN_OPTION) { + // validates the input argument parsed into --min-age and converts input to a duration + 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) { - if let Some(captures) = regex.captures(value) { - if let Some(matched_number) = captures.get(1) { - let duration = matched_number.as_str().parse::().unwrap(); - - let unit = value.chars().last().unwrap(); - - let duration = match unit { - 'm' => Duration::from_secs(duration * 60), - 'h' => Duration::from_secs(duration * 60 * 60), - 'd' => Duration::from_secs(duration * 60 * 60 * 24), - _ => Duration::from_secs(duration), // unit = s - }; - Some(duration) - } else { - None - } - } else { - None + match parse(value) { + Ok(duration) => Some(duration), + Err(_) => None } } else { None @@ -269,7 +255,7 @@ fn settings() -> io::Result { threshold, keep, deletion_chunk_size, - older_than, + min_age, }) } diff --git a/src/run.rs b/src/run.rs index 142ed5d..22526e7 100644 --- a/src/run.rs +++ b/src/run.rs @@ -628,7 +628,7 @@ fn vacuum( threshold: Byte, keep: &Option, deletion_chunk_size: usize, - older_than: &Option, + min_age: &Option, ) -> io::Result<()> { // Find all images. let image_records = list_image_records(state)?; @@ -672,12 +672,16 @@ fn vacuum( } // If the user provided the `--older-than` argument, we need to filter out images which are newer than the provided duration. - if let Some(duration) = older_than { + if let Some(duration) = min_age { let now = SystemTime::now(); - let time_stamp = (now - *duration).duration_since(UNIX_EPOCH).unwrap(); - sorted_image_nodes.retain(|(_, image_node)| { - image_node.last_used_since_epoch < time_stamp - }); + match (now - *duration).duration_since(UNIX_EPOCH) { + Ok(time_stamp) => { + sorted_image_nodes.retain(|(_, image_node)| { + image_node.last_used_since_epoch < time_stamp + }); + }, + Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidInput, e)) + }; } // Check if we're over the threshold. @@ -774,7 +778,7 @@ pub fn run( threshold, &settings.keep, settings.deletion_chunk_size, - &settings.older_than, + &settings.min_age, )?; state::save(state)?; *first_run = false; @@ -853,7 +857,7 @@ pub fn run( threshold, &settings.keep, settings.deletion_chunk_size, - &settings.older_than, + &settings.min_age, )?; } From ff513c0c417def67fcb6590f13d3d621132a55f8 Mon Sep 17 00:00:00 2001 From: Jonathan Solskov Jensen Date: Thu, 2 May 2024 14:44:45 +0200 Subject: [PATCH 05/19] Change Fixed to Added changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ad8d75..d11993b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [0.25.0] - 2024-05-02 -### Fixed +### Added - Added `--min-age` argument. ## [0.24.0] - 2024-04-05 From 57e7b6f30eccaf09165b438b5fbbd96a429d8199 Mon Sep 17 00:00:00 2001 From: Jonathan Solskov Jensen Date: Thu, 2 May 2024 14:51:00 +0200 Subject: [PATCH 06/19] Correcting short name and comment mentioning --older-than --- src/main.rs | 4 ++-- src/run.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5fa7616..176fbcf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -202,8 +202,8 @@ fn settings() -> io::Result { ) .arg( Arg::with_name(MIN_AGE_OPTION) - .value_name("OLDER THAN") - .short("o") + .value_name("MIN AGE") + .short("m") .long(MIN_AGE_OPTION) .help("Specifies which images to delete based image creation time"), ) diff --git a/src/run.rs b/src/run.rs index 22526e7..ae670b6 100644 --- a/src/run.rs +++ b/src/run.rs @@ -671,7 +671,7 @@ fn vacuum( }); } - // If the user provided the `--older-than` argument, we need to filter out images which are newer than the provided duration. + // 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) { From a74373bb9416f1ff81d555edb053dbfc51231eff Mon Sep 17 00:00:00 2001 From: Glubiz Date: Fri, 3 May 2024 06:18:07 +0200 Subject: [PATCH 07/19] Implementing comment suggestion, error handling on parse error and removing regex --- src/main.rs | 34 +++++++--------------------------- src/run.rs | 3 +-- 2 files changed, 8 insertions(+), 29 deletions(-) diff --git a/src/main.rs b/src/main.rs index 176fbcf..4c5a573 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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] @@ -235,17 +220,12 @@ fn settings() -> io::Result { 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, diff --git a/src/run.rs b/src/run.rs index ae670b6..2ed3b13 100644 --- a/src/run.rs +++ b/src/run.rs @@ -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 From d5b3ee43560135b671d6606e3584fd607010908b Mon Sep 17 00:00:00 2001 From: Jonathan Solskov Jensen Date: Fri, 3 May 2024 07:13:49 +0200 Subject: [PATCH 08/19] Fixing imports formatting --- src/main.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4c5a573..66b9e70 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,22 @@ 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}, parse_duration::parse, regex::RegexSet, 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 } }; From 1b62823df3f48c1f12215c07abf2f179e9729868 Mon Sep 17 00:00:00 2001 From: Jonathan Solskov Jensen Date: Fri, 3 May 2024 07:30:52 +0200 Subject: [PATCH 09/19] Fix linting errors --- src/main.rs | 11 +++++------ src/run.rs | 5 ++--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 66b9e70..715da09 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,8 @@ use { parse_duration::parse, regex::RegexSet, std::{ - env, io::{self, Write}, + env, + io::{self, Write}, process::exit, str::FromStr, sync::{Arc, Mutex}, @@ -236,11 +237,9 @@ fn settings() -> io::Result { // Determine the minimum age for images to be considered for deletion. let min_age = match matches.value_of(MIN_AGE_OPTION) { - Some(value) => { - match parse(value) { - Ok(duration) => Some(duration), - Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidInput, e)), - } + Some(value) => match parse(value) { + Ok(duration) => Some(duration), + Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidInput, e)), }, None => None, }; diff --git a/src/run.rs b/src/run.rs index 2ed3b13..652db16 100644 --- a/src/run.rs +++ b/src/run.rs @@ -675,9 +675,8 @@ fn vacuum( if let Some(duration) = min_age { 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 - }); + sorted_image_nodes + .retain(|(_, image_node)| image_node.last_used_since_epoch < time_stamp); }, Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidInput, e)) }; From d89fa773dda0ec8b04bf1f9b79af285bfd1fe6d8 Mon Sep 17 00:00:00 2001 From: Jonathan Solskov Jensen Date: Fri, 3 May 2024 07:36:52 +0200 Subject: [PATCH 10/19] Moving commas to make linter happy --- src/main.rs | 2 +- src/run.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 715da09..5f92fc0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,7 @@ use { str::FromStr, sync::{Arc, Mutex}, thread::sleep, - time::Duration + time::Duration, } }; diff --git a/src/run.rs b/src/run.rs index 652db16..f188c67 100644 --- a/src/run.rs +++ b/src/run.rs @@ -677,8 +677,8 @@ fn vacuum( Ok(time_stamp) => { sorted_image_nodes .retain(|(_, image_node)| image_node.last_used_since_epoch < time_stamp); - }, - Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidInput, e)) + } + Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidInput, e)), }; } From d9538f1cea903c854df340ed5d544f2c1f4f026e Mon Sep 17 00:00:00 2001 From: Jonathan Solskov Jensen Date: Fri, 3 May 2024 07:41:41 +0200 Subject: [PATCH 11/19] Removing trailing space in imports --- src/main.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5f92fc0..83a8be8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,22 +3,22 @@ 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}, - parse_duration::parse, - regex::RegexSet, + 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, + env, + io::{self, Write}, + process::exit, + str::FromStr, + sync::{Arc, Mutex}, + thread::sleep, time::Duration, } }; From 14409a40a96afbe654c69f6a0e227dbc177c2b15 Mon Sep 17 00:00:00 2001 From: Jonathan Solskov Jensen Date: Fri, 3 May 2024 07:49:35 +0200 Subject: [PATCH 12/19] Fixing linting --- src/main.rs | 2 +- src/run.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 83a8be8..5385f58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ use { sync::{Arc, Mutex}, thread::sleep, time::Duration, - } + }, }; #[macro_use] diff --git a/src/run.rs b/src/run.rs index f188c67..2efed39 100644 --- a/src/run.rs +++ b/src/run.rs @@ -671,7 +671,8 @@ fn vacuum( }); } - // If the user provided the `--min-age` argument, we need to filter out images which are newer than the provided duration. + // If the `--min-age` argument is provided, we need to filter out images + // which are newer than the provided duration. if let Some(duration) = min_age { match (SystemTime::now() - *duration).duration_since(UNIX_EPOCH) { Ok(time_stamp) => { From abfd36ded1101b20ef954c40d1fa8002cfe4bb48 Mon Sep 17 00:00:00 2001 From: Jonathan Solskov Jensen Date: Fri, 3 May 2024 08:39:01 +0200 Subject: [PATCH 13/19] Changing Duration to all caps in README.md for consistency --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0e86459..d5cff03 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ OPTIONS: -t, --threshold Sets the maximum amount of space to be used for Docker images (default: 10 GB) - -m, --min-age + -m, --min-age Sets the minimum age of images to be considered for deletion -v, --version From 29ef0938c80f8f1c58c4d0ed0f1ea9ffbed0d807 Mon Sep 17 00:00:00 2001 From: Jonathan Solskov Jensen Date: Fri, 3 May 2024 08:50:56 +0200 Subject: [PATCH 14/19] Adding debug logging to retain --- src/run.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/run.rs b/src/run.rs index 2efed39..ecbada8 100644 --- a/src/run.rs +++ b/src/run.rs @@ -676,8 +676,22 @@ fn vacuum( if let Some(duration) = min_age { 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); + sorted_image_nodes.retain(|(_, image_node)| { + if image_node.last_used_since_epoch > time_stamp { + for repository_tag in &image_node.image_record.repository_tags { + debug!( + "Ignored image {} due to the {} flag.", + format!("{}:{}", repository_tag.repository, repository_tag.tag) + .code_str(), + "--min-age".code_str(), + ); + } + + return false; + } + + true + }); } Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidInput, e)), }; From 1e0a5fc06a95dc996d6ce7b981d17e1a0b494531 Mon Sep 17 00:00:00 2001 From: Jonathan Solskov Jensen Date: Fri, 3 May 2024 10:40:36 +0200 Subject: [PATCH 15/19] Adding image_id to debug log --- src/run.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/run.rs b/src/run.rs index ecbada8..69a93e8 100644 --- a/src/run.rs +++ b/src/run.rs @@ -676,16 +676,13 @@ fn vacuum( if let Some(duration) = min_age { match (SystemTime::now() - *duration).duration_since(UNIX_EPOCH) { Ok(time_stamp) => { - sorted_image_nodes.retain(|(_, image_node)| { + sorted_image_nodes.retain(|(image_id, image_node)| { if image_node.last_used_since_epoch > time_stamp { - for repository_tag in &image_node.image_record.repository_tags { - debug!( - "Ignored image {} due to the {} flag.", - format!("{}:{}", repository_tag.repository, repository_tag.tag) - .code_str(), - "--min-age".code_str(), - ); - } + debug!( + "Ignored image {} due to the {} flag.", + image_id, + "--min-age".code_str(), + ); return false; } From 4d89f540d89ef1c3b97713abd4728e58c38d99a5 Mon Sep 17 00:00:00 2001 From: Jonathan Solskov Jensen Date: Fri, 3 May 2024 10:52:59 +0200 Subject: [PATCH 16/19] Adding code_str() to image_id --- src/run.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/run.rs b/src/run.rs index 69a93e8..c0fd9ab 100644 --- a/src/run.rs +++ b/src/run.rs @@ -680,7 +680,7 @@ fn vacuum( if image_node.last_used_since_epoch > time_stamp { debug!( "Ignored image {} due to the {} flag.", - image_id, + image_id.code_str(), "--min-age".code_str(), ); From 9304bbb7f90210376b8cd795ebd32a172be48145 Mon Sep 17 00:00:00 2001 From: Jonathan Solskov Jensen Date: Fri, 3 May 2024 10:55:08 +0200 Subject: [PATCH 17/19] Matching value name and help from readme --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5385f58..59eeac9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -202,10 +202,10 @@ fn settings() -> io::Result { ) .arg( Arg::with_name(MIN_AGE_OPTION) - .value_name("MIN AGE") + .value_name("DURATION") .short("m") .long(MIN_AGE_OPTION) - .help("Specifies which images to delete based image creation time"), + .help("Sets the minimum age of images to be considered for deletion"), ) .get_matches(); From b6c71fcda63d0e5aa7fdf469479f6d5cda86a98c Mon Sep 17 00:00:00 2001 From: Jonathan Solskov Jensen Date: Fri, 3 May 2024 10:56:51 +0200 Subject: [PATCH 18/19] Updating readme and argument --- README.md | 2 +- src/main.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d5cff03..8b70aa3 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ OPTIONS: -t, --threshold Sets the maximum amount of space to be used for Docker images (default: 10 GB) - -m, --min-age + -m, --min-age Sets the minimum age of images to be considered for deletion -v, --version diff --git a/src/main.rs b/src/main.rs index 59eeac9..89e913c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -202,7 +202,7 @@ fn settings() -> io::Result { ) .arg( Arg::with_name(MIN_AGE_OPTION) - .value_name("DURATION") + .value_name("MIN AGE") .short("m") .long(MIN_AGE_OPTION) .help("Sets the minimum age of images to be considered for deletion"), From 359a2452829eb767be01f18150c465d0b59d0b3c Mon Sep 17 00:00:00 2001 From: Jonathan Solskov Jensen Date: Fri, 3 May 2024 11:06:48 +0200 Subject: [PATCH 19/19] Moving the --min-age argument above threshold in the readme to match --help output --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8b70aa3..aff7731 100644 --- a/README.md +++ b/README.md @@ -50,12 +50,12 @@ OPTIONS: -k, --keep ... Prevents deletion of images for which repository:tag matches - -t, --threshold - Sets the maximum amount of space to be used for Docker images (default: 10 GB) - -m, --min-age Sets the minimum age of images to be considered for deletion + -t, --threshold + Sets the maximum amount of space to be used for Docker images (default: 10 GB) + -v, --version Prints version information ```