with executor

This commit is contained in:
Kasper Juul Hermansen 2022-11-22 23:35:52 +01:00
parent 367fa16fd7
commit 18ec10914b
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
7 changed files with 20 additions and 28 deletions

BIN
_examples/actions/write_a_readme/dist/bin vendored Executable file

Binary file not shown.

View File

@ -6,6 +6,8 @@ func main() {
_, err := script. _, err := script.
Echo("# Readme"). Echo("# Readme").
WriteFile("README.md") WriteFile("README.md")
println("i did something!")
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -2,10 +2,7 @@ use std::path::PathBuf;
use async_trait::async_trait; use async_trait::async_trait;
use crate::{ use crate::{builder::RunnableBin, shell::execute_shell};
builder::RunnableBin,
shell::{execute_shell, print_res},
};
pub struct GolangBinBuildOpts { pub struct GolangBinBuildOpts {
pub entry: String, pub entry: String,
@ -26,14 +23,15 @@ impl GolangBinBuild {
"build golang_bin" "build golang_bin"
); );
let res = execute_shell( execute_shell(
format!("go build {}", opts.entry), format!("go build -o dist/bin {}", opts.entry),
Some(opts.src_path.clone()), Some(opts.src_path.clone()),
) )
.await?; .await?;
print_res("golang_bin".into(), res);
Ok(GolangBin::new(opts.src_path)) let abs_path = std::fs::canonicalize(opts.src_path.join("dist/bin"))?;
Ok(GolangBin::new(abs_path))
} }
} }
@ -49,7 +47,9 @@ impl GolangBin {
#[async_trait] #[async_trait]
impl RunnableBin for GolangBin { impl RunnableBin for GolangBin {
async fn run(&self) -> eyre::Result<()> { async fn run(&self, victim_path: PathBuf) -> eyre::Result<()> {
todo!("not implemented") execute_shell(self.path.to_string_lossy().to_string(), Some(victim_path)).await?;
Ok(())
} }
} }

View File

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

View File

@ -31,11 +31,7 @@ impl Executor for DefaultExecutor {
match action { match action {
Action::Go { entry } => { Action::Go { entry } => {
GolangExecutor::new() GolangExecutor::new()
.execute(GolangExecutorOpts { .execute(GolangExecutorOpts { bin, victim_path })
bin,
entry,
src_path: victim_path,
})
.await? .await?
} }
} }

View File

@ -4,8 +4,7 @@ use crate::builder::DynRunnableBin;
pub struct GolangExecutorOpts { pub struct GolangExecutorOpts {
pub bin: DynRunnableBin, pub bin: DynRunnableBin,
pub entry: String, pub victim_path: PathBuf,
pub src_path: PathBuf,
} }
pub struct GolangExecutor; pub struct GolangExecutor;
@ -16,6 +15,8 @@ impl GolangExecutor {
} }
pub async fn execute(&self, opts: GolangExecutorOpts) -> eyre::Result<()> { pub async fn execute(&self, opts: GolangExecutorOpts) -> eyre::Result<()> {
opts.bin.run(opts.victim_path).await?;
Ok(()) Ok(())
} }
} }

View File

@ -3,7 +3,7 @@ use std::{path::PathBuf, process::Stdio};
use eyre::Context; use eyre::Context;
use tokio::io::{AsyncBufReadExt, BufReader}; use tokio::io::{AsyncBufReadExt, BufReader};
pub async fn execute_shell(cmd: String, path: Option<PathBuf>) -> eyre::Result<Vec<String>> { pub async fn execute_shell(cmd: String, path: Option<PathBuf>) -> eyre::Result<()> {
let mut command = tokio::process::Command::new("sh"); let mut command = tokio::process::Command::new("sh");
let command = command.arg("-c"); let command = command.arg("-c");
@ -42,16 +42,9 @@ pub async fn execute_shell(cmd: String, path: Option<PathBuf>) -> eyre::Result<V
} }
}); });
let mut lines: Vec<String> = Vec::new();
while let Some(line) = reader.next_line().await? { while let Some(line) = reader.next_line().await? {
lines.push(line) tracing::trace!("{}", line)
} }
Ok(lines) Ok(())
}
pub fn print_res(scope: String, res: Vec<String>) {
for r in res {
tracing::debug!("{}: {}", scope, r);
}
} }