From e2b258ca09093a50b90b8345e9a9b2b11d16e909 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 24 Aug 2025 16:11:03 +0800 Subject: [PATCH] feat: add env_var --- Cargo.toml | 2 +- src/util_env.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2705d54..7e8d0b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rust_util" -version = "0.6.48" +version = "0.6.49" authors = ["Hatter Jiang "] edition = "2018" description = "Hatter's Rust Util" diff --git a/src/util_env.rs b/src/util_env.rs index 0599013..c90ec36 100644 --- a/src/util_env.rs +++ b/src/util_env.rs @@ -17,3 +17,15 @@ pub fn is_off(val: &str) -> bool { let lower_val = val.to_lowercase(); ["false", "no", "0"].iter().any(|x| *x == lower_val) } + +pub fn env_var(var: &str) -> Option { + let var_from_env = env::var(var).ok(); + if var_from_env.is_some() { + return var_from_env; + } + let var_content = crate::util_file::read_file_content(&format!("~/.config/envs/{}", var)); + if let Ok(var_content) = var_content { + return Some(var_content.trim().to_string()); + } + None +}