feat: disable unneeded CLI args for standalone

This commit is contained in:
Erica Marigold 2023-11-22 19:50:26 +05:30
parent 2bf68c1e2a
commit 4bb0eba589
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1

View file

@ -85,9 +85,16 @@ impl Cli {
#[allow(clippy::too_many_lines)] #[allow(clippy::too_many_lines)]
pub async fn run(self) -> Result<ExitCode> { pub async fn run(self) -> Result<ExitCode> {
// Signature which is only present in standalone lune binaries
let signature: Vec<u8> = vec![0x4f, 0x3e, 0xf8, 0x41, 0xc3, 0x3a, 0x52, 0x16];
// Read the current lune binary to memory
let bin = read_to_vec(env::current_exe()?).await?;
let is_standalone = bin[bin.len() - signature.len()..bin.len()] == signature;
// List files in `lune` and `.lune` directories, if wanted // List files in `lune` and `.lune` directories, if wanted
// This will also exit early and not run anything else // This will also exit early and not run anything else
if self.list { if self.list && !is_standalone {
let sorted_relative = find_lune_scripts(false).await.map(sort_lune_scripts); let sorted_relative = find_lune_scripts(false).await.map(sort_lune_scripts);
let sorted_home_dir = find_lune_scripts(true).await.map(sort_lune_scripts); let sorted_home_dir = find_lune_scripts(true).await.map(sort_lune_scripts);
@ -150,12 +157,7 @@ impl Cli {
return Ok(ExitCode::SUCCESS); return Ok(ExitCode::SUCCESS);
} }
// Signature which is only present in standalone lune binaries if is_standalone {
let signature: Vec<u8> = vec![0x4f, 0x3e, 0xf8, 0x41, 0xc3, 0x3a, 0x52, 0x16];
// Read the current lune binary to memory
let bin = read_to_vec(env::current_exe()?).await?;
let mut bytecode_offset = 0; let mut bytecode_offset = 0;
let mut bytecode_size = 0; let mut bytecode_size = 0;
@ -198,7 +200,7 @@ impl Cli {
// allow any runner functionality within standalone binaries // allow any runner functionality within standalone binaries
let result = Lune::new() let result = Lune::new()
.with_args(self.script_args.clone()) .with_args(self.script_args.clone()) // TODO: args should also include lune reserved ones
.run( .run(
"STANDALONE", "STANDALONE",
&bin[usize::try_from(bytecode_offset).unwrap() &bin[usize::try_from(bytecode_offset).unwrap()
@ -214,11 +216,14 @@ impl Cli {
Ok(code) => code, Ok(code) => code,
}); });
} }
}
// If not in a standalone context and we don't have any arguments // If not in a standalone context and we don't have any arguments
// display the interactive REPL interface // display the interactive REPL interface
return repl::show_interface().await; return repl::show_interface().await;
} }
if !is_standalone {
// Figure out if we should read from stdin or from a file, // Figure out if we should read from stdin or from a file,
// reading from stdin is marked by passing a single "-" // reading from stdin is marked by passing a single "-"
// (dash) as the script name to run to the cli // (dash) as the script name to run to the cli
@ -248,7 +253,9 @@ impl Cli {
); );
return Ok( return Ok(
match build_standalone(output_path, strip_shebang(script_contents.clone())).await { match build_standalone(output_path, strip_shebang(script_contents.clone()))
.await
{
Ok(exitcode) => exitcode, Ok(exitcode) => exitcode,
Err(err) => { Err(err) => {
eprintln!("{err}"); eprintln!("{err}");
@ -263,12 +270,15 @@ impl Cli {
.with_args(self.script_args) .with_args(self.script_args)
.run(&script_display_name, strip_shebang(script_contents)) .run(&script_display_name, strip_shebang(script_contents))
.await; .await;
Ok(match result { return Ok(match result {
Err(err) => { Err(err) => {
eprintln!("{err}"); eprintln!("{err}");
ExitCode::FAILURE ExitCode::FAILURE
} }
Ok(code) => code, Ok(code) => code,
}) });
}
Ok(ExitCode::SUCCESS)
} }
} }