Refactor templates
This commit is contained in:
parent
ebc93dc6d0
commit
492e15a0af
@ -61,6 +61,8 @@ pub(crate) struct GitMirror {
|
||||
pub(crate) branch: String,
|
||||
pub(crate) commit: 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)]
|
||||
|
@ -1,2 +1,3 @@
|
||||
pub(crate) mod cli;
|
||||
pub(crate) mod copy;
|
||||
pub(crate) mod template;
|
||||
|
@ -1,5 +1,6 @@
|
||||
use chrono::prelude::*;
|
||||
use handlebars::{handlebars_helper, Handlebars};
|
||||
use serde::Serialize;
|
||||
|
||||
handlebars_helper!(date_helper: | | Utc::now().format("%Y-%m-%d").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
|
||||
}
|
||||
|
||||
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)]
|
||||
mod tests {
|
||||
use std::collections::HashMap;
|
@ -3,7 +3,6 @@ pub(crate) mod helpers;
|
||||
pub(crate) mod mirror;
|
||||
pub(crate) mod patch;
|
||||
pub(crate) mod source;
|
||||
pub(crate) mod template;
|
||||
|
||||
use clap::Parser;
|
||||
use log::{error, info};
|
||||
|
@ -1,5 +1,5 @@
|
||||
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) package: Vec<String>,
|
||||
|
@ -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 super::Target;
|
||||
@ -9,6 +9,8 @@ pub(crate) struct Git {
|
||||
pub(crate) path: String,
|
||||
pub(crate) branch: String,
|
||||
pub(crate) commit: Option<String>,
|
||||
pub(crate) default_branch: Option<String>,
|
||||
pub(crate) rebase: bool,
|
||||
}
|
||||
|
||||
impl Target for Git {
|
||||
@ -18,29 +20,33 @@ impl Target for Git {
|
||||
chart_local: ChartLocal,
|
||||
dry_run: bool,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut reg = template::register_handlebars();
|
||||
// Prepare the URL
|
||||
reg.register_template_string("url", self.url.clone())?;
|
||||
let url = reg.render("url", &chart_local)?;
|
||||
let url = template::render(self.url.clone(), &chart_local)?;
|
||||
//Prepare the git dir
|
||||
reg.register_template_string("git_dir", self.git_dir.clone())?;
|
||||
let git_dir = reg.render("git_dir", &chart_local)?;
|
||||
|
||||
let git_dir = template::render(self.git_dir.clone(), &chart_local)?;
|
||||
let cmd = format!("git clone {} {}", url, git_dir);
|
||||
cli_exec_from_dir(cmd, workdir_path.clone())?;
|
||||
let git_repo_path = format!("{}/{}", workdir_path, git_dir);
|
||||
|
||||
// Prepare branch
|
||||
reg.register_template_string("branch", self.branch.clone())?;
|
||||
let branch = reg.render("branch", &chart_local)?;
|
||||
let branch = template::render(self.branch.clone(), &chart_local)?;
|
||||
let cmd = format!("git checkout {}", branch);
|
||||
if let Err(_) = cli_exec_from_dir(cmd, git_repo_path.clone()) {
|
||||
let cmd = format!("git checkout -b {}", branch);
|
||||
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
|
||||
reg.register_template_string("path", self.path.clone())?;
|
||||
let path = reg.render("path", &chart_local)?;
|
||||
let path = template::render(self.path.clone(), &chart_local)?;
|
||||
let repo_local_full_path = format!("{}/{}", git_repo_path, path);
|
||||
CopyBuilder::new(chart_local.path.clone(), repo_local_full_path.clone())
|
||||
.overwrite_if_size_differs(true)
|
||||
@ -51,15 +57,14 @@ impl Target for Git {
|
||||
Some(commit) => commit,
|
||||
None => "helmuled {{ name }}-{{ version }}".to_string(),
|
||||
};
|
||||
reg.register_template_string("commit", commit_message.clone())?;
|
||||
let commit = reg.render("commit", &chart_local)?;
|
||||
let commit = template::render(commit_message.clone(), &chart_local)?;
|
||||
let cmd = format!(
|
||||
"git add . && git diff --staged --quiet || git commit -m '{}'",
|
||||
commit
|
||||
);
|
||||
cli_exec_from_dir(cmd, repo_local_full_path.clone())?;
|
||||
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)?;
|
||||
}
|
||||
Ok(())
|
||||
|
@ -28,6 +28,11 @@ pub(crate) fn mirror_from_mirror_obj(
|
||||
},
|
||||
branch: git.branch,
|
||||
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 {
|
||||
return Ok(Box::from(custom_command::CustomCommands {
|
||||
|
Loading…
Reference in New Issue
Block a user