Skip to main content

lux_cli/
exec.rs

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