Rewrite rust #38

Merged
kjuulh merged 24 commits from experiment/local into v0.3 2022-11-27 12:21:36 +01:00
7 changed files with 20 additions and 28 deletions
Showing only changes of commit 18ec10914b - Show all commits

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.
Echo("# Readme").
WriteFile("README.md")
println("i did something!")
if err != nil {
panic(err)
}

View File

@ -2,10 +2,7 @@ use std::path::PathBuf;
use async_trait::async_trait;
use crate::{
builder::RunnableBin,
shell::{execute_shell, print_res},
};
use crate::{builder::RunnableBin, shell::execute_shell};
pub struct GolangBinBuildOpts {
pub entry: String,
@ -26,14 +23,15 @@ impl GolangBinBuild {
"build golang_bin"
);
let res = execute_shell(
format!("go build {}", opts.entry),
execute_shell(
format!("go build -o dist/bin {}", opts.entry),
Some(opts.src_path.clone()),
)
.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]
impl RunnableBin for GolangBin {
async fn run(&self) -> eyre::Result<()> {
todo!("not implemented")
async fn run(&self, victim_path: PathBuf) -> eyre::Result<()> {
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]
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>;

View File

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

View File

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

View File

@ -3,7 +3,7 @@ use std::{path::PathBuf, process::Stdio};
use eyre::Context;
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 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? {
lines.push(line)
tracing::trace!("{}", line)
}
Ok(lines)
}
pub fn print_res(scope: String, res: Vec<String>) {
for r in res {
tracing::debug!("{}: {}", scope, r);
}
Ok(())
}