add tag exclusion | force option
This commit is contained in:
parent
8a83b259f1
commit
c28ba1bb26
29
src/main.rs
29
src/main.rs
|
@ -17,13 +17,21 @@ 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 {
|
||||||
/// repository name
|
/// filter by repository name
|
||||||
#[clap(short, long)]
|
#[clap(short, long)]
|
||||||
repository: Option<String>,
|
repository: Option<String>,
|
||||||
|
|
||||||
|
/// add tags exclusion
|
||||||
|
#[clap(short, long)]
|
||||||
|
tags: Option<Vec<String>>,
|
||||||
|
|
||||||
/// image cleanup will not be triggered
|
/// image cleanup will not be triggered
|
||||||
#[clap(long, takes_value = false)]
|
#[clap(long, takes_value = false)]
|
||||||
dry_run: bool,
|
dry_run: bool,
|
||||||
|
|
||||||
|
/// should docker force image removal (it may create orphan images)
|
||||||
|
#[clap(long, takes_value = false)]
|
||||||
|
force: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
|
@ -32,6 +40,8 @@ struct Image {
|
||||||
created_at: DateTime<FixedOffset>,
|
created_at: DateTime<FixedOffset>,
|
||||||
#[serde(rename = "ID")]
|
#[serde(rename = "ID")]
|
||||||
id: String,
|
id: String,
|
||||||
|
#[serde(rename = "Tag")]
|
||||||
|
tag: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -69,24 +79,29 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
let now = Utc::now();
|
let now = Utc::now();
|
||||||
let day = now.day();
|
|
||||||
let month = now.month();
|
|
||||||
let year = now.year();
|
|
||||||
|
|
||||||
let past_date = get_past_date(year, month, 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 mut ids = vec![];
|
let mut ids = vec![];
|
||||||
|
|
||||||
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() {
|
||||||
ids.push(image.id);
|
if !tags.contains(&image.tag) {
|
||||||
|
ids.push(image.id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if args.dry_run {
|
if args.dry_run {
|
||||||
println!("dry run activated");
|
println!("dry run activated");
|
||||||
} else {
|
} else {
|
||||||
match Command::new(DOCKER_BIN)
|
let mut cmd = Command::new(DOCKER_BIN);
|
||||||
|
if args.force {
|
||||||
|
cmd.arg("-f");
|
||||||
|
}
|
||||||
|
|
||||||
|
match cmd
|
||||||
.args(DOCKER_RMI_CMD)
|
.args(DOCKER_RMI_CMD)
|
||||||
.args(&ids)
|
.args(&ids)
|
||||||
.stdout(Stdio::null())
|
.stdout(Stdio::null())
|
||||||
|
|
Reference in New Issue