fix: ref, also more cleanup and lints

This commit is contained in:
əlemi 2024-02-15 03:12:00 +01:00
parent 00c4e848e1
commit 003d535c97
Signed by: alemi
GPG key ID: A4895B84D311642C
5 changed files with 23 additions and 24 deletions

View file

@ -19,8 +19,7 @@ pub struct TciConfig {
impl TciConfig {
pub fn load() -> Result<Self, toml::de::Error> {
let config_path = std::env::var("TCI_CONFIG")
.as_deref()
.unwrap_or("/etc/tci/config.toml");
.unwrap_or_else(|_| "/etc/tci/config.toml".into());
let cfg_raw = std::fs::read_to_string(config_path)
.unwrap_or_default();

View file

@ -1,6 +1,6 @@
#[derive(Debug, thiserror::Error)]
pub enum TciError {
pub enum TciErr {
#[error("could not understand file system structure, bailing out: {0}")]
FsError(&'static str),
@ -23,4 +23,4 @@ pub enum TciError {
ConfigError(#[from] toml::de::Error),
}
pub type TciResult<T> = Result<T, TciError>;
pub type TciResult<T> = Result<T, TciErr>;

View file

@ -7,15 +7,18 @@ pub struct TciRepo {
}
impl TciRepo {
pub fn new(updates_raw: &str, home: &str) -> crate::error::TciResult<Self> {
pub fn new(updates_raw: &str) -> crate::error::TciResult<Self> {
let home = std::env::var("HOME")
.unwrap_or_default();
let path = match std::env::var("GIT_DIR") {
Ok(p) => std::path::PathBuf::from(&p),
Err(e) => std::env::current_dir()?,
Err(_) => std::env::current_dir()?,
};
// TODO kind of ew but ehh should do its job
let mut name = path.to_string_lossy()
.replace(home, "");
.replace(&home, "");
if name.starts_with('/') {
name.remove(0);
}

View file

@ -1,6 +1,6 @@
mod error;
mod git;
mod config;
mod cfg;
mod tci;
fn main() {

View file

@ -1,19 +1,15 @@
use std::io::Read;
use crate::{config::{TciConfig, TciScriptConfig}, error::{TciError, TciResult}, git::{RefUpdate, RefUpdateVecHelper, TciRepo}};
use crate::{cfg::{TciConfig, TciScriptConfig}, error::{TciErr, TciResult}, git::{RefUpdateVecHelper, TciRepo}};
pub struct Tci {
cfg: TciConfig,
repo: TciRepo,
home: String,
stdin: String,
// stdin: String,
}
impl Tci {
pub fn new() -> TciResult<Self> {
let home = std::env::var("HOME")
.unwrap_or_default();
let cfg = TciConfig::load()
.unwrap_or_else(|e| {
eprintln!("[!] invalid config: {e}");
@ -28,9 +24,9 @@ impl Tci {
0
});
let repo = TciRepo::new(&stdin, &home)?;
let repo = TciRepo::new(&stdin)?;
Ok(Tci{cfg, repo, home, stdin})
Ok(Tci{cfg, repo})
}
/// check if tci is allowed to run for this repository
@ -59,11 +55,12 @@ impl Tci {
let env = self.prepare_env()?;
let tci_script = self.repo.cfg.get_string("tci.path").unwrap_or(".tci".into());
let tci_path = env.path().join(&tci_script);
let tci_script = self.repo.cfg.get_string("tci.path")
.unwrap_or_else(|_| ".tci".into());
let tci_path = env.path().join(tci_script);
if !tci_path.is_file() && !tci_path.is_dir() {
return Err(TciError::Missing);
return Err(TciErr::Missing);
}
for setup in &self.cfg.setup {
@ -75,7 +72,7 @@ impl Tci {
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)
self.run_tci_hook(env.path(), &tci_path)
} else { Ok(()) }; // will never happen but rustc doesn't know
for cleanup in &self.cfg.cleanup {
@ -98,14 +95,14 @@ impl Tci {
// TODO allow customizing clone? just clone recursive? just let hook setup submodules?
git2::Repository::clone(
self.repo.path.to_str()
.ok_or(TciError::FsError("repo path is not a valid string"))?,
.ok_or(TciErr::FsError("repo path is not a valid string"))?,
tmp.path(),
)?;
Ok(tmp)
}
fn run_tci_hook(&self, cwd: &std::path::Path, dir: std::path::PathBuf) -> TciResult<()> {
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 {
@ -133,8 +130,8 @@ impl Tci {
.code()
{
Some(0) => Ok(()),
Some(x) => Err(crate::error::TciError::SubprocessError(x)),
None => Err(crate::error::TciError::SubprocessTerminated),
Some(x) => Err(crate::error::TciErr::SubprocessError(x)),
None => Err(crate::error::TciErr::SubprocessTerminated),
}
}
}