feat: rename cuddle_cli -> cuddle
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
188
cuddle/src/actions/mod.rs
Normal file
188
cuddle/src/actions/mod.rs
Normal file
@@ -0,0 +1,188 @@
|
||||
use std::{collections::HashMap, path::PathBuf};
|
||||
|
||||
use anyhow::Context;
|
||||
use clap::ArgMatches;
|
||||
use rlua::Lua;
|
||||
use rlua_searcher::AddSearcher;
|
||||
|
||||
use crate::{
|
||||
actions::shell::ShellAction,
|
||||
model::{CuddleScript, CuddleShellScriptArg, CuddleVariable},
|
||||
};
|
||||
|
||||
pub mod shell;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[allow(dead_code)]
|
||||
pub struct CuddleAction {
|
||||
pub script: CuddleScript,
|
||||
pub path: PathBuf,
|
||||
pub description: Option<String>,
|
||||
pub name: String,
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
impl CuddleAction {
|
||||
pub fn new(
|
||||
script: CuddleScript,
|
||||
path: PathBuf,
|
||||
name: String,
|
||||
description: Option<String>,
|
||||
) -> Self {
|
||||
Self {
|
||||
script,
|
||||
path,
|
||||
name,
|
||||
description,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn execute(
|
||||
self,
|
||||
matches: &ArgMatches,
|
||||
variables: Vec<CuddleVariable>,
|
||||
) -> anyhow::Result<()> {
|
||||
match self.script {
|
||||
CuddleScript::Shell(s) => {
|
||||
let mut arg_variables: Vec<CuddleVariable> = vec![];
|
||||
if let Some(args) = s.args {
|
||||
for (k, v) in args {
|
||||
let var = match v {
|
||||
CuddleShellScriptArg::Env(e) => {
|
||||
let env_var = matches.get_one::<String>(&k).cloned().ok_or(
|
||||
anyhow::anyhow!(
|
||||
"failed to find env variable with key: {}",
|
||||
&e.key
|
||||
),
|
||||
)?;
|
||||
|
||||
CuddleVariable::new(k.clone(), env_var)
|
||||
}
|
||||
CuddleShellScriptArg::Flag(flag) => {
|
||||
match matches.get_one::<String>(&flag.name) {
|
||||
Some(flag_var) => {
|
||||
CuddleVariable::new(k.clone(), flag_var.clone())
|
||||
}
|
||||
None => continue,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
arg_variables.push(var);
|
||||
}
|
||||
} else {
|
||||
arg_variables = vec![]
|
||||
};
|
||||
|
||||
let mut vars = variables.clone();
|
||||
vars.append(&mut arg_variables);
|
||||
|
||||
log::trace!("preparing to run action");
|
||||
|
||||
return match ShellAction::new(
|
||||
self.name.clone(),
|
||||
format!(
|
||||
"{}/scripts/{}.sh",
|
||||
self.path
|
||||
.to_str()
|
||||
.expect("action doesn't have a name, this should never happen"),
|
||||
self.name
|
||||
),
|
||||
)
|
||||
.execute(vars)
|
||||
{
|
||||
Ok(()) => {
|
||||
log::trace!("finished running action");
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("{}", e);
|
||||
Err(e)
|
||||
}
|
||||
};
|
||||
}
|
||||
CuddleScript::Dagger(_d) => Err(anyhow::anyhow!("not implemented yet!")),
|
||||
CuddleScript::Lua(l) => {
|
||||
let lua = Lua::new();
|
||||
|
||||
let mut map = HashMap::new();
|
||||
//map.insert("init".into(), "print(\"something\")".into());
|
||||
|
||||
let lua_dir = PathBuf::new().join(&self.path).join("scripts").join("lua");
|
||||
if lua_dir.exists() {
|
||||
let absolute_lua_dir = lua_dir.canonicalize()?;
|
||||
for entry in walkdir::WalkDir::new(&lua_dir)
|
||||
.into_iter()
|
||||
.filter_map(|e| e.ok())
|
||||
{
|
||||
if entry.metadata()?.is_file() {
|
||||
let full_file_path = entry.path().canonicalize()?;
|
||||
let relative_module_path =
|
||||
full_file_path.strip_prefix(&absolute_lua_dir)?;
|
||||
let module_path = relative_module_path
|
||||
.to_string_lossy()
|
||||
.to_string()
|
||||
.trim_end_matches("/init.lua")
|
||||
.trim_end_matches(".lua")
|
||||
.replace("/", ".");
|
||||
let contents = std::fs::read_to_string(entry.path())?;
|
||||
tracing::trace!(module_path = &module_path, "adding lua file");
|
||||
|
||||
map.insert(module_path.into(), contents.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
let lua_rocks_dir = PathBuf::new()
|
||||
.join(&self.path)
|
||||
.join("lua_modules")
|
||||
.join("share")
|
||||
.join("lua")
|
||||
.join("5.4");
|
||||
if lua_rocks_dir.exists() {
|
||||
let absolute_lua_dir = lua_rocks_dir.canonicalize()?;
|
||||
for entry in walkdir::WalkDir::new(&lua_rocks_dir)
|
||||
.into_iter()
|
||||
.filter_map(|e| e.ok())
|
||||
{
|
||||
if entry.metadata()?.is_file() {
|
||||
let full_file_path = entry.path().canonicalize()?;
|
||||
let relative_module_path =
|
||||
full_file_path.strip_prefix(&absolute_lua_dir)?;
|
||||
let module_path = relative_module_path
|
||||
.to_string_lossy()
|
||||
.to_string()
|
||||
.trim_end_matches("/init.lua")
|
||||
.trim_end_matches(".lua")
|
||||
.replace("/", ".");
|
||||
let contents = std::fs::read_to_string(entry.path())?;
|
||||
tracing::trace!(module_path = &module_path, "adding lua file");
|
||||
|
||||
map.insert(module_path.into(), contents.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lua.context::<_, anyhow::Result<()>>(|lua_ctx| {
|
||||
lua_ctx.add_searcher(map)?;
|
||||
let globals = lua_ctx.globals();
|
||||
|
||||
let lua_script_entry = std::fs::read_to_string(
|
||||
PathBuf::new()
|
||||
.join(&self.path)
|
||||
.join("scripts")
|
||||
.join(format!("{}.lua", &self.name)),
|
||||
)
|
||||
.context("failed to find lua script")?;
|
||||
|
||||
lua_ctx
|
||||
.load(&lua_script_entry)
|
||||
.set_name(&self.name)?
|
||||
.exec()?;
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user