Init commit

This commit is contained in:
2024-01-10 12:33:05 +01:00
commit 09f81a5899
32 changed files with 2737 additions and 0 deletions

View File

@ -0,0 +1,33 @@
use crate::helpers::cli::cli_exec_from_dir;
use super::Target;
pub(crate) struct CustomCommands {
pub(crate) package: Vec<String>,
pub(crate) upload: Vec<String>,
}
impl Target for CustomCommands {
fn push(
&self,
workdir_path: String,
chart_local: crate::source::ChartLocal,
dry_run: bool,
) -> Result<(), Box<dyn std::error::Error>> {
for cmd_tmpl in self.package.clone() {
let mut reg = super::register_handlebars();
reg.register_template_string("cmd", cmd_tmpl)?;
let cmd = reg.render("cmd", &chart_local)?;
cli_exec_from_dir(cmd, workdir_path.clone())?;
}
if !dry_run {
for cmd_tmpl in self.upload.clone() {
let mut reg = super::register_handlebars();
reg.register_template_string("cmd", cmd_tmpl)?;
let cmd = reg.render("cmd", &chart_local)?;
cli_exec_from_dir(cmd, workdir_path.clone())?;
}
}
Ok(())
}
}

60
src/mirror/git.rs Normal file
View File

@ -0,0 +1,60 @@
use crate::{helpers::cli::cli_exec_from_dir, source::ChartLocal};
use dircpy::*;
use super::Target;
pub(crate) struct Git {
pub(crate) git_dir: String,
pub(crate) url: String,
pub(crate) path: String,
pub(crate) branch: String,
pub(crate) commit: Option<String>,
}
impl Target for Git {
fn push(
&self,
workdir_path: String,
chart_local: ChartLocal,
dry_run: bool,
) -> Result<(), Box<dyn std::error::Error>> {
let cmd = format!("git clone {} {}", self.url, self.git_dir);
cli_exec_from_dir(cmd, workdir_path.clone())?;
let git_repo_path = format!("{}/{}", workdir_path, self.git_dir);
// Prepare branch
let mut reg = super::register_handlebars();
reg.register_template_string("branch", self.branch.clone())?;
let branch = reg.render("branch", &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())?;
};
// Prepare path
reg.register_template_string("path", self.path.clone())?;
let path = reg.render("path", &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)
.run()?;
// Prepare the commit message
let commit_message = match self.commit.clone() {
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 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);
cli_exec_from_dir(cmd, repo_local_full_path)?;
}
Ok(())
}
}

53
src/mirror/mod.rs Normal file
View File

@ -0,0 +1,53 @@
use chrono::prelude::*;
use handlebars::{handlebars_helper, Handlebars};
use time::{format_description::parse, OffsetDateTime};
use crate::{config::Mirror, source::ChartLocal};
pub(crate) mod custom_command;
pub(crate) mod git;
pub(crate) trait Target {
fn push(
&self,
workdir_path: String,
chart_local: ChartLocal,
dry_run: bool,
) -> Result<(), Box<dyn std::error::Error>>;
}
pub(crate) fn mirror_from_mirror_obj(
mirror: Mirror,
) -> Result<Box<dyn Target>, Box<dyn std::error::Error>> {
if let Some(git) = mirror.git {
return Ok(Box::from(git::Git {
git_dir: mirror.name.clone(),
url: git.url,
path: match git.path {
Some(path) => path,
None => "".to_string(),
},
branch: git.branch,
commit: git.commit,
}));
} else if let Some(command) = mirror.custom_command {
return Ok(Box::from(custom_command::CustomCommands {
package: command.package,
upload: command.upload,
}));
}
Err(Box::from(format!(
"a kind is unknown for the mirror {}",
mirror.name
)))
}
handlebars_helper!(date_helper: | | Utc::now().format("%Y-%m-%d").to_string());
handlebars_helper!(time_helper: | | Utc::now().format("%H-%M-%S").to_string());
pub(crate) fn register_handlebars() -> Handlebars<'static> {
let mut handlebars = Handlebars::new();
handlebars.register_helper("date", Box::new(date_helper));
handlebars.register_helper("time", Box::new(time_helper));
handlebars
}