feat: v0.1.8 add env
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -128,7 +128,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "runrs"
|
||||
version = "0.1.7"
|
||||
version = "0.1.8"
|
||||
dependencies = [
|
||||
"rust_util",
|
||||
"sha256",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "runrs"
|
||||
version = "0.1.7"
|
||||
version = "0.1.8"
|
||||
edition = "2018"
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "Run Rust Scripts"
|
||||
|
||||
11
README.md
11
README.md
@@ -3,8 +3,19 @@
|
||||
Run rust script
|
||||
|
||||
Install:
|
||||
|
||||
```shell
|
||||
cargo install --git https://git.hatter.ink/hatter/runrs runrs
|
||||
```
|
||||
|
||||
Environment variables
|
||||
|
||||
| KEY | value |
|
||||
| ---- |---------------------------------------------------------------|
|
||||
| `HOME` | User home, default by OS system |
|
||||
| `SKIP_CACHE` | Skip compiled cache file , turn on `true`, `yes`, `on` or `1` |
|
||||
| `RUST_SCRIPT` | `rust_script` command line bin file |
|
||||
|
||||
<br>
|
||||
|
||||
Simply call `rust-script` (https://github.com/fornwall/rust-script).
|
||||
34
src/main.rs
34
src/main.rs
@@ -19,17 +19,13 @@ fn main() {
|
||||
);
|
||||
}
|
||||
let first_argument = args.get(0).unwrap_or_else(|| failure_and_exit!("Must assign file name"));
|
||||
|
||||
if first_argument == "--help" {
|
||||
print_help();
|
||||
return;
|
||||
}
|
||||
|
||||
let script_file = first_argument;
|
||||
let script_content = fs::read_to_string(script_file).unwrap_or_else(|e|
|
||||
failure_and_exit!("Read file: {}, failed: {}", script_file, e)
|
||||
);
|
||||
let script_sha256 = sha256::digest(script_content);
|
||||
|
||||
let (_, script_sha256) = read_file_and_digest(script_file);
|
||||
debugging!("File {} -> sha256: {}", script_file, script_sha256);
|
||||
let cache_script_bin_name = format!("{}/Library/Caches/rust-script/binaries/release/{}",
|
||||
home,
|
||||
@@ -86,7 +82,31 @@ runrs <script.rs> [arguments]
|
||||
);
|
||||
}
|
||||
|
||||
fn read_file_and_digest(script_file: &str) -> (String, String) {
|
||||
match fs::metadata(script_file) {
|
||||
Err(_) => failure_and_exit!("Script file not exists: {}", script_file),
|
||||
Ok(metadata) => if metadata.is_dir() {
|
||||
failure_and_exit!("Assigned input is dir: {}", script_file);
|
||||
} else if metadata.len() > 1024 * 1024 {
|
||||
failure_and_exit!("Script file: {} too large: {}", script_file, metadata.len());
|
||||
}
|
||||
}
|
||||
let script_content = fs::read_to_string(script_file).unwrap_or_else(|e|
|
||||
failure_and_exit!("Read file: {}, failed: {}", script_file, e)
|
||||
);
|
||||
let script_sha256 = sha256::digest(&script_content);
|
||||
|
||||
(script_content, script_sha256)
|
||||
}
|
||||
|
||||
fn get_run_script_bin_name(home: &str) -> PathBuf {
|
||||
if let Ok(rust_script) = env::var("RUST_SCRIPT") {
|
||||
let rust_script_path_buf = PathBuf::from(&rust_script);
|
||||
if !rust_script_path_buf.exists() {
|
||||
warning!("RUST_SCRIPT={} not exists", &rust_script);
|
||||
}
|
||||
return rust_script_path_buf;
|
||||
}
|
||||
let rust_script = PathBuf::from(format!("{}/.cargo/bin/rust-script", home));
|
||||
if !rust_script.exists() {
|
||||
warning!("rust-script not found, install it...");
|
||||
@@ -105,7 +125,7 @@ fn get_run_script_bin_name(home: &str) -> PathBuf {
|
||||
|
||||
fn is_env_on(env: &str) -> bool {
|
||||
env::var(env).map(|v| {
|
||||
let v = v.to_string();
|
||||
let v = v.to_lowercase();
|
||||
v == "true" || v == "on" || v == "yes" || v == "1"
|
||||
}).unwrap_or_else(|_| false)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user