From 4d33d9d4a0f79a4590fc576a2040899292cacdbb Mon Sep 17 00:00:00 2001 From: Robert Femmer Date: Wed, 4 Sep 2024 22:03:24 +0200 Subject: [PATCH 1/5] fix: sanitize helm version string by removing leading 'v' characters --- src/lib.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index c41973f..788cebd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -362,10 +362,18 @@ impl HelmClient { .output() .map_err(HelmError::HelmNotInstalled)?; let version_text = String::from_utf8(helm_version.stdout).map_err(HelmError::Utf8Error)?; - Ok(version_text[1..].trim().to_string()) + Ok(sanitize_helm_version_string(&version_text)) } } +/// Sanitize the version string returned by helm +/// +/// Returns a sanitized version text parseable by the semver crate +fn sanitize_helm_version_string(version_text: &str) -> String { + // Helm version strings may come with leading 'v' or without. Strip it, if it exists. + version_text.trim_start_matches('v').trim().to_string() +} + /// Check for errors in Helm's stderr output /// /// Returns `Ok(())` if everything is fine, or `HelmError` if something is wrong @@ -431,4 +439,12 @@ mod tests { assert_eq!(test_chart.name, "test_chart"); assert_eq!(test_chart.chart, "test_chart-1.2.32-rc2"); } + + #[test] + fn test_sanitize_version_string() { + // As reported by most (?) helm versions + assert_eq!(&sanitize_helm_version_string("v3.15.4+gfa9efb0"), "3.15.4+gfa9efb0"); + // As reported by helm on Fedora 40 + assert_eq!(&sanitize_helm_version_string("3.15.4+gfa9efb0"), "3.15.4+gfa9efb0"); + } } From df4d2ce499a83ad22cdc7f732cb33021c1c1eeb1 Mon Sep 17 00:00:00 2001 From: Robert Femmer Date: Wed, 4 Sep 2024 22:18:06 +0200 Subject: [PATCH 2/5] fix: trim white spaces first, then leading v's --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 788cebd..8e0c87b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -371,7 +371,7 @@ impl HelmClient { /// Returns a sanitized version text parseable by the semver crate fn sanitize_helm_version_string(version_text: &str) -> String { // Helm version strings may come with leading 'v' or without. Strip it, if it exists. - version_text.trim_start_matches('v').trim().to_string() + version_text.trim().trim_start_matches('v').to_string() } /// Check for errors in Helm's stderr output From 085baced71151c1a14c453b450dd26e3f5060de4 Mon Sep 17 00:00:00 2001 From: Robert Femmer Date: Wed, 4 Sep 2024 22:59:57 +0200 Subject: [PATCH 3/5] satisfy rustfmt --- src/lib.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8e0c87b..416fc0f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -443,8 +443,14 @@ mod tests { #[test] fn test_sanitize_version_string() { // As reported by most (?) helm versions - assert_eq!(&sanitize_helm_version_string("v3.15.4+gfa9efb0"), "3.15.4+gfa9efb0"); + assert_eq!( + &sanitize_helm_version_string("v3.15.4+gfa9efb0"), + "3.15.4+gfa9efb0" + ); // As reported by helm on Fedora 40 - assert_eq!(&sanitize_helm_version_string("3.15.4+gfa9efb0"), "3.15.4+gfa9efb0"); + assert_eq!( + &sanitize_helm_version_string("3.15.4+gfa9efb0"), + "3.15.4+gfa9efb0" + ); } } From a1fbd2ef8eff3a2cdaf44ea606ab3c86bec6c645 Mon Sep 17 00:00:00 2001 From: Robert Femmer Date: Wed, 4 Sep 2024 23:03:25 +0200 Subject: [PATCH 4/5] satisfy clippy --- src/lib.rs | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 416fc0f..a936dd6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -77,21 +77,21 @@ impl InstallArg { pub fn install(&self) -> Command { let mut command = Command::new("helm"); - command.args(&["install", &self.name, &self.chart]); + command.args(["install", &self.name, &self.chart]); self.apply_args(&mut command); command } pub fn upgrade(&self) -> Command { let mut command = Command::new("helm"); - command.args(&["upgrade", "--install", &self.name, &self.chart]); + command.args(["upgrade", "--install", &self.name, &self.chart]); self.apply_args(&mut command); command } fn apply_args(&self, command: &mut Command) { if let Some(namespace) = &self.namespace { - command.args(&["--namespace", namespace]); + command.args(["--namespace", namespace]); } if self.develop { @@ -99,7 +99,7 @@ impl InstallArg { } if let Some(version) = &self.version { - command.args(&["--version", version]); + command.args(["--version", version]); } for value_path in &self.values { @@ -115,10 +115,10 @@ impl InstallArg { impl From for Command { fn from(arg: InstallArg) -> Self { let mut command = Command::new("helm"); - command.args(&["install", &arg.name, &arg.chart]); + command.args(["install", &arg.name, &arg.chart]); if let Some(namespace) = &arg.namespace { - command.args(&["--namespace", namespace]); + command.args(["--namespace", namespace]); } if arg.develop { @@ -126,7 +126,7 @@ impl From for Command { } if let Some(version) = &arg.version { - command.args(&["--version", version]); + command.args(["--version", version]); } for value_path in &arg.values { @@ -190,17 +190,17 @@ impl UninstallArg { impl From for Command { fn from(arg: UninstallArg) -> Self { let mut command = Command::new("helm"); - command.args(&["uninstall", &arg.release]); + command.args(["uninstall", &arg.release]); if let Some(namespace) = &arg.namespace { - command.args(&["--namespace", namespace]); + command.args(["--namespace", namespace]); } if arg.dry_run { command.arg("--dry-run"); } - for timeout in &arg.timeout { + if let Some(timeout) = arg.timeout { command.arg("--timeout").arg(timeout); } @@ -270,7 +270,7 @@ impl HelmClient { #[instrument(skip(self))] pub fn repo_add(&self, chart: &str, location: &str) -> Result<(), HelmError> { Command::new("helm") - .args(&["repo", "add", chart, location]) + .args(["repo", "add", chart, location]) .result()?; Ok(()) } @@ -278,7 +278,7 @@ impl HelmClient { /// Updates the local helm repository #[instrument(skip(self))] pub fn repo_update(&self) -> Result<(), HelmError> { - Command::new("helm").args(&["repo", "update"]).result()?; + Command::new("helm").args(["repo", "update"]).result()?; Ok(()) } @@ -287,9 +287,9 @@ impl HelmClient { pub fn search_repo(&self, chart: &str, version: &str) -> Result, HelmError> { let mut command = Command::new("helm"); command - .args(&["search", "repo", chart]) - .args(&["--version", version]) - .args(&["--output", "json"]); + .args(["search", "repo", chart]) + .args(["--version", version]) + .args(["--output", "json"]); let output = command.result()?; @@ -302,9 +302,9 @@ impl HelmClient { pub fn versions(&self, chart: &str) -> Result, HelmError> { let mut command = Command::new("helm"); command - .args(&["search", "repo"]) - .args(&["--versions", chart]) - .args(&["--output", "json", "--devel"]); + .args(["search", "repo"]) + .args(["--versions", chart]) + .args(["--output", "json", "--devel"]); let output = command.result()?; check_helm_stderr(output.stderr)?; @@ -340,11 +340,11 @@ impl HelmClient { match namespace { Some(ns) => { - command.args(&["--namespace", ns]); + command.args(["--namespace", ns]); } None => { // Search all namespaces - command.args(&["-A"]); + command.args(["-A"]); } } @@ -433,8 +433,7 @@ mod tests { let installed_charts: Vec = serde_json::from_slice(JSON_RESPONSE.as_bytes()).expect("can not parse json"); assert_eq!(installed_charts.len(), 1); - let test_chart = installed_charts - .get(0) + let test_chart = installed_charts.first() .expect("can not grab the first result"); assert_eq!(test_chart.name, "test_chart"); assert_eq!(test_chart.chart, "test_chart-1.2.32-rc2"); From 0986eba7b555007df0b120cea31aad61f1b22ee8 Mon Sep 17 00:00:00 2001 From: Robert Femmer Date: Wed, 4 Sep 2024 23:21:23 +0200 Subject: [PATCH 5/5] satisfy rustfmt again --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index a936dd6..9950044 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -433,7 +433,8 @@ mod tests { let installed_charts: Vec = serde_json::from_slice(JSON_RESPONSE.as_bytes()).expect("can not parse json"); assert_eq!(installed_charts.len(), 1); - let test_chart = installed_charts.first() + let test_chart = installed_charts + .first() .expect("can not grab the first result"); assert_eq!(test_chart.name, "test_chart"); assert_eq!(test_chart.chart, "test_chart-1.2.32-rc2");