Skip to content

Commit 2f687e7

Browse files
Mira5121804ElhamAryanpur
authored andcommitted
feat(cli): add -e flag to execute code from command line
1 parent c34aed3 commit 2f687e7

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

src/commands/run.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use tracing::error;
55
/// Runs a Lua script.
66
pub async fn run_command(
77
file_path: Option<String>,
8+
code: Option<String>,
89
stdlib_path: Option<String>,
910
check_teal_code: bool,
1011
extra_args: Option<Vec<String>>,
@@ -32,13 +33,19 @@ pub async fn run_command(
3233

3334
// Load and execute the Lua script.
3435
#[allow(clippy::expect_used)]
35-
let user_file = if let Some(file_path) = file_path {
36-
check_for_default_file(file_path)
36+
let (user_file, actual_path_str) = if let Some(code) = code {
37+
actual_path = "<commandline>".to_string();
38+
(code, actual_path.clone())
3739
} else {
38-
check_for_default_file(".".to_string())
40+
let file = if let Some(file_path) = file_path {
41+
check_for_default_file(file_path)
42+
} else {
43+
check_for_default_file(".".to_string())
44+
};
45+
(file, actual_path.clone())
3946
};
4047

41-
run_command_prerequisite(&actual_path, stdlib_path, check_teal_code, extra_args).await;
48+
run_command_prerequisite(&actual_path_str, stdlib_path, check_teal_code, extra_args).await;
4249
spawn_termination_task();
4350

4451
// Remove the Shebang lines
@@ -48,17 +55,21 @@ pub async fn run_command(
4855
.collect::<Vec<_>>()
4956
.join("\n");
5057

51-
if let Some(is_teal) = PathBuf::from(&actual_path).extension()
58+
if actual_path_str == "<commandline>" {
59+
if let Err(e) = lua.load(user_file).set_name("<commandline>").exec_async().await {
60+
error!("{}", e);
61+
}
62+
} else if let Some(is_teal) = PathBuf::from(&actual_path_str).extension()
5263
&& is_teal == "tl"
5364
{
5465
if let Err(e) = crate::components::load_teal(lua).await {
5566
error!("{}", e);
5667
}
5768

58-
if let Err(e) = crate::components::execute_teal_code(lua, &actual_path, &user_file).await {
69+
if let Err(e) = crate::components::execute_teal_code(lua, &actual_path_str, &user_file).await {
5970
error!("{}", e);
6071
}
61-
} else if let Err(e) = lua.load(user_file).set_name(actual_path).exec_async().await {
72+
} else if let Err(e) = lua.load(user_file).set_name(actual_path_str).exec_async().await {
6273
error!("{}", e);
6374
}
6475

src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ enum AstraCLI {
4545
Run {
4646
/// Path to the Lua script file.
4747
file_path: Option<String>,
48+
/// Execute code directly from command line instead of a file.
49+
#[arg(short = 'e', long)]
50+
code: Option<String>,
4851
/// Path to the standard library folder
4952
#[arg(short, long)]
5053
stdlib_path: Option<String>,
@@ -88,10 +91,11 @@ pub async fn main() -> std::io::Result<()> {
8891
match AstraCLI::parse() {
8992
AstraCLI::Run {
9093
file_path,
94+
code,
9195
stdlib_path,
9296
check_teal_code,
9397
extra_args,
94-
} => commands::run_command(file_path, stdlib_path, check_teal_code, extra_args).await,
98+
} => commands::run_command(file_path, code, stdlib_path, check_teal_code, extra_args).await,
9599
AstraCLI::ExportBundle { teal_export, path } => {
96100
commands::export_bundle_command(teal_export, path).await?
97101
}

0 commit comments

Comments
 (0)