feat: made it way simpler

no .tci dir! no .tci.toml! just one tci script, keep your scripts in
your repo and start them from .tci with bash tasks, instead of
reinventing the wheel with configs and conditions i should pre-parse as
many info as possible and provide to the underlying .tci script
This commit is contained in:
əlemi 2024-02-15 04:12:41 +01:00
parent a2e0a10ded
commit 7e1fd5fe3e
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 19 additions and 78 deletions

View file

@ -8,12 +8,6 @@ pub struct TciConfig {
#[serde(default)]
pub hooks: Vec<String>,
#[serde(default)]
pub setup: Vec<String>,
#[serde(default)]
pub cleanup: Vec<String>,
}
impl TciConfig {
@ -27,27 +21,3 @@ impl TciConfig {
toml::from_str(&cfg_raw)
}
}
#[derive(Debug, Default, serde::Deserialize)]
pub struct TciScriptConfig {
#[serde(default)]
pub branch: Option<String>,
#[serde(default)]
pub filter: Option<String>,
#[serde(default)]
pub scripts: Vec<String>,
// #[serde(default)]
// concurrent: bool,
}
impl TciScriptConfig {
pub fn load(path: &std::path::PathBuf) -> Result<Self, toml::de::Error> {
let cfg_raw = std::fs::read_to_string(path)
.unwrap_or_default();
toml::from_str(&cfg_raw)
}
}

View file

@ -1,6 +1,6 @@
use std::io::Read;
use crate::{cfg::{TciConfig, TciScriptConfig}, error::{TciErr, TciResult}, git::{RefUpdateVecHelper, TciRepo}};
use crate::{cfg::TciConfig, error::{TciErr, TciResult}, git::{RefUpdateVecHelper, TciRepo}};
pub struct Tci {
cfg: TciConfig,
@ -35,15 +35,13 @@ impl Tci {
return false; // we are in whitelist mode and this repo is not whitelisted
}
if let Some(ref branch) = self.cfg.branch {
if !self.repo.updates.contains_branch(branch) {
return false; // tci is not allowed to run on changes for this branch
match self.cfg.branch.as_deref() {
None => self.repo.updates.contains_branch(".tci"),
Some("") => true,
Some(b) => self.repo.updates.contains_branch(b),
}
}
true
}
pub fn run(&self) -> TciResult<()> {
// run hooks
for hook in &self.cfg.hooks {
@ -55,33 +53,16 @@ impl Tci {
let env = self.prepare_env()?;
let tci_script = self.repo.cfg.get_string("tci.path")
let tci_script = self.repo.cfg.get_string("tci.script")
.unwrap_or_else(|_| ".tci".into());
let tci_path = env.path().join(tci_script);
if !tci_path.is_file() && !tci_path.is_dir() {
if !tci_path.is_file() {
return Err(TciErr::Missing);
}
for setup in &self.cfg.setup {
println!("[+] setting up ({setup})");
Tci::exec(&self.repo.path, setup)?;
}
let res = if tci_path.is_file() {
println!("[=] running tci script for {}", self.repo.name);
Tci::exec(env.path(), tci_path)
} else if tci_path.is_dir() {
self.run_tci_hook(env.path(), &tci_path)
} else { Ok(()) }; // will never happen but rustc doesn't know
for cleanup in &self.cfg.cleanup {
println!("[-] cleaning up ({cleanup}) ");
Tci::exec(&self.repo.path, cleanup)?;
}
res?; // if there was an error, return it now
Tci::exec(env.path(), tci_path)?;
println!("[o] tci complete");
Ok(())
@ -102,26 +83,6 @@ impl Tci {
Ok(tmp)
}
fn run_tci_hook(&self, cwd: &std::path::Path, dir: &std::path::Path) -> TciResult<()> {
let cfg = TciScriptConfig::load(&dir.join("config.toml"))?;
if let Some(branch) = cfg.branch {
if !self.repo.updates.contains_branch(&branch) {
return Ok(()); // tci is not configured to run on this branch
}
}
// TODO calculate which files changed and decide if we should
// run depending on cfg.filter (regex match idk?)
for script in cfg.scripts {
println!("[=] running tci script '{script}' for {}", self.repo.name);
Tci::exec(cwd, dir.join(script))?;
}
Ok(())
}
fn exec<S : AsRef<std::ffi::OsStr>>(cwd: &std::path::Path, s: S) -> TciResult<()> {
match std::process::Command::new(s)
.current_dir(cwd)
@ -134,5 +95,15 @@ impl Tci {
None => Err(crate::error::TciErr::SubprocessTerminated),
}
}
#[allow(unused)]
fn spawn<S : AsRef<std::ffi::OsStr>>(cwd: &std::path::Path, s: S) -> TciResult<std::process::Child> {
Ok(
std::process::Command::new(s)
.current_dir(cwd)
// .stdin(self.stdin.clone()) // TODO not this easy
.spawn()?
)
}
}