Skip to main content

lux_cli/
exec.rs

1use std::env;
2
3use clap::Args;
4use eyre::Result;
5use lux_lib::{config::Config, lua_version::LuaVersion, operations, path::Paths, project::Project};
6
7#[derive(Args)]
8pub struct Exec {
9    /// The command to run.
10    command: String,
11
12    /// Arguments to pass to the program.
13    args: Option<Vec<String>>,
14
15    /// Do not add `require('lux').loader()` to `LUA_INIT`.
16    /// If a rock has conflicting transitive dependencies,
17    /// disabling the Lux loader may result in the wrong modules being loaded.
18    #[clap(default_value_t = false)]
19    #[arg(long)]
20    no_loader: bool,
21}
22
23pub async fn exec(run: Exec, config: Config) -> Result<()> {
24    let project = Project::current()?;
25    let tree = match &project {
26        Some(project) => project.tree(&config)?,
27        None => {
28            let lua_version = LuaVersion::from(&config)?.clone();
29            config.user_tree(lua_version)?
30        }
31    };
32
33    let paths = Paths::new(&tree)?;
34    unsafe {
35        // safe as long as this is single-threaded
36        env::set_var("PATH", paths.path_prepended().joined());
37    }
38    operations::Exec::new(&run.command, project.as_ref(), &config)
39        .args(run.args.unwrap_or_default())
40        .disable_loader(run.no_loader)
41        .exec()
42        .await?;
43    Ok(())
44}