use log crate
This commit is contained in:
parent
4c55e8cfff
commit
264102b92d
|
@ -25,6 +25,12 @@ version = "1.3.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.19"
|
||||
|
@ -74,6 +80,7 @@ version = "0.4.1"
|
|||
dependencies = [
|
||||
"chrono",
|
||||
"clap",
|
||||
"log",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
@ -127,6 +134,15 @@ version = "0.2.117"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c"
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.4.1"
|
||||
|
|
|
@ -12,4 +12,5 @@ readme = "README.md"
|
|||
serde = { version = "1.0", features = ["derive"] }
|
||||
clap = { version = "3.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
chrono = "0.4"
|
||||
chrono = "0.4"
|
||||
log = "0.4"
|
11
src/date.rs
11
src/date.rs
|
@ -1,6 +1,7 @@
|
|||
use chrono::{Date, DateTime, Datelike, FixedOffset, NaiveDate, NaiveDateTime, TimeZone, Utc};
|
||||
use serde::{self, Deserialize, Deserializer};
|
||||
use std::{num::TryFromIntError, process::exit};
|
||||
use log::error;
|
||||
|
||||
pub fn deserialize<'de, D>(deserializer: D) -> Result<DateTime<FixedOffset>, D::Error>
|
||||
where
|
||||
|
@ -25,7 +26,7 @@ fn get_day_month(year: i32, month: u32) -> u32 {
|
|||
.num_days(),
|
||||
)
|
||||
.unwrap_or_else(|_: TryFromIntError| {
|
||||
eprintln!("failed to convert i64 to u32");
|
||||
error!("failed to convert i64 to u32");
|
||||
exit(1);
|
||||
})
|
||||
}
|
||||
|
@ -44,7 +45,7 @@ pub fn get_past_date(mut year: i32, mut month: u32, mut day: u32, day_remove: u3
|
|||
2 => new_day,
|
||||
1 => new_day - 1,
|
||||
_ => {
|
||||
eprintln!("invalid day in condition...");
|
||||
error!("invalid day in condition...");
|
||||
exit(1)
|
||||
}
|
||||
}
|
||||
|
@ -74,14 +75,14 @@ pub fn parse_date(date: Option<String>) -> (DateTime<Utc>, Option<DateTime<Utc>>
|
|||
};
|
||||
|
||||
let base_from = NaiveDateTime::parse_from_str(&base_from, "%FT%T").unwrap_or_else(|e| {
|
||||
eprintln!(
|
||||
error!(
|
||||
"failed to parse from date. Verify its format (example: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS): {}",
|
||||
e
|
||||
);
|
||||
exit(1);
|
||||
});
|
||||
let base_to = NaiveDateTime::parse_from_str(&base_to, "%FT%T").unwrap_or_else(|e| {
|
||||
eprintln!(
|
||||
error!(
|
||||
"failed to parse to date. Verify its format (example: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS): {}",
|
||||
e
|
||||
);
|
||||
|
@ -94,7 +95,7 @@ pub fn parse_date(date: Option<String>) -> (DateTime<Utc>, Option<DateTime<Utc>>
|
|||
|
||||
let date = NaiveDateTime::parse_from_str(&formatted_date, "%FT%T")
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!(
|
||||
error!(
|
||||
"failed to parse date. Verify its format (example: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS): {}",
|
||||
e
|
||||
);
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use chrono::{DateTime, FixedOffset};
|
||||
use log::{error, warn};
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
num::ParseFloatError,
|
||||
|
@ -64,7 +65,7 @@ pub fn process_imgs(
|
|||
.unwrap_or_else(failed_convert_size)
|
||||
* 1000 as f32
|
||||
} else {
|
||||
eprintln!("Unknown size identification: {}", image.size);
|
||||
error!("Unknown size identification: {}", image.size);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
@ -85,18 +86,18 @@ fn get_images(repo: Option<String>) -> Vec<u8> {
|
|||
match cmd.output() {
|
||||
Ok(o) => {
|
||||
if !o.status.success() {
|
||||
eprintln!(
|
||||
error!(
|
||||
"{}",
|
||||
std::str::from_utf8(&o.stderr).expect("failed to parse STDERR to UTF-8")
|
||||
);
|
||||
eprintln!("failed to retrieve docker images. Please checkout STDERR");
|
||||
error!("failed to retrieve docker images. Please checkout STDERR");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
o.stdout
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("docker command failed: {}", e);
|
||||
error!("docker command failed: {}", e);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
@ -106,7 +107,7 @@ fn parse_imgs(repository: Option<String>) -> Vec<String> {
|
|||
let stdout = get_images(repository);
|
||||
|
||||
let output = String::from_utf8(stdout).unwrap_or_else(|e| {
|
||||
eprintln!("failed to parse docker output: {}", e);
|
||||
error!("failed to parse docker output: {}", e);
|
||||
exit(1);
|
||||
});
|
||||
let mut images: Vec<String> = output.lines().map(|s| s.to_string()).collect();
|
||||
|
@ -114,7 +115,7 @@ fn parse_imgs(repository: Option<String>) -> Vec<String> {
|
|||
images.remove(images.len() - 1);
|
||||
|
||||
if images.len() == 0 {
|
||||
println!("No images found for current timestamp and/or repository");
|
||||
warn!("No images found for current timestamp and/or repository");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
@ -122,6 +123,6 @@ fn parse_imgs(repository: Option<String>) -> Vec<String> {
|
|||
}
|
||||
|
||||
fn failed_convert_size(e: ParseFloatError) -> f32 {
|
||||
eprintln!("failed to convert \"String\" to \"f32\": {}", e);
|
||||
error!("failed to convert \"String\" to \"f32\": {}", e);
|
||||
exit(1);
|
||||
}
|
||||
|
|
19
src/main.rs
19
src/main.rs
|
@ -3,6 +3,7 @@ mod images;
|
|||
|
||||
use clap::Parser;
|
||||
use std::process::{Command, Stdio};
|
||||
use log::{error, info};
|
||||
|
||||
use crate::images::process_imgs;
|
||||
|
||||
|
@ -46,41 +47,41 @@ fn main() {
|
|||
let (ids, saved_size) = process_imgs(args.repository, tags, args.date);
|
||||
|
||||
if args.dry_run {
|
||||
println!("dry run activated");
|
||||
info!("dry run activated");
|
||||
} else {
|
||||
let mut cmd = Command::new(DOCKER_BIN);
|
||||
cmd.arg("rmi");
|
||||
|
||||
if args.force {
|
||||
println!("\"--force\" flag set");
|
||||
info!("\"--force\" flag set");
|
||||
cmd.arg("--force");
|
||||
}
|
||||
|
||||
if ids.len() == 0 {
|
||||
println!("nothing to do...");
|
||||
info!("nothing to do...");
|
||||
return;
|
||||
}
|
||||
|
||||
if args.verbose {
|
||||
println!("trigger \"docker rmi\" command");
|
||||
info!("trigger \"docker rmi\" command");
|
||||
}
|
||||
|
||||
match cmd.args(&ids).stdout(Stdio::null()).status() {
|
||||
Ok(s) => {
|
||||
if !s.success() {
|
||||
eprintln!("failed to delete images. Please checkout STDERR")
|
||||
error!("failed to delete images. Please checkout STDERR")
|
||||
}
|
||||
|
||||
println!("images deleted!")
|
||||
info!("images deleted!")
|
||||
}
|
||||
Err(e) => eprintln!("docker command failed: {}", e),
|
||||
Err(e) => error!("docker command failed: {}", e),
|
||||
};
|
||||
}
|
||||
|
||||
if args.verbose || args.dry_run {
|
||||
println!("deleted images: {:#?}", ids);
|
||||
info!("deleted images: {:#?}", ids);
|
||||
}
|
||||
println!(
|
||||
info!(
|
||||
"Total disk space saved: {}",
|
||||
if saved_size / 1000 as f32 > 1 as f32 {
|
||||
format!("{:.2}GB", saved_size / 1000.0)
|
||||
|
|
Reference in New Issue