use std::path::{Path, PathBuf};
use anyhow::{Context, Result, anyhow};
pub(crate) const MANIFEST_FILENAME: &str = "lightshuttle.yml";
pub(crate) fn resolve_manifest(override_path: Option<&Path>) -> Result<PathBuf> {
if let Some(path) = override_path {
if path.is_file() {
return Ok(path.to_path_buf());
}
return Err(anyhow!("manifest not found at `{}`", path.display()));
}
let cwd = std::env::current_dir().context("failed to read the current directory")?;
discover_from(&cwd)
}
fn discover_from(start: &Path) -> Result<PathBuf> {
let mut current: Option<&Path> = Some(start);
while let Some(dir) = current {
let candidate = dir.join(MANIFEST_FILENAME);
if candidate.is_file() {
return Ok(candidate);
}
current = dir.parent();
}
Err(anyhow!(
"no `{MANIFEST_FILENAME}` found between `{}` and the filesystem root",
start.display()
))
}