[pve-devel] [PATCH installer v6 17/36] auto-installer: use glob crate for pattern matching
Aaron Lauterer
a.lauterer at proxmox.com
Wed Apr 17 14:30:49 CEST 2024
Tested-by: Christoph Heiss <c.heiss at proxmox.com>
Reviewed-by: Christoph Heiss <c.heiss at proxmox.com>
Signed-off-by: Aaron Lauterer <a.lauterer at proxmox.com>
---
proxmox-auto-installer/Cargo.toml | 1 +
proxmox-auto-installer/src/utils.rs | 46 +++++++++++------------------
2 files changed, 18 insertions(+), 29 deletions(-)
diff --git a/proxmox-auto-installer/Cargo.toml b/proxmox-auto-installer/Cargo.toml
index b1d3e7a..741794a 100644
--- a/proxmox-auto-installer/Cargo.toml
+++ b/proxmox-auto-installer/Cargo.toml
@@ -9,6 +9,7 @@ homepage = "https://www.proxmox.com"
[dependencies]
anyhow = "1.0"
+glob = "0.3"
proxmox-installer-common = { path = "../proxmox-installer-common" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
diff --git a/proxmox-auto-installer/src/utils.rs b/proxmox-auto-installer/src/utils.rs
index ae28b1e..6101d66 100644
--- a/proxmox-auto-installer/src/utils.rs
+++ b/proxmox-auto-installer/src/utils.rs
@@ -1,4 +1,5 @@
use anyhow::{bail, Result};
+use glob::Pattern;
use log::info;
use std::{
collections::BTreeMap,
@@ -15,29 +16,11 @@ use proxmox_installer_common::{
};
use serde::Deserialize;
-/// Supports the globbing character '*' at the beginning, end or both of the pattern.
-/// Globbing within the pattern is not supported
-fn find_with_glob(pattern: &str, value: &str) -> bool {
- let globbing_symbol = '*';
- let mut start_glob = false;
- let mut end_glob = false;
- let mut pattern = pattern;
-
- if pattern.starts_with(globbing_symbol) {
- start_glob = true;
- pattern = &pattern[1..];
- }
-
- if pattern.ends_with(globbing_symbol) {
- end_glob = true;
- pattern = &pattern[..pattern.len() - 1]
- }
-
- match (start_glob, end_glob) {
- (true, true) => value.contains(pattern),
- (true, false) => value.ends_with(pattern),
- (false, true) => value.starts_with(pattern),
- _ => value == pattern,
+fn find_with_glob(pattern: &str, value: &str) -> Result<bool> {
+ let p = Pattern::new(pattern)?;
+ match p.matches(value) {
+ true => Ok(true),
+ false => Ok(false),
}
}
@@ -75,7 +58,7 @@ fn get_single_udev_index(
'outer: for (dev, dev_values) in udev_list {
for (filter_key, filter_value) in &filter {
for (udev_key, udev_value) in dev_values {
- if udev_key == filter_key && find_with_glob(filter_value, udev_value) {
+ if udev_key == filter_key && find_with_glob(filter_value, udev_value)? {
dev_index = Some(dev.clone());
break 'outer; // take first match
}
@@ -100,7 +83,7 @@ fn get_matched_udev_indexes(
let mut did_match_all = true;
for (filter_key, filter_value) in &filter {
for (udev_key, udev_value) in dev_values {
- if udev_key == filter_key && find_with_glob(filter_value, udev_value) {
+ if udev_key == filter_key && find_with_glob(filter_value, udev_value)? {
did_match_once = true;
} else if udev_key == filter_key {
did_match_all = false;
@@ -416,9 +399,14 @@ mod tests {
#[test]
fn test_glob_patterns() {
let test_value = "foobar";
- assert_eq!(find_with_glob("*bar", test_value), true);
- assert_eq!(find_with_glob("foo*", test_value), true);
- assert_eq!(find_with_glob("foobar", test_value), true);
- assert_eq!(find_with_glob("oobar", test_value), false);
+ assert_eq!(find_with_glob("*bar", test_value).unwrap(), true);
+ assert_eq!(find_with_glob("foo*", test_value).unwrap(), true);
+ assert_eq!(find_with_glob("foobar", test_value).unwrap(), true);
+ assert_eq!(find_with_glob("oobar", test_value).unwrap(), false);
+ assert_eq!(find_with_glob("f*bar", test_value).unwrap(), true);
+ assert_eq!(find_with_glob("f?bar", test_value).unwrap(), false);
+ assert_eq!(find_with_glob("fo?bar", test_value).unwrap(), true);
+ assert_eq!(find_with_glob("f[!a]obar", test_value).unwrap(), true);
+ assert_eq!(find_with_glob("f[oa]obar", test_value).unwrap(), true);
}
}
--
2.39.2
More information about the pve-devel
mailing list