1
0
mirror of https://github.com/jht5945/rust_util.git synced 2025-12-27 15:40:03 +08:00

feat: add env_var

This commit is contained in:
2025-08-24 16:11:03 +08:00
parent 4b596db8de
commit e2b258ca09
2 changed files with 13 additions and 1 deletions

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "rust_util" name = "rust_util"
version = "0.6.48" version = "0.6.49"
authors = ["Hatter Jiang <jht5945@gmail.com>"] authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018" edition = "2018"
description = "Hatter's Rust Util" description = "Hatter's Rust Util"

View File

@@ -17,3 +17,15 @@ pub fn is_off(val: &str) -> bool {
let lower_val = val.to_lowercase(); let lower_val = val.to_lowercase();
["false", "no", "0"].iter().any(|x| *x == lower_val) ["false", "no", "0"].iter().any(|x| *x == lower_val)
} }
pub fn env_var(var: &str) -> Option<String> {
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
}