feat: v0.1.8 add env

This commit is contained in:
2022-08-06 15:44:55 +08:00
parent bb80fbabc9
commit 5a8b23cd2d
4 changed files with 40 additions and 9 deletions

View File

@@ -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)
}