extract logic from execute

This commit is contained in:
2022-11-27 11:06:24 +01:00
parent 4ec5250a8d
commit a777b1d0a4
16 changed files with 159 additions and 103 deletions

View File

@@ -19,13 +19,13 @@ impl BuilderCapabilities {
#[async_trait]
impl Builder for BuilderCapabilities {
async fn build(&self, action_path: PathBuf, action: Action) -> eyre::Result<DynRunnableBin> {
async fn build(&self, action_path: &PathBuf, action: &Action) -> eyre::Result<DynRunnableBin> {
match action {
Action::Go { entry } => {
let bin = GolangBinBuild::new()
.build(GolangBinBuildOpts {
entry,
src_path: action_path,
entry: entry.clone(),
src_path: action_path.clone(),
})
.await?;

View File

@@ -47,8 +47,12 @@ impl GolangBin {
#[async_trait]
impl RunnableBin for GolangBin {
async fn run(&self, victim_path: PathBuf) -> eyre::Result<()> {
execute_shell(self.path.to_string_lossy().to_string(), Some(victim_path)).await?;
async fn run(&self, victim_path: &PathBuf) -> eyre::Result<()> {
execute_shell(
self.path.to_string_lossy().to_string(),
Some(victim_path.clone()),
)
.await?;
Ok(())
}

View File

@@ -9,14 +9,14 @@ use crate::schema::models::Action;
#[async_trait]
pub trait RunnableBin {
async fn run(&self, victim_path: PathBuf) -> eyre::Result<()>;
async fn run(&self, victim_path: &PathBuf) -> eyre::Result<()>;
}
pub type DynRunnableBin = Arc<dyn RunnableBin + Send + Sync>;
#[async_trait]
pub trait Builder {
async fn build(&self, action_path: PathBuf, action: Action) -> eyre::Result<DynRunnableBin>;
async fn build(&self, action_path: &PathBuf, action: &Action) -> eyre::Result<DynRunnableBin>;
}
pub type DynBuilder = Arc<dyn Builder + Send + Sync>;

View File

@@ -23,19 +23,22 @@ impl DefaultExecutor {
impl Executor for DefaultExecutor {
async fn execute(
&self,
victim_path: PathBuf,
action_path: PathBuf,
action: Action,
victim_path: &PathBuf,
action_path: &PathBuf,
action: &Action,
) -> eyre::Result<()> {
tracing::trace!(
victim_path = victim_path.to_string_lossy().to_string(),
"execute"
);
let bin = self.builder.build(action_path, action.clone()).await?;
let bin = self.builder.build(action_path, action).await?;
match action {
Action::Go { .. } => {
GolangExecutor::new()
.execute(GolangExecutorOpts { bin, victim_path })
.execute(GolangExecutorOpts {
bin,
victim_path: victim_path.clone(),
})
.await?
}
}

View File

@@ -8,9 +8,9 @@ use crate::schema::models::Action;
pub trait Executor {
async fn execute(
&self,
victim_path: PathBuf,
action_path: PathBuf,
action: Action,
victim_path: &PathBuf,
action_path: &PathBuf,
action: &Action,
) -> eyre::Result<()>;
}

View File

@@ -15,7 +15,7 @@ impl GolangExecutor {
}
pub async fn execute(&self, opts: GolangExecutorOpts) -> eyre::Result<()> {
opts.bin.run(opts.victim_path).await?;
opts.bin.run(&opts.victim_path).await?;
Ok(())
}

View File

@@ -29,7 +29,8 @@ impl LocalGitProvider {
#[async_trait::async_trait]
impl GitProvider for LocalGitProvider {
async fn clone_from_url(&self, url: String) -> eyre::Result<(PathBuf, Repository)> {
async fn clone_from_url(&self, url: &String) -> eyre::Result<(PathBuf, Repository)> {
let url = url.clone();
tracing::debug!(url, "allocating dir");
let dir = self.storage_engine.allocate_dir().await?;
let options = self.options.clone();
@@ -65,7 +66,6 @@ impl GitProvider for LocalGitProvider {
builder.fetch_options(fo).with_checkout(checkout_builder);
tracing::debug!(
url,
path = dirpath.as_os_str().to_string_lossy().to_string(),
"clone git repo"
);

View File

@@ -24,7 +24,7 @@ pub type DynGiteaClient = Arc<dyn GiteaClient + Send + Sync>;
#[async_trait]
pub trait GiteaProvider {
async fn clone_from_qualified(&self, repo: String) -> eyre::Result<(PathBuf, Repository)>;
async fn clone_from_qualified(&self, repo: &String) -> eyre::Result<(PathBuf, Repository)>;
async fn create_branch(
&self,
repo: Arc<Mutex<Repository>>,

View File

@@ -30,7 +30,7 @@ impl DefaultGiteaProvider {
#[async_trait]
impl GiteaProvider for DefaultGiteaProvider {
async fn clone_from_qualified(&self, repo: String) -> eyre::Result<(PathBuf, Repository)> {
async fn clone_from_qualified(&self, repo: &String) -> eyre::Result<(PathBuf, Repository)> {
let (owner, repo_name) = repo
.split_once("/")
.ok_or(eyre::anyhow!("repo is not a valid format"))?;
@@ -40,7 +40,7 @@ impl GiteaProvider for DefaultGiteaProvider {
.get_clone_url(owner.into(), repo_name.into())
.await?;
let (path, repo) = self.git_provider.clone_from_url(clone_url).await?;
let (path, repo) = self.git_provider.clone_from_url(&clone_url).await?;
Ok((path, repo))
}

View File

@@ -4,14 +4,12 @@ use async_trait::async_trait;
use git2::Repository;
use tokio::sync::Mutex;
use crate::schema::models::GitPushBranch;
pub mod git;
pub mod gitea;
#[async_trait]
pub trait GitProvider {
async fn clone_from_url(&self, url: String) -> eyre::Result<(PathBuf, Repository)>;
async fn clone_from_url(&self, url: &String) -> eyre::Result<(PathBuf, Repository)>;
async fn create_branch(
&self,
repo: Arc<Mutex<Repository>>,

View File

@@ -2,5 +2,6 @@ pub mod builder;
pub mod executor;
pub mod git;
pub mod schema;
pub mod selectors;
mod shell;
pub mod storage;

View File

@@ -0,0 +1,48 @@
use std::{path::PathBuf, sync::Arc};
use tokio::sync::Mutex;
use crate::{
executor::executor::DynExecutor,
git::DynGitProvider,
schema::models::{Action, Git},
};
pub struct GitSelector {
git_provider: DynGitProvider,
executor: DynExecutor,
}
impl GitSelector {
pub fn new(git_provider: DynGitProvider, executor: DynExecutor) -> Self {
Self {
git_provider,
executor,
}
}
pub async fn run(&self, git: &Git, action_path: &PathBuf, action: &Action) -> eyre::Result<()> {
tracing::info!("fetching repos");
for repo in &git.repositories {
let gp = self.git_provider.clone();
let (path, repo) = gp.clone_from_url(repo).await?;
let repo = Arc::new(Mutex::new(repo));
if let Some(push) = &git.push {
self.git_provider
.create_branch(repo.clone(), &push.branch.name)
.await?;
}
self.executor.execute(&path, action_path, action).await?;
if let Some(push) = &git.push {
self.git_provider
.push_branch(repo, &push.branch.name)
.await?;
}
}
Ok(())
}
}

View File

@@ -0,0 +1,59 @@
use std::{path::PathBuf, sync::Arc};
use tokio::sync::Mutex;
use crate::{
executor::executor::DynExecutor,
git::{gitea::DynGiteaProvider, DynGitProvider},
schema::models::{Action, Gitea},
};
pub struct GiteaSelector {
gitea_provider: DynGiteaProvider,
git_provider: DynGitProvider,
executor: DynExecutor,
}
impl GiteaSelector {
pub fn new(
gitea_provider: DynGiteaProvider,
git_provider: DynGitProvider,
executor: DynExecutor,
) -> Self {
Self {
gitea_provider,
git_provider,
executor,
}
}
pub async fn run(
&self,
git: &Gitea,
action_path: &PathBuf,
action: &Action,
) -> eyre::Result<()> {
tracing::info!("fetching repos");
for repo in &git.repositories {
let gp = self.gitea_provider.clone();
let (path, repo) = gp.clone_from_qualified(repo).await?;
let repo = Arc::new(Mutex::new(repo));
if let Some(push) = &git.push {
self.git_provider
.create_branch(repo.clone(), &push.pull_request.name)
.await?;
}
self.executor.execute(&path, action_path, action).await?;
if let Some(push) = &git.push {
self.git_provider
.push_branch(repo, &push.pull_request.name)
.await?;
}
}
Ok(())
}
}

View File

@@ -0,0 +1,3 @@
pub mod git_selector;
pub mod gitea_selector;