From 77ff6cb7141d5d1bf64bc7eba6f1dec7962c856c Mon Sep 17 00:00:00 2001 From: SteveLauC Date: Sat, 27 Jan 2024 10:54:55 +0800 Subject: [PATCH] feat: support wildcard in ignored_containers (#666) --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + config.example.toml | 3 ++- src/steps/containers.rs | 15 ++++++++++----- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 18644341..bf67ceb2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2417,6 +2417,7 @@ dependencies = [ "tracing-subscriber", "walkdir", "which", + "wildmatch", "winapi", ] @@ -2712,6 +2713,12 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c168940144dd21fd8046987c16a46a33d5fc84eec29ef9dcddc2ac9e31526b7c" +[[package]] +name = "wildmatch" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "495ec47bf3c1345005f40724f0269362c8556cbc43aed0526ed44cae1d35fceb" + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index c9320290..bdcbcf9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,7 @@ tracing-subscriber = { version = "~0.3", features = ["env-filter", "time"] } merge = "~0.1" regex-split = "~0.1" notify-rust = "~4.10" +wildmatch = "2.3.0" [package.metadata.generate-rpm] assets = [{ source = "target/release/topgrade", dest = "/usr/bin/topgrade" }] diff --git a/config.example.toml b/config.example.toml index cb31dadb..b9d44428 100644 --- a/config.example.toml +++ b/config.example.toml @@ -229,4 +229,5 @@ # containers = ["archlinux-latest"] [containers] -# ignored_containers = ["ghcr.io/rancher-sandbox/rancher-desktop/rdx-proxy:latest"] +# Specify the containers to ignore while updating (Wildcard supported) +# ignored_containers = ["ghcr.io/rancher-sandbox/rancher-desktop/rdx-proxy:latest", "docker.io*"] diff --git a/src/steps/containers.rs b/src/steps/containers.rs index 5a56d09e..218009e8 100644 --- a/src/steps/containers.rs +++ b/src/steps/containers.rs @@ -6,6 +6,7 @@ use color_eyre::eyre::eyre; use color_eyre::eyre::Context; use color_eyre::eyre::Result; use tracing::{debug, error, warn}; +use wildmatch::WildMatch; use crate::command::CommandExt; use crate::error::{self, TopgradeError}; @@ -51,6 +52,13 @@ impl Display for Container { /// /// Containers specified in `ignored_containers` will be filtered out. fn list_containers(crt: &Path, ignored_containers: Option<&Vec>) -> Result> { + let ignored_containers = ignored_containers.map(|patterns| { + patterns + .iter() + .map(|pattern| WildMatch::new(pattern)) + .collect::>() + }); + debug!( "Querying '{} image ls --format \"{{{{.Repository}}}}:{{{{.Tag}}}}/{{{{.ID}}}}\"' for containers", crt.display() @@ -85,11 +93,8 @@ fn list_containers(crt: &Path, ignored_containers: Option<&Vec>) -> Resu assert_eq!(split_res.len(), 2); let (repo_tag, image_id) = (split_res[0], split_res[1]); - if let Some(ignored_containers) = ignored_containers { - if ignored_containers - .iter() - .any(|ignored_container| repo_tag.eq(ignored_container)) - { + if let Some(ref ignored_containers) = ignored_containers { + if ignored_containers.iter().any(|pattern| pattern.matches(repo_tag)) { debug!("Skipping ignored container '{}'", line); continue; }