Rewrite rust (#38)
Co-authored-by: kjuulh <contact@kjuulh.io> Reviewed-on: https://git.front.kjuulh.io/kjuulh/octopush/pulls/38
This commit is contained in:
36
crates/octopush_core/src/builder/builder_capabilities.rs
Normal file
36
crates/octopush_core/src/builder/builder_capabilities.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use std::{path::PathBuf, sync::Arc};
|
||||
|
||||
use async_trait::async_trait;
|
||||
|
||||
use crate::schema::models::Action;
|
||||
|
||||
use super::{
|
||||
builders::golang_bin::{GolangBinBuild, GolangBinBuildOpts},
|
||||
Builder, DynRunnableBin,
|
||||
};
|
||||
|
||||
pub struct BuilderCapabilities;
|
||||
|
||||
impl BuilderCapabilities {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Builder for BuilderCapabilities {
|
||||
async fn build(&self, action_path: &PathBuf, action: &Action) -> eyre::Result<DynRunnableBin> {
|
||||
match action {
|
||||
Action::Go { entry } => {
|
||||
let bin = GolangBinBuild::new()
|
||||
.build(GolangBinBuildOpts {
|
||||
entry: entry.clone(),
|
||||
src_path: action_path.clone(),
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(Arc::new(bin))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
59
crates/octopush_core/src/builder/builders/golang_bin.rs
Normal file
59
crates/octopush_core/src/builder/builders/golang_bin.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use async_trait::async_trait;
|
||||
|
||||
use crate::{builder::RunnableBin, shell::execute_shell};
|
||||
|
||||
pub struct GolangBinBuildOpts {
|
||||
pub entry: String,
|
||||
pub src_path: PathBuf,
|
||||
}
|
||||
|
||||
pub struct GolangBinBuild;
|
||||
|
||||
impl GolangBinBuild {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
|
||||
pub async fn build(&self, opts: GolangBinBuildOpts) -> eyre::Result<GolangBin> {
|
||||
tracing::trace!(
|
||||
src = opts.src_path.to_string_lossy().to_string(),
|
||||
entry = opts.entry,
|
||||
"build golang_bin"
|
||||
);
|
||||
|
||||
execute_shell(
|
||||
format!("go build -o dist/bin {}", opts.entry),
|
||||
Some(opts.src_path.clone()),
|
||||
)
|
||||
.await?;
|
||||
|
||||
let abs_path = std::fs::canonicalize(opts.src_path.join("dist/bin"))?;
|
||||
|
||||
Ok(GolangBin::new(abs_path))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GolangBin {
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
impl GolangBin {
|
||||
fn new(path: PathBuf) -> Self {
|
||||
Self { path }
|
||||
}
|
||||
}
|
||||
|
||||
#[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.clone()),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
1
crates/octopush_core/src/builder/builders/mod.rs
Normal file
1
crates/octopush_core/src/builder/builders/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod golang_bin;
|
22
crates/octopush_core/src/builder/mod.rs
Normal file
22
crates/octopush_core/src/builder/mod.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
pub mod builder_capabilities;
|
||||
mod builders;
|
||||
|
||||
use std::{path::PathBuf, sync::Arc};
|
||||
|
||||
use async_trait::async_trait;
|
||||
|
||||
use crate::schema::models::Action;
|
||||
|
||||
#[async_trait]
|
||||
pub trait RunnableBin {
|
||||
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>;
|
||||
}
|
||||
|
||||
pub type DynBuilder = Arc<dyn Builder + Send + Sync>;
|
Reference in New Issue
Block a user