Add variables and template mirror url

This commit is contained in:
2024-01-10 15:50:56 +01:00
parent b36bde0b83
commit 0f936a645c
8 changed files with 64 additions and 25 deletions

View File

@ -1,12 +1,13 @@
use log::{info, warn};
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::{fs::File, collections::HashMap};
pub(crate) mod extension;
pub(crate) mod patch;
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub(crate) struct Config {
pub(crate) variables: Option<HashMap<String, String>>,
pub(crate) repositories: Vec<Repository>,
pub(crate) charts: Vec<Chart>,
pub(crate) mirrors: Vec<Mirror>,
@ -54,6 +55,7 @@ pub(crate) struct GitMirror {
pub(crate) path: Option<String>,
pub(crate) branch: String,
pub(crate) commit: Option<String>,
pub(crate) git_dir: Option<String>,
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
@ -82,6 +84,7 @@ pub(crate) struct Chart {
// A repository object
pub(crate) extensions: Option<Vec<extension::Extension>>,
pub(crate) patches: Option<Vec<patch::Patch>>,
pub(crate) variables: Option<HashMap<String, String>>,
#[serde(skip_serializing)]
pub(crate) repository_obj: Option<Repository>,
#[serde(skip_serializing)]
@ -89,6 +92,18 @@ pub(crate) struct Chart {
}
impl Chart {
pub(crate) fn populate_variables(&mut self, global_variables: Option<HashMap<String, String>>) {
if let Some(global_vars) = global_variables {
self.variables = match self.variables.clone() {
Some(mut vars) => {
vars.extend(global_vars);
Some(vars)
},
None => Some(global_vars),
}
};
}
pub(crate) fn populate_repository(
&mut self,
repositories: Vec<Repository>,

View File

@ -61,19 +61,22 @@ trait PatchInterface {
impl PatchInterface for YqPatch {
fn apply(&self, chart_local_path: String) -> Result<(), Box<dyn std::error::Error>> {
let cmd = match self.op {
YqOperations::Add => format!(
"yq -i '{} += \"{}\"' {}",
self.key,
self.value.clone().unwrap(),
self.file
),
YqOperations::Add => {
let value = match self.value.clone().unwrap().starts_with(['{', '[', '\"', '\'']) {
true => self.value.clone().unwrap(),
false => format!("\"{}\"", self.value.clone().unwrap()),
};
format!("yq -i '{} += {}' {}", self.key, value, self.file)
}
YqOperations::Delete => format!("yq -i \'del({})\' {}", self.key, self.file),
YqOperations::Replace => format!(
"yq e -i \'{} = \"{}\"\' {}",
self.key,
self.value.clone().unwrap(),
self.file
),
YqOperations::Replace => {
let value = match self.value.clone().unwrap().starts_with(['{', '[']) {
true => self.value.clone().unwrap(),
false => format!("\"{}\"", self.value.clone().unwrap()),
};
format!("yq e -i '{} = {}' {}", self.key, value, self.file)
}
};
cli_exec_from_dir(cmd, chart_local_path)?;
Ok(())