fix(config): 优化 layout
- 新增 从环境变量解析目录结构
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::{ShimConfig, ShimLayout};
|
||||
use crate::{ShimConfig, Layout};
|
||||
use anyhow::{Context, Result, anyhow, bail};
|
||||
use mlua::{FromLua, Lua, StdLib, Table, Value};
|
||||
use std::ffi::OsStr;
|
||||
@@ -17,7 +17,7 @@ pub struct LuaRuntime {
|
||||
|
||||
impl LuaRuntime {
|
||||
/// 初始化限定权限的 Lua 沙箱环境
|
||||
pub fn new(layout: &ShimLayout) -> Result<Self> {
|
||||
pub fn new(layout: &Layout) -> Result<Self> {
|
||||
// 只加载安全的标准库,剥离 os / io 等风险模块
|
||||
let lua = Lua::new_with(
|
||||
StdLib::TABLE | StdLib::STRING | StdLib::MATH | StdLib::PACKAGE,
|
||||
@@ -28,12 +28,12 @@ impl LuaRuntime {
|
||||
let globals = lua.globals();
|
||||
|
||||
// 统一使用 POSIX 风格路径规范化路径字符串
|
||||
let root_dir = normalize_path_for_lua(&layout.root_dir);
|
||||
let base_dir = normalize_path_for_lua(&layout.base_dir);
|
||||
let tools_dir = normalize_path_for_lua(&layout.tools_dir);
|
||||
|
||||
// 1. 注入锚点变量 __SHIM_DIR__(shim 安装根目录)
|
||||
globals
|
||||
.set("__SHIM_DIR__", root_dir.clone())
|
||||
.set("__SHIM_DIR__", base_dir.clone())
|
||||
.context("设置 __SHIM_DIR__ 环境变量失败")?;
|
||||
|
||||
// 2. 安全暴露 get_env 供配置读取环境变量
|
||||
@@ -82,14 +82,14 @@ impl LuaRuntime {
|
||||
if let Ok(path) = package.get::<String>("path") {
|
||||
let new_path = format!(
|
||||
"{};{}/?.lua;{}/?/init.lua;{}/?.lua;{}/?/init.lua",
|
||||
path, root_dir, root_dir, tools_dir, tools_dir
|
||||
path, base_dir, base_dir, tools_dir, tools_dir
|
||||
);
|
||||
let _ = package.set("path", new_path);
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 包装 require:配置模块缺失/加载失败时记录日志并跳过该条目,
|
||||
// 而不是让整个 shims.lua 解析失败(排查问题时日志可见)
|
||||
// 而不是让整个 mimic.lua 解析失败(排查问题时日志可见)
|
||||
let original_require: mlua::Function = globals
|
||||
.get("require")
|
||||
.context("获取内置 require 函数失败")?;
|
||||
@@ -145,23 +145,20 @@ impl LuaRuntime {
|
||||
.with_context(|| format!("执行 Lua 配置文件失败: {}", path.display()))
|
||||
}
|
||||
|
||||
// /// 将 Lua Value 解析转化为 ShimConfig 数据对象
|
||||
// pub fn parse_config(&self, value: Value) -> Result<ShimConfig, Error> {
|
||||
// ShimConfig::from_lua(value, &self.lua).map_err(|e| Error::InvalidConfig(e.to_string()))
|
||||
// }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::ShimLayout;
|
||||
use crate::Layout;
|
||||
|
||||
fn test_layout() -> ShimLayout {
|
||||
fn test_layout() -> Layout {
|
||||
let root = std::env::temp_dir().join("rshim-test-layout");
|
||||
ShimLayout {
|
||||
root_dir: root.clone(),
|
||||
Layout {
|
||||
base_dir: root.clone(),
|
||||
bin_dir: root.join("bin"),
|
||||
tools_dir: root.join("tools"),
|
||||
lua_file: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user