Added initial git and storage engine
This commit is contained in:
14
crates/octopush_core/Cargo.toml
Normal file
14
crates/octopush_core/Cargo.toml
Normal 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"
|
25
crates/octopush_core/src/git/github.rs
Normal file
25
crates/octopush_core/src/git/github.rs
Normal 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(())
|
||||
}
|
||||
}
|
9
crates/octopush_core/src/git/mod.rs
Normal file
9
crates/octopush_core/src/git/mod.rs
Normal 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>;
|
2
crates/octopush_core/src/lib.rs
Normal file
2
crates/octopush_core/src/lib.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod git;
|
||||
pub mod storage;
|
51
crates/octopush_core/src/storage/local.rs
Normal file
51
crates/octopush_core/src/storage/local.rs
Normal 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);
|
||||
}
|
||||
}
|
31
crates/octopush_core/src/storage/mod.rs
Normal file
31
crates/octopush_core/src/storage/mod.rs
Normal 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(())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user