Refactor templates

This commit is contained in:
Nikolai Rodionov 2024-01-13 18:53:53 +01:00
parent ebc93dc6d0
commit 492e15a0af
Signed by: allanger
GPG Key ID: 0AA46A90E25592AD
7 changed files with 40 additions and 16 deletions

View File

@ -61,6 +61,8 @@ pub(crate) struct GitMirror {
pub(crate) branch: String, pub(crate) branch: String,
pub(crate) commit: Option<String>, pub(crate) commit: Option<String>,
pub(crate) git_dir: Option<String>, pub(crate) git_dir: Option<String>,
pub(crate) rebase: Option<bool>,
pub(crate) default_branch: Option<String>,
} }
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)] #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]

View File

@ -1,2 +1,3 @@
pub(crate) mod cli; pub(crate) mod cli;
pub(crate) mod copy; pub(crate) mod copy;
pub(crate) mod template;

View File

@ -1,5 +1,6 @@
use chrono::prelude::*; use chrono::prelude::*;
use handlebars::{handlebars_helper, Handlebars}; use handlebars::{handlebars_helper, Handlebars};
use serde::Serialize;
handlebars_helper!(date_helper: | | Utc::now().format("%Y-%m-%d").to_string()); handlebars_helper!(date_helper: | | Utc::now().format("%Y-%m-%d").to_string());
handlebars_helper!(time_helper: | | Utc::now().format("%H-%M-%S").to_string()); handlebars_helper!(time_helper: | | Utc::now().format("%H-%M-%S").to_string());
@ -11,6 +12,17 @@ pub(crate) fn register_handlebars() -> Handlebars<'static> {
handlebars handlebars
} }
pub (crate) fn render<T>(string: String, data: &T) -> Result<String, Box<dyn std::error::Error>>
where
T: Serialize,
{
let mut reg = register_handlebars();
let tmpl_name = "template";
reg.register_template_string(tmpl_name, string)?;
let result = reg.render(tmpl_name, data)?;
Ok(result)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::collections::HashMap; use std::collections::HashMap;

View File

@ -3,7 +3,6 @@ pub(crate) mod helpers;
pub(crate) mod mirror; pub(crate) mod mirror;
pub(crate) mod patch; pub(crate) mod patch;
pub(crate) mod source; pub(crate) mod source;
pub(crate) mod template;
use clap::Parser; use clap::Parser;
use log::{error, info}; use log::{error, info};

View File

@ -1,5 +1,5 @@
use super::Target; use super::Target;
use crate::{helpers::cli::cli_exec_from_dir, template}; use crate::{helpers::cli::cli_exec_from_dir, helpers::template};
pub(crate) struct CustomCommands { pub(crate) struct CustomCommands {
pub(crate) package: Vec<String>, pub(crate) package: Vec<String>,

View File

@ -1,4 +1,4 @@
use crate::{helpers::cli::cli_exec_from_dir, source::ChartLocal, template}; use crate::{helpers::cli::cli_exec_from_dir, source::ChartLocal, helpers::template};
use dircpy::*; use dircpy::*;
use super::Target; use super::Target;
@ -9,6 +9,8 @@ pub(crate) struct Git {
pub(crate) path: String, pub(crate) path: String,
pub(crate) branch: String, pub(crate) branch: String,
pub(crate) commit: Option<String>, pub(crate) commit: Option<String>,
pub(crate) default_branch: Option<String>,
pub(crate) rebase: bool,
} }
impl Target for Git { impl Target for Git {
@ -18,29 +20,33 @@ impl Target for Git {
chart_local: ChartLocal, chart_local: ChartLocal,
dry_run: bool, dry_run: bool,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
let mut reg = template::register_handlebars();
// Prepare the URL // Prepare the URL
reg.register_template_string("url", self.url.clone())?; let url = template::render(self.url.clone(), &chart_local)?;
let url = reg.render("url", &chart_local)?;
//Prepare the git dir //Prepare the git dir
reg.register_template_string("git_dir", self.git_dir.clone())?; let git_dir = template::render(self.git_dir.clone(), &chart_local)?;
let git_dir = reg.render("git_dir", &chart_local)?;
let cmd = format!("git clone {} {}", url, git_dir); let cmd = format!("git clone {} {}", url, git_dir);
cli_exec_from_dir(cmd, workdir_path.clone())?; cli_exec_from_dir(cmd, workdir_path.clone())?;
let git_repo_path = format!("{}/{}", workdir_path, git_dir); let git_repo_path = format!("{}/{}", workdir_path, git_dir);
// Prepare branch // Prepare branch
reg.register_template_string("branch", self.branch.clone())?; let branch = template::render(self.branch.clone(), &chart_local)?;
let branch = reg.render("branch", &chart_local)?;
let cmd = format!("git checkout {}", branch); let cmd = format!("git checkout {}", branch);
if let Err(_) = cli_exec_from_dir(cmd, git_repo_path.clone()) { if let Err(_) = cli_exec_from_dir(cmd, git_repo_path.clone()) {
let cmd = format!("git checkout -b {}", branch); let cmd = format!("git checkout -b {}", branch);
cli_exec_from_dir(cmd, git_repo_path.clone())?; cli_exec_from_dir(cmd, git_repo_path.clone())?;
}; };
let mut git_args: String = String::new();
if self.rebase {
let default_branch = match self.default_branch.clone() {
Some(db) => db,
None => "main".to_string(),
};
let cmd = format!("git rebase {}", default_branch);
cli_exec_from_dir(cmd, git_repo_path.clone())?;
git_args = "--force".to_string();
}
// Prepare path // Prepare path
reg.register_template_string("path", self.path.clone())?; let path = template::render(self.path.clone(), &chart_local)?;
let path = reg.render("path", &chart_local)?;
let repo_local_full_path = format!("{}/{}", git_repo_path, path); let repo_local_full_path = format!("{}/{}", git_repo_path, path);
CopyBuilder::new(chart_local.path.clone(), repo_local_full_path.clone()) CopyBuilder::new(chart_local.path.clone(), repo_local_full_path.clone())
.overwrite_if_size_differs(true) .overwrite_if_size_differs(true)
@ -51,15 +57,14 @@ impl Target for Git {
Some(commit) => commit, Some(commit) => commit,
None => "helmuled {{ name }}-{{ version }}".to_string(), None => "helmuled {{ name }}-{{ version }}".to_string(),
}; };
reg.register_template_string("commit", commit_message.clone())?; let commit = template::render(commit_message.clone(), &chart_local)?;
let commit = reg.render("commit", &chart_local)?;
let cmd = format!( let cmd = format!(
"git add . && git diff --staged --quiet || git commit -m '{}'", "git add . && git diff --staged --quiet || git commit -m '{}'",
commit commit
); );
cli_exec_from_dir(cmd, repo_local_full_path.clone())?; cli_exec_from_dir(cmd, repo_local_full_path.clone())?;
if !dry_run { if !dry_run {
let cmd = format!("git push --set-upstream origin {}", branch); let cmd = format!("git push --set-upstream origin {} {}", branch, git_args);
cli_exec_from_dir(cmd, repo_local_full_path)?; cli_exec_from_dir(cmd, repo_local_full_path)?;
} }
Ok(()) Ok(())

View File

@ -28,6 +28,11 @@ pub(crate) fn mirror_from_mirror_obj(
}, },
branch: git.branch, branch: git.branch,
commit: git.commit, commit: git.commit,
default_branch: git.default_branch,
rebase: match git.rebase {
Some(r) => r,
None => false,
},
})); }));
} else if let Some(command) = mirror.custom_command { } else if let Some(command) = mirror.custom_command {
return Ok(Box::from(custom_command::CustomCommands { return Ok(Box::from(custom_command::CustomCommands {