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 20, 2024
1 parent e533eb7 commit 290a440
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use mlua::{chunk, Error::RuntimeError, Integer, Lua, MultiValue, Table, Value};
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};
use std::{env, fs, path::Path};
use util::{
dprint, filter_hosts, host_display, parse_hosts_json, regex_is_match, set_defaults,
task_display,
Expand Down Expand Up @@ -316,12 +316,14 @@ fn komando(lua: &Lua, (host, task): (Value, Value)) -> mlua::Result<Table> {
}

pub fn run_main_file(lua: &Lua, main_file: &String) -> anyhow::Result<()> {
let main_file = main_file.clone();
lua.load(chunk! {
dofile($main_file)
})
.set_name("main")
.exec()?;
let script = match fs::read_to_string(main_file) {
Ok(script) => script,
Err(e) => {
return Err(anyhow::anyhow!("Failed to read the main file ({}): {}", main_file, e));
}
};

lua.load(&script).set_name(main_file).exec()?;

Ok(())
}
Expand Down

0 comments on commit 290a440

Please sign in to comment.