use crate::hub::get_hub;
use crate::runtime::Runtime;
use crate::{Error, Result};
use mlua::{IntoLua, Lua, Table, Value};
pub fn init_module(lua: &Lua, runtime: &Runtime) -> Result<Table> {
let table = lua.create_table()?;
let rt = runtime.clone();
let git_restore_fn = lua.create_function(move |lua, (path,): (String,)| git_restore(lua, &rt, path))?;
table.set("restore", git_restore_fn)?;
Ok(table)
}
fn git_restore(lua: &Lua, runtime: &Runtime, path: String) -> mlua::Result<Value> {
let current_dir = runtime
.dir_context()
.try_wks_dir_with_err_ctx("aip.git.restore requires a aipack workspace setup")?;
let output = std::process::Command::new("git")
.current_dir(current_dir)
.arg("restore")
.arg(&path)
.output()
.expect("Failed to execute command");
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
if !stderr.is_empty() {
get_hub().publish_sync(format!("stderr: {}", stderr));
return Err(Error::cc(format!("'git restore {path}' failed"), stderr).into());
}
stdout.into_lua(lua)
}