fix: small refactor, show full repo path

This commit is contained in:
əlemi 2024-02-13 21:21:17 +01:00
parent 4a1fef20fd
commit 83d4b3cbbc
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -29,6 +29,8 @@ struct TciConfig {
cleanup: Vec<String>,
}
const HOME : &str = &std::env::var("HOME").unwrap_or_default();
fn main() {
// load tci config
// TODO how could we receive this?
@ -74,25 +76,17 @@ fn tci(cfg: TciConfig) -> Result<(), Box<dyn std::error::Error>> {
return Ok(()); // we are in whitelist mode and this repo is not whitelisted
}
// run setup hooks, if provided
// run hooks
for setup in cfg.setup {
print!("[+] setting up ('{setup}')");
let res = shell(setup)?;
if !res.is_empty() {
print!(": {res}");
}
println!();
print!("[+] setting up ({setup})");
println!("{}", shell(setup)?);
}
let res = tci_hook(&repo_path, &tci_script);
for cleanup in cfg.cleanup {
print!("[-] cleaning up ('{cleanup}')");
let res = shell(cleanup)?;
if !res.is_empty() {
print!(": {res}");
}
println!();
print!("[-] cleaning up ({cleanup}) ");
println!("{}", shell(cleanup)?);
}
res?; // only check error AFTER running cleanup hooks
@ -103,13 +97,17 @@ fn tci(cfg: TciConfig) -> Result<(), Box<dyn std::error::Error>> {
}
fn tci_hook(repo_path: &PathBuf, tci_script: &str) -> Result<(), TciError> {
let repo_name = repo_path
.parent().ok_or(TciError::FsError("missing .git parent"))?
.file_name().ok_or(TciError::FsError("no file name for .git parent"))?
.to_str().ok_or(TciError::FsError(".git parent file name is not a valid string"))?
.to_string();
// TODO kind of ew but ehh should do its job
let mut name = repo_path.to_string_lossy()
.replace(HOME, "");
if name.starts_with('/') {
name.remove(0);
}
if name.ends_with('/') {
name.remove(name.chars().count());
}
let tmp = tempdir::TempDir::new(&format!("tci-{repo_name}"))?;
let tmp = tempdir::TempDir::new(&format!("tci-{name}"))?;
// TODO allow customizing clone? just clone recursive? just let hook setup submodules?
Repository::clone(
@ -124,9 +122,9 @@ fn tci_hook(repo_path: &PathBuf, tci_script: &str) -> Result<(), TciError> {
std::env::set_current_dir(tmp.path())?;
println!("[o] running tci script for repo '{repo_name}'");
println!("[=] running tci script for repo '{name}'");
let res = shell(tmp.path().join(tci_script))?;
println!("{res}");
println!("[:] {}", res.replace("\n", "\n[:]"));
std::env::set_current_dir(repo_path)?;