add total size

This commit is contained in:
DataHearth 2022-02-17 20:28:39 +01:00
parent bc7aa42001
commit 30ca47d5c0
No known key found for this signature in database
GPG Key ID: E88FD356ACC5F3C4
1 changed files with 58 additions and 4 deletions

View File

@ -5,7 +5,10 @@ use clap::Parser;
use date::get_past_date; use date::get_past_date;
use serde::Deserialize; use serde::Deserialize;
use serde_json; use serde_json;
use std::process::{Command, Stdio}; use std::{
num::ParseFloatError,
process::{exit, Command, Stdio},
};
const DOCKER_BIN: &str = "docker"; const DOCKER_BIN: &str = "docker";
const DOCKER_IMGS_CMD: [&str; 1] = ["images"]; const DOCKER_IMGS_CMD: [&str; 1] = ["images"];
@ -17,6 +20,9 @@ const DAYS_RM: u32 = 2;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)] #[clap(author, version, about, long_about = None)]
struct Args { struct Args {
#[clap(short, long, takes_value = false)]
verbose: bool,
/// filter by repository name /// filter by repository name
#[clap(short, long)] #[clap(short, long)]
repository: Option<String>, repository: Option<String>,
@ -42,6 +48,8 @@ struct Image {
id: String, id: String,
#[serde(rename = "Tag")] #[serde(rename = "Tag")]
tag: String, tag: String,
#[serde(rename = "Size")]
size: String,
} }
fn main() { fn main() {
@ -79,16 +87,41 @@ fn main() {
} }
let now = Utc::now(); let now = Utc::now();
let past_date = get_past_date(now.year(), now.month(), now.day(), DAYS_RM).and_hms(1, 0, 0); let past_date = get_past_date(now.year(), now.month(), now.day(), DAYS_RM).and_hms(1, 0, 0);
let tags = if let Some(t) = args.tags { t } else { vec![] }; let tags = if let Some(t) = args.tags { t } else { vec![] };
let mut ids = vec![]; let mut ids = vec![];
let mut saved_size: f32 = 0.0;
for img in images { for img in images {
let image: Image = serde_json::from_str(img).unwrap(); let image: Image = serde_json::from_str(img).unwrap();
if image.created_at.timestamp() <= past_date.timestamp() { if image.created_at.timestamp() <= past_date.timestamp() {
if !tags.contains(&image.tag) { if !tags.contains(&image.tag) {
ids.push(image.id); ids.push(image.id);
saved_size += if image.size.contains("KB") {
image
.size
.replace("KB", "")
.parse::<f32>()
.unwrap_or_else(failed_convert_size)
/ 1000 as f32
} else if image.size.contains("MB") {
image
.size
.replace("MB", "")
.parse::<f32>()
.unwrap_or_else(failed_convert_size)
} else if image.size.contains("GB") {
image
.size
.replace("GB", "")
.parse::<f32>()
.unwrap_or_else(failed_convert_size)
* 1000 as f32
} else {
eprintln!("Unknown size identification: {}", image.size);
exit(1);
}
} }
} }
} }
@ -100,11 +133,17 @@ fn main() {
cmd.args(DOCKER_RMI_CMD); cmd.args(DOCKER_RMI_CMD);
if args.force { if args.force {
println!("\"--force\" flag set");
cmd.arg("--force"); cmd.arg("--force");
} }
if ids.len() == 0 { if ids.len() == 0 {
return println!("nothing to do..."); println!("nothing to do...");
return;
}
if args.verbose {
println!("trigger \"docker rmi\" command");
} }
match cmd.args(&ids).stdout(Stdio::null()).status() { match cmd.args(&ids).stdout(Stdio::null()).status() {
@ -119,5 +158,20 @@ fn main() {
}; };
} }
println!("deleted images: {:#?}", ids); if args.verbose || args.dry_run {
println!("deleted images: {:#?}", ids);
}
println!(
"Total disk space saved: {}",
if saved_size / 1000 as f32 > 1 as f32 {
format!("{:.2}GB", saved_size / 1000.0)
} else {
format!("{:.2}MB", saved_size)
}
);
}
fn failed_convert_size(e: ParseFloatError) -> f32 {
eprintln!("failed to convert \"String\" to \"f32\": {}", e);
exit(1);
} }