[pve-devel] [PATCH v1 installer 15/18] auto-installer: use glob crate for pattern matching
Aaron Lauterer
a.lauterer at proxmox.com
Tue Jan 23 18:00:50 CET 2024
Signed-off-by: Aaron Lauterer <a.lauterer at proxmox.com>
---
proxmox-auto-installer/Cargo.toml | 1 +
proxmox-auto-installer/src/utils.rs | 48 +++++++++++------------------
2 files changed, 19 insertions(+), 30 deletions(-)
diff --git a/proxmox-auto-installer/Cargo.toml b/proxmox-auto-installer/Cargo.toml
index 078a333..158a0a8 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 6e650c5..5990e93 100644
--- a/proxmox-auto-installer/src/utils.rs
+++ b/proxmox-auto-installer/src/utils.rs
@@ -1,4 +1,5 @@
use anyhow::{anyhow, bail, Context, Result};
+use glob::Pattern;
use log::info;
use std::{
collections::BTreeMap,
@@ -21,30 +22,12 @@ 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),
+ }
}
pub fn get_network_settings(
@@ -107,7 +90,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
}
@@ -132,7 +115,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;
@@ -465,9 +448,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