feat: refactor actions
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
parent
2cfaaf83ed
commit
938680cb75
@ -107,16 +107,6 @@ impl Actions {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
struct CuddleActionsSchema {
|
|
||||||
actions: Vec<CuddleActionSchema>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
struct CuddleActionSchema {
|
|
||||||
name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum ActionVariant {
|
pub enum ActionVariant {
|
||||||
Rust { root_path: PathBuf },
|
Rust { root_path: PathBuf },
|
||||||
Docker,
|
Docker,
|
||||||
@ -149,54 +139,6 @@ pub enum ExecutableActionFlag {
|
|||||||
Bool,
|
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(
|
struct LazyResolve(
|
||||||
Arc<dyn Fn() -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + Send>> + Send + Sync>,
|
Arc<dyn Fn() -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + Send>> + Send + Sync>,
|
||||||
);
|
);
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
use std::{
|
use std::{
|
||||||
|
collections::BTreeMap,
|
||||||
env::temp_dir,
|
env::temp_dir,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::actions::CuddleActionsSchema;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use super::ExecutableActions;
|
use super::{ExecutableAction, ExecutableActions, LazyResolve};
|
||||||
|
|
||||||
pub struct RustActionsBuilder {
|
pub struct RustActionsBuilder {
|
||||||
root_path: PathBuf,
|
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(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user