From e47a0673e06c6b55019e6cdccd22158138a81cda Mon Sep 17 00:00:00 2001 From: jiangjianfeng Date: Fri, 17 Sep 2021 14:30:42 +0800 Subject: [PATCH] Resolve environmental variables in bom file --- tools/copy_bom/src/bom.rs | 27 +++++++++++++++++++++++++++ tools/copy_bom/src/util.rs | 13 +++++++++++++ 2 files changed, 40 insertions(+) diff --git a/tools/copy_bom/src/bom.rs b/tools/copy_bom/src/bom.rs index 88987e0a..1f69fa79 100644 --- a/tools/copy_bom/src/bom.rs +++ b/tools/copy_bom/src/bom.rs @@ -408,6 +408,33 @@ impl TargetManagement { self.files_autodep.extend(files_autodep.into_iter()); } + fn resolve_environmental_variables(&mut self) { + self.dirs_to_make = self + .dirs_to_make + .iter() + .map(|dir| resolve_envs(dir)) + .collect(); + self.links_to_create = self + .links_to_create + .iter() + .map(|(src, linkname)| (resolve_envs(src), resolve_envs(linkname))) + .collect(); + self.dirs_to_copy = self + .dirs_to_copy + .iter() + .map(|(src, dest)| (resolve_envs(src), resolve_envs(dest))) + .collect(); + self.files_to_copy = self + .files_to_copy + .iter() + .map(|(src, dest)| (resolve_envs(src), resolve_envs(dest))) + .collect(); + self.files_autodep = self + .files_autodep + .iter() + .map(|file| resolve_envs(file)) + .collect(); + } } /// This function will return all included bom files in the order to deal with. diff --git a/tools/copy_bom/src/util.rs b/tools/copy_bom/src/util.rs index 5749393f..c438787e 100644 --- a/tools/copy_bom/src/util.rs +++ b/tools/copy_bom/src/util.rs @@ -80,3 +80,16 @@ pub fn find_included_bom_file( std::process::exit(FILE_NOT_EXISTS_ERROR); } +/// Try to resolve a path may contain environmental variables to a path without environmental variables +/// This function relies on a third-party crate shellexpand. +/// Known limitations: If the environmental variable points to an empty value, the conversion may fail. +pub fn resolve_envs(path: &str) -> String { + shellexpand::env(path).map_or_else( + |_| { + warn!("{} resolve fails.", path); + path.to_string() + }, + |res| res.to_string(), + ) +} +