diff --git a/src/config/mod.rs b/src/config/mod.rs index 53d1ed8..6f380f9 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -61,6 +61,8 @@ pub(crate) struct GitMirror { pub(crate) branch: String, pub(crate) commit: Option, pub(crate) git_dir: Option, + pub(crate) rebase: Option, + pub(crate) default_branch: Option, } #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)] diff --git a/src/helpers/mod.rs b/src/helpers/mod.rs index 645c2d0..a79403c 100644 --- a/src/helpers/mod.rs +++ b/src/helpers/mod.rs @@ -1,2 +1,3 @@ pub(crate) mod cli; pub(crate) mod copy; +pub(crate) mod template; diff --git a/src/template/mod.rs b/src/helpers/template.rs similarity index 79% rename from src/template/mod.rs rename to src/helpers/template.rs index 843ee9f..c44d130 100644 --- a/src/template/mod.rs +++ b/src/helpers/template.rs @@ -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(string: String, data: &T) -> Result> + 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; diff --git a/src/main.rs b/src/main.rs index 1b77519..7623044 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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}; diff --git a/src/mirror/custom_command.rs b/src/mirror/custom_command.rs index 4e071a2..68399c1 100644 --- a/src/mirror/custom_command.rs +++ b/src/mirror/custom_command.rs @@ -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, diff --git a/src/mirror/git.rs b/src/mirror/git.rs index 5b0a40d..b9ab05f 100644 --- a/src/mirror/git.rs +++ b/src/mirror/git.rs @@ -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, + pub(crate) default_branch: Option, + pub(crate) rebase: bool, } impl Target for Git { @@ -18,29 +20,33 @@ impl Target for Git { chart_local: ChartLocal, dry_run: bool, ) -> Result<(), Box> { - 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(()) diff --git a/src/mirror/mod.rs b/src/mirror/mod.rs index b2826fb..b1f0f0b 100644 --- a/src/mirror/mod.rs +++ b/src/mirror/mod.rs @@ -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 {