feat: v0.1.9 add MAX_SCRIPT_LEN
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -128,7 +128,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "runrs"
|
name = "runrs"
|
||||||
version = "0.1.8"
|
version = "0.1.9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"rust_util",
|
"rust_util",
|
||||||
"sha256",
|
"sha256",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "runrs"
|
name = "runrs"
|
||||||
version = "0.1.8"
|
version = "0.1.9"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
description = "Run Rust Scripts"
|
description = "Run Rust Scripts"
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ Environment variables
|
|||||||
| `HOME` | User home, default by OS system |
|
| `HOME` | User home, default by OS system |
|
||||||
| `SKIP_CACHE` | Skip compiled cached file , turn on `true`, `yes`, `on` or `1` |
|
| `SKIP_CACHE` | Skip compiled cached file , turn on `true`, `yes`, `on` or `1` |
|
||||||
| `RUST_SCRIPT` | `rust_script` command line bin file |
|
| `RUST_SCRIPT` | `rust_script` command line bin file |
|
||||||
|
| `MAX_SCRIPT_LEN` | Max script length |
|
||||||
|
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
|
|||||||
25
src/main.rs
25
src/main.rs
@@ -10,16 +10,14 @@ use rust_util::{util_cmd, util_msg};
|
|||||||
use rust_util::util_msg::MessageType;
|
use rust_util::util_msg::MessageType;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let home = get_user_home();
|
let user_home = get_user_home();
|
||||||
let rust_script = get_run_script_bin_name(&home);
|
let rust_script = get_run_script_bin_name(&user_home);
|
||||||
|
|
||||||
let args = env::args().skip(1).collect::<Vec<_>>();
|
let args = env::args().skip(1).collect::<Vec<_>>();
|
||||||
if args.is_empty() {
|
if args.is_empty() {
|
||||||
failure_and_exit!("runrs v{}, need arguments, e.g.\n\nrunrs <script.rs> [arguments]\n",
|
failure_and_exit!("runrs v{}, need arguments, e.g.\n\nrunrs <script.rs> [arguments]\n", env!("CARGO_PKG_VERSION"));
|
||||||
env!("CARGO_PKG_VERSION"),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
let first_argument = args.get(0).unwrap_or_else(|| failure_and_exit!("Must assign file name"));
|
let first_argument = args.get(0).unwrap_or_else(|| failure_and_exit!("Must assign a script file name"));
|
||||||
if first_argument == "--help" {
|
if first_argument == "--help" {
|
||||||
print_help();
|
print_help();
|
||||||
return;
|
return;
|
||||||
@@ -29,15 +27,15 @@ fn main() {
|
|||||||
let (_, script_sha256) = read_file_and_digest(script_file);
|
let (_, script_sha256) = read_file_and_digest(script_file);
|
||||||
debugging!("File {} -> sha256: {}", script_file, script_sha256);
|
debugging!("File {} -> sha256: {}", script_file, script_sha256);
|
||||||
|
|
||||||
let cache_script_bin_name = format!("{}/Library/Caches/rust-script/binaries/release/{}", home, script_sha256);
|
let cache_script_bin_name = format!("{}/Library/Caches/rust-script/binaries/release/{}", user_home, script_sha256);
|
||||||
let mut run_script_cmd = build_script_command(rust_script, script_file, &script_sha256, &cache_script_bin_name);
|
let mut run_script_cmd = build_script_command(rust_script, script_file, &script_sha256, &cache_script_bin_name);
|
||||||
for arg in args.iter().skip(1) {
|
for arg in args.iter().skip(1) {
|
||||||
run_script_cmd.arg(arg);
|
run_script_cmd.arg(arg);
|
||||||
}
|
}
|
||||||
run_script_command(script_file, cache_script_bin_name, &mut run_script_cmd)
|
run_script_command(script_file, &cache_script_bin_name, &mut run_script_cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_script_command(rust_script: PathBuf, script_file: &String, script_sha256: &String, cache_script_bin_name: &String) -> Command {
|
fn build_script_command(rust_script: PathBuf, script_file: &str, script_sha256: &str, cache_script_bin_name: &str) -> Command {
|
||||||
let skip_cache = is_env_on("SKIP_CACHE");
|
let skip_cache = is_env_on("SKIP_CACHE");
|
||||||
let cache_script_bin_name_exists = fs::metadata(&cache_script_bin_name).is_ok();
|
let cache_script_bin_name_exists = fs::metadata(&cache_script_bin_name).is_ok();
|
||||||
debugging!("Bin name: {} {}exists", cache_script_bin_name, iff!(cache_script_bin_name_exists, "", "not "));
|
debugging!("Bin name: {} {}exists", cache_script_bin_name, iff!(cache_script_bin_name_exists, "", "not "));
|
||||||
@@ -53,7 +51,7 @@ fn build_script_command(rust_script: PathBuf, script_file: &String, script_sha25
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_script_command(script_file: &String, cache_script_bin_name: String, mut run_script_cmd: &mut Command) -> ! {
|
fn run_script_command(script_file: &str, cache_script_bin_name: &str, mut run_script_cmd: &mut Command) -> ! {
|
||||||
debugging!("Run command: {:?}", run_script_cmd);
|
debugging!("Run command: {:?}", run_script_cmd);
|
||||||
let run_command_start = SystemTime::now();
|
let run_command_start = SystemTime::now();
|
||||||
match util_cmd::run_command_and_wait(&mut run_script_cmd) {
|
match util_cmd::run_command_and_wait(&mut run_script_cmd) {
|
||||||
@@ -68,7 +66,7 @@ fn run_script_command(script_file: &String, cache_script_bin_name: String, mut r
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_script_file_to_src(script_file: &String, cache_script_bin_name: String) {
|
fn write_script_file_to_src(script_file: &str, cache_script_bin_name: &str) {
|
||||||
if let Ok(Some(canonicalized_script_file)) = PathBuf::from(script_file)
|
if let Ok(Some(canonicalized_script_file)) = PathBuf::from(script_file)
|
||||||
.canonicalize().map(|f| f.to_str().map(|f| f.to_string())) {
|
.canonicalize().map(|f| f.to_str().map(|f| f.to_string())) {
|
||||||
let cache_script_bin_name_src = format!("{}.src", cache_script_bin_name);
|
let cache_script_bin_name_src = format!("{}.src", cache_script_bin_name);
|
||||||
@@ -99,11 +97,14 @@ runrs <script.rs> [arguments]
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn read_file_and_digest(script_file: &str) -> (String, String) {
|
fn read_file_and_digest(script_file: &str) -> (String, String) {
|
||||||
|
let default_max_script_len = 1024 * 1024;
|
||||||
|
let max_script_len: u64 = env::var("MAX_SCRIPT_LEN")
|
||||||
|
.map(|len| len.parse().unwrap_or_else(|| default_max_script_len)).unwrap_or_else(|| default_max_script_len);
|
||||||
match fs::metadata(script_file) {
|
match fs::metadata(script_file) {
|
||||||
Err(_) => failure_and_exit!("Script file not exists: {}", script_file),
|
Err(_) => failure_and_exit!("Script file not exists: {}", script_file),
|
||||||
Ok(metadata) => if metadata.is_dir() {
|
Ok(metadata) => if metadata.is_dir() {
|
||||||
failure_and_exit!("Assigned input is dir: {}", script_file);
|
failure_and_exit!("Assigned input is dir: {}", script_file);
|
||||||
} else if metadata.len() > 1024 * 1024 {
|
} else if metadata.len() > max_script_len {
|
||||||
failure_and_exit!("Script file: {} too large: {}", script_file, metadata.len());
|
failure_and_exit!("Script file: {} too large: {}", script_file, metadata.len());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user