feat: scripts in dir run in order

This commit is contained in:
əlemi 2024-02-13 23:10:40 +01:00
parent 2f19906f0c
commit 97dc8a0aee
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -118,33 +118,41 @@ fn tci_hook(repo_path: &std::path::PathBuf, tci_script: &str) -> Result<(), TciE
let tci_path = tmp.path().join(tci_script);
// if .tci is one script file, just run it
if tci_path.is_file() {
println!("[=] running tci script for repo '{name}'");
std::env::set_current_dir(tmp.path())?;
let res = shell_out(tmp.path().join(tci_script));
std::env::set_current_dir(repo_path)?;
res
// if .tci is a directory of scripts, run all of them sequentially
} else if tci_path.is_dir() {
let mut scripts : Vec<String> = std::fs::read_dir(&tci_path)?
.filter_map(|x| Some(x.ok()?.file_name().to_string_lossy().to_string()))
.collect();
scripts.sort(); // so that we get reliable execution order
std::env::set_current_dir(tmp.path())?;
for script in std::fs::read_dir(&tci_path)? {
match script {
Err(e) => eprintln!("[?] error while listing .tci dir: {e}"),
Ok(s) => {
println!("[=] running tci script '{:?}' for repo '{name}'", s.file_name());
let p = tci_path.clone();
let res = shell_out(p.join(s.path()));
std::env::set_current_dir(repo_path)?;
for script in scripts {
println!("[=] running tci script '{script}' for repo '{name}'");
let res = shell_out(tci_path.clone().join(&script));
if let Err(e) = res {
eprintln!("[!] error executing script '{:?}': {e}", s.file_name());
eprintln!("[!] error executing script '{script}': {e}");
std::env::set_current_dir(repo_path)?;
return Err(e);
}
}
}
}
Ok(())
} else {
return Err(TciError::MissingScript);
}
std::env::set_current_dir(repo_path)?;
Ok(())
// return error in any other case
} else {
return Err(TciError::MissingScript);
}
}