Skip to content

Commit

Permalink
Merge pull request #147 from pulp-platform/path_env_vars
Browse files Browse the repository at this point in the history
Allow environment variables in Bender.yml paths
  • Loading branch information
micprog authored Jan 8, 2024
2 parents 59d09aa + 50c8d00 commit b54a9ce
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 120 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
### Added
- Add macOS binary for releases
- Add `init` command to initialize a Bender.yml file of an IP.
- Allow environment variables in dependency and sources paths.

## 0.27.4 - 2023-11-14
### Added
Expand Down
39 changes: 25 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ indexmap = { version = "2", features = ["serde"] }
tempfile = "3.5"
glob = "0.3"
walkdir = "2"
subst = "0.3"
14 changes: 7 additions & 7 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ pub fn read_manifest(path: &Path) -> Result<Manifest> {
let manifest = partial
.validate()
.map_err(|cause| Error::chain(format!("Error in manifest {:?}.", path), cause))?;
Ok(manifest.prefix_paths(path.parent().unwrap()))
Ok(manifest.prefix_paths(path.parent().unwrap())?)
}

/// Load a configuration by traversing a directory hierarchy upwards.
Expand Down Expand Up @@ -413,7 +413,7 @@ fn load_config(from: &Path) -> Result<Config> {

// Assemble and merge the default configuration.
let default_cfg = PartialConfig {
database: Some(from.join(".bender")),
database: Some(from.join(".bender").to_str().unwrap().to_string()),
git: Some("git".into()),
overrides: None,
plugins: None,
Expand Down Expand Up @@ -445,7 +445,7 @@ fn maybe_load_config(path: &Path) -> Result<Option<PartialConfig>> {
.map_err(|cause| Error::chain(format!("Cannot open config {:?}.", path), cause))?;
let partial: PartialConfig = serde_yaml::from_reader(file)
.map_err(|cause| Error::chain(format!("Syntax error in config {:?}.", path), cause))?;
Ok(Some(partial.prefix_paths(path.parent().unwrap())))
Ok(Some(partial.prefix_paths(path.parent().unwrap())?))
}

/// Read a lock file.
Expand All @@ -462,14 +462,14 @@ fn read_lockfile(path: &Path, root_dir: &Path) -> Result<Locked> {
.packages
.iter()
.map(|pack| {
if let LockedSource::Path(path) = &pack.1.source {
Ok(if let LockedSource::Path(path) = &pack.1.source {
(
pack.0.clone(),
LockedPackage {
revision: pack.1.revision.clone(),
version: pack.1.version.clone(),
source: LockedSource::Path(if path.is_relative() {
path.clone().prefix_paths(root_dir)
path.clone().prefix_paths(root_dir)?
} else {
path.clone()
}),
Expand All @@ -478,9 +478,9 @@ fn read_lockfile(path: &Path, root_dir: &Path) -> Result<Locked> {
)
} else {
(pack.0.clone(), pack.1.clone())
}
})
})
.collect(),
.collect::<Result<_>>()?,
})
}

Expand Down
32 changes: 18 additions & 14 deletions src/cmd/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ pub fn run(sess: &Session, matches: &ArgMatches) -> Result<()> {
let target_path = patch_link
.clone()
.to_prefix
.prefix_paths(&vendor_package.target_dir);
.prefix_paths(&vendor_package.target_dir)?;
if target_path.exists() {
if target_path.is_dir() {
std::fs::remove_dir_all(target_path.clone())
Expand Down Expand Up @@ -266,8 +266,8 @@ pub fn init(
let link_to = patch_link
.to_prefix
.clone()
.prefix_paths(&vendor_package.target_dir);
let link_from = patch_link.from_prefix.clone().prefix_paths(dep_path);
.prefix_paths(&vendor_package.target_dir)?;
let link_from = patch_link.from_prefix.clone().prefix_paths(dep_path)?;
std::fs::create_dir_all(link_to.parent().unwrap()).map_err(|cause| {
Error::chain(
format!("Failed to create directory {:?}", link_to.parent()),
Expand All @@ -281,7 +281,7 @@ pub fn init(

// Check if includes exist
for path in vendor_package.include_from_upstream.clone() {
if !PathBuf::from(extend_paths(&[path.clone()], dep_path)[0].clone()).exists() {
if !PathBuf::from(extend_paths(&[path.clone()], dep_path)?[0].clone()).exists() {
warnln!("{} not found in upstream, continuing.", path);
}
}
Expand All @@ -291,7 +291,7 @@ pub fn init(
true => copy_recursively(
&link_from,
&link_to,
&extend_paths(&vendor_package.include_from_upstream, dep_path),
&extend_paths(&vendor_package.include_from_upstream, dep_path)?,
&vendor_package
.exclude_from_upstream
.clone()
Expand Down Expand Up @@ -364,6 +364,7 @@ pub fn apply_patches(
.from_prefix
.clone()
.prefix_paths(git.path)
.unwrap()
.is_file()
{
patch_link.from_prefix.as_path()
Expand Down Expand Up @@ -404,11 +405,11 @@ pub fn diff(
let link_from = patch_link
.from_prefix
.clone()
.prefix_paths(dep_path.as_ref()); // dep_path: path to temporary clone. link_to: targetdir/link.to
.prefix_paths(dep_path.as_ref())?; // dep_path: path to temporary clone. link_to: targetdir/link.to
let link_to = patch_link
.to_prefix
.clone()
.prefix_paths(vendor_package.target_dir.as_ref());
.prefix_paths(vendor_package.target_dir.as_ref())?;
if !&link_to.exists() {
return Err(Error::new(format!(
"Could not find {}. Did you run bender vendor init?",
Expand All @@ -423,7 +424,7 @@ pub fn diff(
&extend_paths(
&vendor_package.include_from_upstream,
&vendor_package.target_dir,
),
)?,
&vendor_package
.exclude_from_upstream
.clone()
Expand Down Expand Up @@ -528,7 +529,7 @@ pub fn gen_format_patch(
let to_path = patch_link
.to_prefix
.clone()
.prefix_paths(target_dir.as_ref());
.prefix_paths(target_dir.as_ref())?;
if !&to_path.exists() {
return Err(Error::new(format!(
"Could not find {}. Did you run bender vendor init?",
Expand Down Expand Up @@ -724,16 +725,19 @@ pub fn copy_recursively(
}

/// Prefix paths with prefix. Append ** to directories.
pub fn extend_paths(include_from_upstream: &[String], prefix: impl AsRef<Path>) -> Vec<String> {
pub fn extend_paths(
include_from_upstream: &[String],
prefix: impl AsRef<Path>,
) -> Result<Vec<String>> {
include_from_upstream
.iter()
.map(|pattern| {
let pattern_long = PathBuf::from(pattern).prefix_paths(prefix.as_ref());
let pattern_long = PathBuf::from(pattern).prefix_paths(prefix.as_ref())?;
if pattern_long.is_dir() {
String::from(pattern_long.join("**").to_str().unwrap())
Ok(String::from(pattern_long.join("**").to_str().unwrap()))
} else {
String::from(pattern_long.to_str().unwrap())
Ok(String::from(pattern_long.to_str().unwrap()))
}
})
.collect()
.collect::<Result<_>>()
}
Loading

0 comments on commit b54a9ce

Please sign in to comment.