Added initial git and storage engine

This commit is contained in:
2022-10-25 22:00:46 +02:00
parent 1be156d911
commit dce155979e
16 changed files with 931 additions and 12 deletions

View File

@@ -0,0 +1,14 @@
[package]
name = "octopush_core"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
async-trait = { workspace = true }
eyre = { workspace = true }
tokio = { workspace = true }
rand = "0.8.5"
hex = "0.4.3"
git2 = "0.15.0"

View File

@@ -0,0 +1,25 @@
use crate::storage::DynStorageEngine;
use super::GitProvider;
pub struct GitHubGitProvider {
storage_engine: DynStorageEngine,
}
impl GitHubGitProvider {
pub fn new(storage_engine: DynStorageEngine) -> Self {
Self { storage_engine }
}
}
#[async_trait::async_trait]
impl GitProvider for GitHubGitProvider {
async fn clone_from_url(&self, url: String) -> eyre::Result<()> {
let dir = self.storage_engine.allocate_dir().await?;
tokio::task::spawn_blocking(move || git2::Repository::clone(url.as_str(), dir.path()))
.await??;
Ok(())
}
}

View File

@@ -0,0 +1,9 @@
use std::sync::Arc;
pub mod github;
#[async_trait::async_trait]
pub trait GitProvider {
async fn clone_from_url(&self, url: String) -> eyre::Result<()>;
}
pub type DynGitProvider = Arc<dyn GitProvider + Send + Sync>;

View File

@@ -0,0 +1,2 @@
pub mod git;
pub mod storage;

View File

@@ -0,0 +1,51 @@
use std::path::PathBuf;
use rand::distributions::{DistString, Standard};
use super::StorageEngine;
pub struct LocalStorageEngine {
root: PathBuf,
}
impl LocalStorageEngine {
pub fn new(root: PathBuf) -> Self {
Self { root }
}
}
#[async_trait::async_trait]
impl StorageEngine for LocalStorageEngine {
async fn allocate_dir(&self) -> eyre::Result<super::TemporaryDir> {
let subdir_name = Standard.sample_string(&mut rand::thread_rng(), 2);
let mut path = self.root.clone();
path.push(hex::encode(subdir_name));
Ok(super::TemporaryDir::new(path))
}
async fn cleanup(&self) -> eyre::Result<()> {
tokio::fs::remove_dir_all(self.root.clone()).await?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use crate::storage::StorageEngine;
use super::LocalStorageEngine;
#[tokio::test]
async fn create_local_storage_engine_and_allocate() {
let local_storage = LocalStorageEngine::new(PathBuf::new());
let dir = local_storage.allocate_dir().await.expect("to allocate dir");
assert_eq!(dir.path().to_string_lossy().len(), 16);
assert_eq!(dir.path().to_string_lossy().is_empty(), false);
}
}

View File

@@ -0,0 +1,31 @@
pub mod local;
use std::{path::PathBuf, sync::Arc};
use async_trait::async_trait;
#[async_trait]
pub trait StorageEngine {
async fn allocate_dir(&self) -> eyre::Result<TemporaryDir>;
async fn cleanup(&self) -> eyre::Result<()>;
}
pub type DynStorageEngine = Arc<dyn StorageEngine + Send + Sync>;
pub struct TemporaryDir {
path: PathBuf,
}
impl TemporaryDir {
pub fn new(path: PathBuf) -> Self {
Self { path }
}
pub fn path(&self) -> PathBuf {
self.path.clone()
}
pub fn cleanup(self) -> eyre::Result<()> {
Ok(())
}
}