Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Factor out product config path resolution
  • Loading branch information
nightkr committed Dec 9, 2021
commit 3d239afa897963f97c158c94ddf7b7c055a30f7f
26 changes: 16 additions & 10 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,10 @@ impl FromStr for ProductConfigPath {
}

impl ProductConfigPath {
/// Load the [`ProductConfigManager`] from the given path, falling back to the first
/// path that exists from `default_search_paths` if none is given by the user.
pub fn load(
self,
// Should be AsRef<Path>, but that depends on https://github.com/stackabletech/product-config/pull/43
default_search_paths: &[impl AsRef<str>],
) -> OperatorResult<ProductConfigManager> {
fn resolve_or_default<'a>(
&'a self,
default_search_paths: &'a [impl AsRef<str> + 'a],
) -> OperatorResult<&'a str> {
// Use override if specified by the user, otherwise search through defaults given
let search_paths = if let Some(path) = self.path.as_deref() {
vec![path]
Expand All @@ -156,15 +153,24 @@ impl ProductConfigPath {
};
for path in &search_paths {
if <str as AsRef<Path>>::as_ref(path).exists() {
return ProductConfigManager::from_yaml_file(path)
// Fail early if we try and fail to load any files
.map_err(|source| error::Error::ProductConfigLoadError { source });
return Ok(path);
}
}
Err(error::Error::RequiredFileMissing {
search_path: search_paths.into_iter().map(PathBuf::from).collect(),
})
}

/// Load the [`ProductConfigManager`] from the given path, falling back to the first
/// path that exists from `default_search_paths` if none is given by the user.
pub fn load(
&self,
// Should be AsRef<Path>, but that depends on https://github.com/stackabletech/product-config/pull/43
default_search_paths: &[impl AsRef<str>],
) -> OperatorResult<ProductConfigManager> {
ProductConfigManager::from_yaml_file(self.resolve_or_default(default_search_paths)?)
.map_err(|source| error::Error::ProductConfigLoadError { source })
}
}

const PRODUCT_CONFIG_ARG: &str = "product-config";
Expand Down