From 003d535c97cfc14dc374ddb00d9ff6837fd130cb Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 15 Feb 2024 03:12:00 +0100 Subject: [PATCH] fix: ref, also more cleanup and lints --- src/{config.rs => cfg.rs} | 3 +-- src/error.rs | 4 ++-- src/git.rs | 9 ++++++--- src/main.rs | 2 +- src/tci.rs | 29 +++++++++++++---------------- 5 files changed, 23 insertions(+), 24 deletions(-) rename src/{config.rs => cfg.rs} (94%) diff --git a/src/config.rs b/src/cfg.rs similarity index 94% rename from src/config.rs rename to src/cfg.rs index 7cb6fb9..dea9a96 100644 --- a/src/config.rs +++ b/src/cfg.rs @@ -19,8 +19,7 @@ pub struct TciConfig { impl TciConfig { pub fn load() -> Result { 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(); diff --git a/src/error.rs b/src/error.rs index eec0c3c..d38af28 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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 = Result; +pub type TciResult = Result; diff --git a/src/git.rs b/src/git.rs index d092347..15fadb1 100644 --- a/src/git.rs +++ b/src/git.rs @@ -7,15 +7,18 @@ pub struct TciRepo { } impl TciRepo { - pub fn new(updates_raw: &str, home: &str) -> crate::error::TciResult { + pub fn new(updates_raw: &str) -> crate::error::TciResult { + 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); } diff --git a/src/main.rs b/src/main.rs index 0a3f56e..b1326a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ mod error; mod git; -mod config; +mod cfg; mod tci; fn main() { diff --git a/src/tci.rs b/src/tci.rs index 0dad902..6e8ad05 100644 --- a/src/tci.rs +++ b/src/tci.rs @@ -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 { - 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), } } }