Interpolate env variables in NAVI_PATH (#588)

Fixes #572
This commit is contained in:
Denis Isidoro 2021-08-07 07:41:19 -03:00 committed by GitHub
parent 5e7b24abe8
commit dd1cdb5bce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View file

@ -1,3 +1,4 @@
use crate::env_var;
pub use crate::fs::{
create_dir, exe_string, pathbuf_to_string, read_lines, remove_dir, InvalidPath, UnreadableDir,
};
@ -66,6 +67,21 @@ fn without_first(string: &str) -> String {
.to_string()
}
fn interpolate_paths(paths: String) -> String {
let re = Regex::new(r#"\$\{?[a-zA-Z_][a-zA-Z_0-9]*"#).unwrap();
let mut newtext = paths.to_string();
for capture in re.captures_iter(&paths) {
if let Some(c) = capture.get(0) {
let varname = c.as_str().replace("$", "").replace("{", "").replace("}", "");
let replacement = &env_var::must_get(&varname);
newtext = newtext
.replace(&format!("${}", varname), replacement)
.replace(&format!("${{{}}}", varname), replacement);
}
}
newtext
}
fn gen_lists(tag_rules: Option<String>) -> (Option<Vec<String>>, Option<Vec<String>>) {
let mut allowlist = None;
let mut denylist: Option<Vec<String>> = None;
@ -126,7 +142,8 @@ impl fetcher::Fetcher for Fetcher {
};
let paths = paths.expect("Unable to get paths");
let folders = paths_from_path_param(&paths);
let interpolated_paths = interpolate_paths(paths);
let folders = paths_from_path_param(&interpolated_paths);
let home_regex = Regex::new(r"^~").unwrap();
let home = BaseDirs::new().and_then(|b| pathbuf_to_string(b.home_dir()).ok());

View file

@ -18,7 +18,8 @@ _navi() {
stty sane || true
local path="${NAVI_TEST_PATH:-$TEST_CHEAT_PATH}"
path="${path//$HOME/~}"
RUST_BACKTRACE=1 NAVI_PATH="$path" "$NAVI_EXE" "$@"
export NAVI_TEST_PATH="$path"
RUST_BACKTRACE=1 NAVI_PATH='$NAVI_TEST_PATH' "$NAVI_EXE" "$@"
}
_navi_cases() {