refactor: move git into own file

This commit is contained in:
Kasper Juul Hermansen 2025-02-28 23:12:56 +01:00
parent dfe2ac62e3
commit ffb81a5ee6
Signed by: kjuulh
SSH Key Fingerprint: SHA256:RjXh0p7U6opxnfd3ga/Y9TCo18FYlHFdSpRIV72S/QM
2 changed files with 54 additions and 59 deletions

View File

@ -4,66 +4,8 @@ use anyhow::Context;
use crate::model::Project;
pub mod git;
pub mod local;
pub mod git {
use std::{
env::temp_dir,
path::{Path, PathBuf},
};
use super::local;
pub async fn reconcile(
url: &str,
path: &Option<PathBuf>,
plan_dir: &Path,
) -> anyhow::Result<()> {
let temp = TempDir::new();
tokio::fs::create_dir_all(&temp.0).await?;
let mut cmd = tokio::process::Command::new("git");
cmd.args(["clone", url, &temp.0.display().to_string(), "--depth=1"]);
tracing::info!("cloning plan: {}", url);
let out = cmd.output().await?;
if !out.status.success() {
let stdout = std::str::from_utf8(&out.stdout).unwrap_or_default();
let stderr = std::str::from_utf8(&out.stderr).unwrap_or_default();
anyhow::bail!("failed to process git plan: {}, {}", stdout, stderr)
}
let temp_plan_dir = if let Some(path) = path {
temp.0.join(path)
} else {
temp.0.to_path_buf()
};
local::reconcile(&temp_plan_dir, plan_dir).await?;
drop(temp);
Ok(())
}
struct TempDir(PathBuf);
impl TempDir {
fn new() -> Self {
Self(
temp_dir()
.join("forest")
.join(uuid::Uuid::new_v4().to_string()),
)
}
}
impl Drop for TempDir {
fn drop(&mut self) {
std::fs::remove_dir_all(&self.0).expect("to be able to remove temp dir");
}
}
}
#[derive(Default)]
pub struct PlanReconciler {}

View File

@ -0,0 +1,53 @@
use std::{
env::temp_dir,
path::{Path, PathBuf},
};
use super::local;
pub async fn reconcile(url: &str, path: &Option<PathBuf>, plan_dir: &Path) -> anyhow::Result<()> {
let temp = TempDir::new();
tokio::fs::create_dir_all(&temp.0).await?;
let mut cmd = tokio::process::Command::new("git");
cmd.args(["clone", url, &temp.0.display().to_string(), "--depth=1"]);
tracing::info!("cloning plan: {}", url);
let out = cmd.output().await?;
if !out.status.success() {
let stdout = std::str::from_utf8(&out.stdout).unwrap_or_default();
let stderr = std::str::from_utf8(&out.stderr).unwrap_or_default();
anyhow::bail!("failed to process git plan: {}, {}", stdout, stderr)
}
let temp_plan_dir = if let Some(path) = path {
temp.0.join(path)
} else {
temp.0.to_path_buf()
};
local::reconcile(&temp_plan_dir, plan_dir).await?;
drop(temp);
Ok(())
}
struct TempDir(PathBuf);
impl TempDir {
fn new() -> Self {
Self(
temp_dir()
.join("forest")
.join(uuid::Uuid::new_v4().to_string()),
)
}
}
impl Drop for TempDir {
fn drop(&mut self) {
std::fs::remove_dir_all(&self.0).expect("to be able to remove temp dir");
}
}