feat: refactor actions

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-10-26 21:32:09 +02:00
parent 2cfaaf83ed
commit 938680cb75
Signed by: kjuulh
GPG Key ID: D85D7535F18F35FA
2 changed files with 61 additions and 60 deletions

View File

@ -107,16 +107,6 @@ impl Actions {
}
}
#[derive(Debug, Deserialize)]
struct CuddleActionsSchema {
actions: Vec<CuddleActionSchema>,
}
#[derive(Debug, Deserialize)]
struct CuddleActionSchema {
name: String,
}
pub enum ActionVariant {
Rust { root_path: PathBuf },
Docker,
@ -149,54 +139,6 @@ pub enum ExecutableActionFlag {
Bool,
}
impl CuddleActionsSchema {
fn to_executable(self, action_path: &Path) -> anyhow::Result<ExecutableActions> {
Ok(ExecutableActions {
actions: self
.actions
.into_iter()
.map(|a| {
let name = a.name.clone();
let action_path = action_path.to_string_lossy().to_string();
ExecutableAction {
name: a.name,
description: String::new(),
flags: BTreeMap::default(),
call_fn: LazyResolve::new(Box::new(move || {
let name = name.clone();
let action_path = action_path.clone();
Box::pin(async move {
if let Some(parent) = PathBuf::from(&action_path).parent() {
tokio::process::Command::new("touch")
.arg(parent)
.output()
.await?;
}
tracing::debug!("calling: {}", name);
let mut cmd = tokio::process::Command::new(action_path);
cmd.args(["do", &name]);
let output = cmd.output().await?;
let stdout = std::str::from_utf8(&output.stdout)?;
for line in stdout.lines() {
println!("{}: {}", &name, line);
}
tracing::debug!("finished call for output: {}", &name);
Ok(())
})
})),
}
})
.collect(),
})
}
}
struct LazyResolve(
Arc<dyn Fn() -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + Send>> + Send + Sync>,
);

View File

@ -1,11 +1,12 @@
use std::{
collections::BTreeMap,
env::temp_dir,
path::{Path, PathBuf},
};
use crate::actions::CuddleActionsSchema;
use serde::Deserialize;
use super::ExecutableActions;
use super::{ExecutableAction, ExecutableActions, LazyResolve};
pub struct RustActionsBuilder {
root_path: PathBuf,
@ -171,3 +172,61 @@ impl Drop for TempGuard {
}
}
}
#[derive(Debug, Deserialize)]
struct CuddleActionsSchema {
actions: Vec<CuddleActionSchema>,
}
#[derive(Debug, Deserialize)]
struct CuddleActionSchema {
name: String,
}
impl CuddleActionsSchema {
fn to_executable(self, action_path: &Path) -> anyhow::Result<ExecutableActions> {
Ok(ExecutableActions {
actions: self
.actions
.into_iter()
.map(|a| {
let name = a.name.clone();
let action_path = action_path.to_string_lossy().to_string();
ExecutableAction {
name: a.name,
description: String::new(),
flags: BTreeMap::default(),
call_fn: LazyResolve::new(Box::new(move || {
let name = name.clone();
let action_path = action_path.clone();
Box::pin(async move {
if let Some(parent) = PathBuf::from(&action_path).parent() {
tokio::process::Command::new("touch")
.arg(parent)
.output()
.await?;
}
tracing::debug!("calling: {}", name);
let mut cmd = tokio::process::Command::new(action_path);
cmd.args(["do", &name]);
let output = cmd.output().await?;
let stdout = std::str::from_utf8(&output.stdout)?;
for line in stdout.lines() {
println!("{}: {}", &name, line);
}
tracing::debug!("finished call for output: {}", &name);
Ok(())
})
})),
}
})
.collect(),
})
}
}