diff --git a/Cargo.lock b/Cargo.lock index bc18255..e25f6fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -76,7 +76,7 @@ dependencies = [ [[package]] name = "clear-docker-images" -version = "0.4.1" +version = "0.4.2" dependencies = [ "chrono", "clap", diff --git a/Cargo.toml b/Cargo.toml index b29e252..20f2277 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "clear-docker-images" -version = "0.4.1" +version = "0.4.2" edition = "2021" authors = ["Antoine Langlois"] description = "small binary to clean up docker images (mostly in CI)" diff --git a/Earthfile b/Earthfile index e607e2e..83e47cf 100644 --- a/Earthfile +++ b/Earthfile @@ -10,9 +10,16 @@ WORKDIR /clear-docker-images build-linux: COPY . . + RUN rustup target add x86_64-unknown-linux-gnu RUN cargo build --release --target x86_64-unknown-linux-gnu SAVE ARTIFACT target/x86_64-unknown-linux-gnu /x86_64-unknown-linux-gnu AS LOCAL target/x86_64-unknown-linux-gnu +build-musl: + COPY . . + RUN rustup target add x86_64-unknown-linux-musl + RUN cargo build --release --target x86_64-unknown-linux-musl + SAVE ARTIFACT target/x86_64-unknown-linux-musl /x86_64-unknown-linux-musl AS LOCAL target/x86_64-unknown-linux-musl + build-images: FROM docker:20.10.12-dind-alpine3.15 @@ -20,7 +27,7 @@ build-images: LABEL repository="https://github.com/DataHearth/clear-docker-images" LABEL org.opencontainers.image.source="https://github.com/DataHearth/clear-docker-images" - COPY +build-linux/x86_64-unknown-linux-gnu /usr/local/bin/clear-docker-images + COPY +build-musl/x86_64-unknown-linux-musl/release/clear-docker-images /usr/local/bin/clear-docker-images VOLUME ["/var/run/docker.sock"] diff --git a/Makefile b/Makefile index e6d4dd7..2c31b98 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION := 0.4.1 +VERSION := 0.4.2 .PHONY: bump-version bump-version: @@ -7,6 +7,7 @@ bump-version: @sd "version = \"${VERSION}\"" "version = \"${NEW_VERSION}\"" Cargo.toml @sd "VERSION=${VERSION}" "VERSION=${NEW_VERSION}" .env @sd "VERSION := ${VERSION}" "VERSION := ${NEW_VERSION}" Makefile + @cargo update --package=clear-docker-images @git add . @git commit -m "bump v${NEW_VERSION}" @git tag -m "bump v${NEW_VERSION}" v${NEW_VERSION} diff --git a/src/images.rs b/src/images.rs index e1d4f6f..6a9bb42 100644 --- a/src/images.rs +++ b/src/images.rs @@ -49,7 +49,7 @@ where if size.contains("KB") { size.replace("KB", "") .parse::() - .map(|s| s / 1000 as f32) + .map(|s| s / 1000.0) .map_err(serde::de::Error::custom) } else if size.contains("MB") { size.replace("MB", "") @@ -58,7 +58,7 @@ where } else if size.contains("GB") { size.replace("GB", "") .parse::() - .map(|s| s * 1000 as f32) + .map(|s| s * 1000.0) .map_err(serde::de::Error::custom) } else { Err(serde::de::Error::custom(format!( @@ -131,13 +131,10 @@ fn parse_imgs(repository: Option) -> Vec { error!("failed to parse docker output: {}", e); exit(1); }); - let mut images: Vec = output.lines().map(|s| s.to_string()).collect(); - // * remove last empty line - images.remove(images.len() - 1); + let images: Vec = output.lines().map(|s| s.to_string()).collect(); if images.len() == 0 { warn!("No images found for current timestamp and/or repository"); - exit(1); } return images; diff --git a/src/main.rs b/src/main.rs index 84099b9..fdd0f08 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,12 +17,11 @@ const TWO_DAYS_TIMESTAMP: i64 = 172_800; struct Args { /// filter by date. /// - /// Can filter by a minimum age $DATE or from $START|$STOP (format example: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS) [default: $NOW - 2 days] + /// Can filter by a minimum age $DATE or from $START|$STOP (format example: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS) [default: $NOW - 2d] #[clap(short, long, parse(try_from_str = parse_user_date))] - date: DateArgs, + date: Option, /// filter by repository name - #[clap(short, long)] repository: Option, /// add tags exclusion @@ -49,15 +48,30 @@ pub struct DateArgs { } fn main() { - if SimpleLogger::new().init().is_err() { - eprintln!("failed to initialize logger"); + let args = Args::parse(); + let logger = SimpleLogger::new() + .without_timestamps() + .with_level(match args.verbose { + true => log::LevelFilter::Debug, + false => log::LevelFilter::Info, + }); + + if let Some(e) = logger.init().err() { + eprintln!("failed to initialize logger: {}", e); exit(1); } - let args = Args::parse(); - - let tags = args.tags.map_or(vec![], |tags| tags); - let (ids, saved_size) = process_imgs(args.repository, tags, args.date); + let (ids, saved_size) = process_imgs( + args.repository, + args.tags.map_or(vec![], |tags| tags), + args.date.map_or( + DateArgs { + start: Utc::now().timestamp() - TWO_DAYS_TIMESTAMP, + stop: None, + }, + |d| d, + ), + ); if args.dry_run { info!("dry run activated"); @@ -91,12 +105,12 @@ fn main() { }; } - if args.verbose || args.dry_run { + if args.dry_run { info!("deleted images: {:#?}", ids); } info!( "Total disk space saved: {}", - if saved_size / 1000 as f32 > 1 as f32 { + if saved_size / 1000.0 > 1.0 { format!("{:.2}GB", saved_size / 1000.0) } else { format!("{:.2}MB", saved_size)