use super::paths::fix_unc_path;
use anyhow::Context as _;
use std::path::{Path, PathBuf};
pub fn find_archive_path(path: &Path) -> anyhow::Result<PathBuf> {
let abs_path = fix_unc_path(&path.canonicalize()?);
for working_path in abs_path.ancestors() {
if working_path.join(".taf").exists() {
return Ok(working_path.to_owned());
}
}
anyhow::bail!(format!(
"{} is not inside a Stelae Archive. Run `taf conf init` to create an archive at this location.",
abs_path.to_string_lossy()
))
}
pub fn get_name_parts(qualified_name: &str) -> anyhow::Result<(String, String)> {
let mut name_parts = qualified_name.split('/');
let org = name_parts.next().context("No organization specified");
let name = name_parts.next().context("No name specified");
Ok((org?.into(), name?.into()))
}
#[cfg(test)]
mod test {
use crate::utils::archive::get_name_parts;
#[test]
fn get_name_parts_when_qualified_name_correct_expect_name_parts() {
let cut = get_name_parts;
let actual = cut("stele/test").unwrap();
let expected = ("stele".to_owned(), "test".to_owned());
assert_eq!(expected, actual);
}
#[test]
fn get_name_parts_when_qualified_name_incorrect_expect_error() {
let cut = get_name_parts;
let actual = cut("test").unwrap_err();
let expected = "No name specified";
assert!(
actual.to_string().contains(expected),
"\"{actual}\" doesn't contain {expected}"
);
}
}