From bb80fbabc90f58496f3bbdd8df08770b6b8ccce0 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sat, 6 Aug 2022 13:22:28 +0800 Subject: [PATCH] feat: v0.1.7 add SKIP_CACHE env --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 16 +++++++++++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7d3df78..079907e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -128,7 +128,7 @@ dependencies = [ [[package]] name = "runrs" -version = "0.1.6" +version = "0.1.7" dependencies = [ "rust_util", "sha256", diff --git a/Cargo.toml b/Cargo.toml index acb1878..e4939e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runrs" -version = "0.1.6" +version = "0.1.7" edition = "2018" license = "MIT/Apache-2.0" description = "Run Rust Scripts" diff --git a/src/main.rs b/src/main.rs index c1e0534..e871767 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,9 +34,11 @@ fn main() { let cache_script_bin_name = format!("{}/Library/Caches/rust-script/binaries/release/{}", home, script_sha256); + + let skip_cache = is_env_on("SKIP_CACHE"); 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 ")); - let mut run_script_cmd = if cache_script_bin_name_exists { + let mut run_script_cmd = if !skip_cache && cache_script_bin_name_exists { Command::new(&cache_script_bin_name) } else { let mut cmd = Command::new(rust_script); @@ -93,13 +95,21 @@ fn get_run_script_bin_name(home: &str) -> PathBuf { debugging!("Run command: {:?}", cargo_install_rust_script); match util_cmd::run_command_and_wait(&mut cargo_install_rust_script) { Err(e) => failure_and_exit!("Install rust-script failed: {}", e), - Ok(exist_status) if !exist_status.success() => failure!("Install rust-script not success: {}", exist_status), - Ok(_) => (), + Ok(exist_status) => if !exist_status.success() { + failure!("Install rust-script not success: {}", exist_status); + }, } } rust_script } +fn is_env_on(env: &str) -> bool { + env::var(env).map(|v| { + let v = v.to_string(); + v == "true" || v == "on" || v == "yes" || v == "1" + }).unwrap_or_else(|_| false) +} + fn get_user_home() -> String { env::var("HOME").unwrap_or_else(|_| failure_and_exit!("$HOME not found!")