Skip to content

Commit

Permalink
Refactor run_main_file function
Browse files Browse the repository at this point in the history
  • Loading branch information
hahnavi committed Dec 17, 2024
1 parent e9ea73f commit 3f47ba8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ rustyline = "15.0.0"
serde_json = "1.0.133"
ssh2 = { version = "0.9.4", features = ["vendored-openssl"] }
tokio = { version = "1.42.0", features = ["io-std", "macros", "rt"] }

[profile.release]
lto = "fat"
63 changes: 30 additions & 33 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use args::Args;
use clap::Parser;
use mlua::{chunk, Error::RuntimeError, Integer, Lua, MultiValue, Table, Value};
use modules::{apt, base_module, cmd, lineinfile, download, script, template, upload};
use modules::{apt, base_module, cmd, download, lineinfile, script, template, upload};
use rustyline::DefaultEditor;
use ssh::{ElevateMethod, Elevation, SSHAuthMethod, SSHSession};
use std::{env, path::Path};
Expand All @@ -27,6 +27,31 @@ async fn main() -> anyhow::Result<()> {

let lua = Lua::new();

let project_dir = match args.main_file.clone() {
Some(main_file) => {
let main_file_path = Path::new(&main_file);
let project_dir = match main_file_path.parent() {
Some(parent) => Some(
parent
.canonicalize()
.unwrap_or_else(|_| parent.to_path_buf()),
),
_none => None,
}
.unwrap();
project_dir.display().to_string()
}
None => env::current_dir()?.display().to_string(),
};

let project_dir_lua = project_dir.clone();
lua.load(
chunk! {
package.path = $project_dir_lua .. "/?.lua;" .. $project_dir_lua .. "/?;" .. $project_dir_lua .. "/lua_modules/share/lua/5.1/?.lua;" .. $project_dir_lua .. "/lua_modules/share/lua/5.1/?/init.lua;" .. package.path
package.cpath = $project_dir_lua .. "/?.so;" .. $project_dir_lua .. "/lua_modules/lib/lua/5.1/?.so;" .. package.cpath
}
).exec()?;

setup_komandan_table(&lua)?;

let chunk = args.chunk.clone();
Expand All @@ -39,7 +64,7 @@ async fn main() -> anyhow::Result<()> {

match &args.main_file {
Some(main_file) => {
run_main_file(&lua, main_file)?;
run_main_file(&lua, &main_file)?;
}
None => {
if args.chunk.is_none() {
Expand All @@ -48,7 +73,7 @@ async fn main() -> anyhow::Result<()> {
}
};

if args.interactive && (!&args.main_file.is_none() || !&args.chunk.is_none()) {
if args.interactive && (!args.main_file.is_none() || !&args.chunk.is_none()) {
repl(&lua);
}

Expand Down Expand Up @@ -371,37 +396,9 @@ fn repl(lua: &Lua) {
}

fn run_main_file(lua: &Lua, main_file: &String) -> anyhow::Result<()> {
let main_file_path = Path::new(&main_file);
let main_file_name = main_file_path
.file_stem()
.unwrap_or_default()
.to_str()
.unwrap()
.to_string();

let project_dir = match main_file_path.parent() {
Some(parent) => Some(
parent
.canonicalize()
.unwrap_or_else(|_| parent.to_path_buf()),
),
_none => None,
}
.unwrap()
.display()
.to_string();

let project_dir_lua = lua.create_string(&project_dir)?;
lua.load(
chunk! {
local project_dir = $project_dir_lua
package.path = project_dir .. "/?.lua;" .. project_dir .. "/?;" .. project_dir .. "/lua_modules/share/lua/5.1/?.lua;" .. project_dir .. "/lua_modules/share/lua/5.1/?/init.lua;"
package.cpath = project_dir .. "/?.so;" .. project_dir .. "/lua_modules/lib/lua/5.1/?.so;"
}
).exec()?;

let main_file = main_file.clone();
lua.load(chunk! {
require($main_file_name)
dofile($main_file)
})
.set_name("main")
.exec()?;
Expand Down

0 comments on commit 3f47ba8

Please sign in to comment.