From c1bc7437176177beb5927eaa2751a3a7d0e6809d Mon Sep 17 00:00:00 2001 From: Nikolai Rodionov Date: Sun, 14 Jan 2024 18:38:12 +0100 Subject: [PATCH] Patches can be defined globally --- helmule.yaml | 31 +++++++++++-------------- src/config/mod.rs | 1 + src/config/patch.rs | 55 ++++++++++++++++++++++++++++++++++++++++----- src/main.rs | 2 +- 4 files changed, 65 insertions(+), 24 deletions(-) diff --git a/helmule.yaml b/helmule.yaml index 0c7a237..235419a 100644 --- a/helmule.yaml +++ b/helmule.yaml @@ -1,7 +1,16 @@ +patches: + - name: yamlfmt + custom_command: + commands: + - |- + cat <> .yamlfmt + formatter: + pad_line_comments: 2 + EOT + - yamlfmt values.yaml --conf ./yamlfmt.yaml + - rm -f yamlfmt.yaml + # mirror charts -include: - - kind: Charts - path: ./examples/use/charts/vaultwardens.yaml repositories: - name: bitnami-oci helm: @@ -21,11 +30,6 @@ repositories: helm: url: https://fluxcd-community.github.io/helm-charts charts: - - name: postgresql - repository: bitnami-oci - version: 13.2.29 - mirrors: - - badhouseplants-git - name: flux2 repository: flux-community extensions: @@ -48,16 +52,7 @@ charts: - name: Remove installCRDs value from the default values regexp: path: ./examples/patches/flux-regexp - - name: yaml-fmt - custom_command: - commands: - - |- - cat <> .yamlfmt - formatter: - pad_line_comments: 2 - EOT - - yamlfmt values.yaml --conf ./yamlfmt.yaml - - rm -f yamlfmt.yaml + - name: yamlfmt mirrors: - custom-command mirrors: diff --git a/src/config/mod.rs b/src/config/mod.rs index 6f380f9..c169718 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -8,6 +8,7 @@ pub(crate) mod include; #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)] pub(crate) struct Config { + pub(crate) patches: Option>, pub(crate) include: Option>, pub(crate) variables: Option>, pub(crate) repositories: Option>, diff --git a/src/config/patch.rs b/src/config/patch.rs index 6eec37a..7bd2b52 100644 --- a/src/config/patch.rs +++ b/src/config/patch.rs @@ -41,6 +41,7 @@ pub(crate) struct CustomCommandPatch { #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)] pub(crate) struct Patch { + name: Option, regexp: Option, git: Option, custom_command: Option, @@ -48,8 +49,14 @@ pub(crate) struct Patch { } impl Patch { - pub(crate) fn apply(&self, chart_local_path: String) -> Result<(), Box> { - let patch_action = patch_action_from_definition(self.clone())?; + pub(crate) fn apply(&self, chart_local_path: String, global_patches: Option>) -> Result<(), Box> { + let patch_action: Box; + if self.is_ref(){ + let patch_ref = self.get_ref(global_patches)?; + patch_action = Box::from(patch_action_from_definition(patch_ref)?); + } else { + patch_action = Box::from(patch_action_from_definition(self.clone())?); + } patch_action.apply(chart_local_path) } pub(crate) fn get_path(&self) -> String { @@ -63,7 +70,45 @@ impl Patch { git.path = path; } else if let Some(ref mut yq) = self.yq { yq.file = path - } + } + } + + fn is_ref(&self) -> bool { + self.regexp.is_none() + && self.git.is_none() + && self.custom_command.is_none() + && self.yq.is_none() + && self.name.is_some() + } + + pub(crate) fn get_ref( + &self, + global_patches: Option>, + ) -> Result> { + match global_patches { + Some(patches) => { + let patch = patches + .iter() + .find(|&patch| patch.clone().name.unwrap() == self.clone().name.unwrap()); + match patch { + Some(patch) => { + return Ok(patch.clone()); + } + None => { + return Err(Box::from(format!( + "global patch is not found: {}", + self.clone().name.unwrap() + ))) + } + } + } + None => { + return Err(Box::from(format!( + "patch {} is recognized as a reference, but global patches are not defined", + self.clone().name.unwrap() + ))) + } + } } } @@ -225,9 +270,9 @@ fn patch_action_from_definition( return Ok(Box::new(GitPatch { path: { let path = PathBuf::from(git.path.clone()); - match fs::canonicalize(path).ok(){ + match fs::canonicalize(path).ok() { Some(can_path) => can_path.into_os_string().into_string().ok().unwrap(), - None => git.path.clone(), + None => git.path.clone(), } }, })); diff --git a/src/main.rs b/src/main.rs index 7623044..034a211 100644 --- a/src/main.rs +++ b/src/main.rs @@ -160,7 +160,7 @@ fn main() { } if let Some(patches) = chart.patches.clone() { for patch in patches { - if let Err(err) = patch.apply(res.clone().path) { + if let Err(err) = patch.apply(res.clone().path, config.patches.clone()) { error!("{}", err); exit(1); }