before index-conflict changes
This commit is contained in:
86
src/utils/parser/env.rs
Normal file
86
src/utils/parser/env.rs
Normal file
@ -0,0 +1,86 @@
|
||||
use std::io;
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::*;
|
||||
|
||||
|
||||
pub fn get_install_env(repo: &String, pkgname: &String, pkg_md_path: &Path, stage: &str) -> HashMap<String, String> {
|
||||
let mut full_env = HashMap::new();
|
||||
full_env.insert("repo".to_string(), repo.clone());
|
||||
full_env.insert("pkgname".to_string(), pkgname.clone());
|
||||
if let Some(global_env) = get_global_env_string().ok() { full_env.extend(global_env) }
|
||||
if let Some(pkg_env) = get_pkg_env(pkg_md_path).ok() { full_env.extend(pkg_env) }
|
||||
if let Some(repo_env) = get_repo_env(repo).ok() { full_env.extend(repo_env) }
|
||||
if let Some(stage_env) = get_stage_env(stage).ok() { full_env.extend(stage_env) }
|
||||
if let Some(repo_stage_env) = get_repo_and_stage_env(repo, stage).ok() { full_env.extend(repo_stage_env) }
|
||||
full_env.extend(env::vars().map(|(k, v)| (k, v)));
|
||||
return full_env
|
||||
}
|
||||
|
||||
pub fn get_custom_env(repo: &String, pkgname: &String, pkg_md_path: &Path) -> HashMap<String, String> {
|
||||
let mut full_env = HashMap::new();
|
||||
full_env.insert("repo".to_string(), repo.clone());
|
||||
full_env.insert("pkgname".to_string(), pkgname.clone());
|
||||
if let Some(global_env) = get_global_env_string().ok() { full_env.extend(global_env) }
|
||||
if let Some(pkg_env) = get_pkg_env(pkg_md_path).ok() { full_env.extend(pkg_env) }
|
||||
if let Some(repo_env) = get_repo_env(repo).ok() { full_env.extend(repo_env) }
|
||||
full_env.extend(env::vars().map(|(k, v)| (k, v)));
|
||||
return full_env
|
||||
}
|
||||
|
||||
pub fn get_global_env() -> HashMap<String, String> {
|
||||
let mut full_env = HashMap::new();
|
||||
full_env.insert("repo".to_string(), repo.clone());
|
||||
full_env.insert("pkgname".to_string(), pkgname.clone());
|
||||
if let Some(global_env) = get_global_env_string().ok() { full_env.extend(global_env) }
|
||||
return full_env
|
||||
}
|
||||
|
||||
fn get_global_env_string() -> io::Result<HashMap<String, String>> {
|
||||
let cfg_path = crate::commands::get_etc_path().join("aeropkg.md");
|
||||
let content = extract_block(&cfg_path, &format!("``` env *** env ***"), "```")?;
|
||||
Ok(parse_env_vars(&content))
|
||||
}
|
||||
|
||||
fn parse_env_vars(content: &str) -> HashMap<String, String> {
|
||||
content
|
||||
.lines()
|
||||
.filter_map(|line| {
|
||||
let line = line.trim();
|
||||
if line.is_empty() || line.starts_with('#') {
|
||||
return None;
|
||||
}
|
||||
let parts: Vec<&str> = line.splitn(2, '=').collect();
|
||||
if parts.len() != 2 {
|
||||
return None;
|
||||
}
|
||||
Some((parts[0].to_string(), parts[1].to_string()))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn get_pkg_env(file_path: &Path) -> io::Result<HashMap<String, String>> {
|
||||
let content = extract_block(file_path, "``` env *** env ***", "```")?;
|
||||
Ok(parse_env_vars(&content))
|
||||
}
|
||||
|
||||
|
||||
pub fn get_repo_env(repo: &str) -> io::Result<HashMap<String, String>> {
|
||||
let cfg_path = crate::commands::get_etc_path().join("aeropkg.md");
|
||||
let content = extract_block(&cfg_path, &format!("``` env *** env {} ***", repo), "```")?;
|
||||
Ok(parse_env_vars(&content))
|
||||
}
|
||||
|
||||
pub fn get_stage_env(stage: &str) -> io::Result<HashMap<String, String>> {
|
||||
let cfg_path = crate::commands::get_etc_path().join("aeropkg.md");
|
||||
let content = extract_block(&cfg_path, &format!("``` env *** env {} ***", stage), "```")?;
|
||||
Ok(parse_env_vars(&content))
|
||||
}
|
||||
|
||||
pub fn get_repo_and_stage_env(repo: &str, stage: &str) -> io::Result<HashMap<String, String>> {
|
||||
let cfg_path = crate::commands::get_etc_path().join("aeropkg.md");
|
||||
let content = extract_block(&cfg_path, &format!("``` env *** env {} {} ***", repo, stage), "```")?;
|
||||
Ok(parse_env_vars(&content))
|
||||
}
|
||||
Reference in New Issue
Block a user