use gy_ledger::{Actor, Error, FileStore, FormatVersion, Repository, Result, format};
use std::path::{Path, PathBuf};
const READ_ACTOR: &str = "gy-read";
pub fn root(start: Option<&Path>) -> Result<PathBuf> {
let mut dir = match start {
Some(dir) => dir
.canonicalize()
.map_err(|_| Error::invalid(format!("no directory {}", dir.display())))?,
None => std::env::current_dir()?,
};
loop {
if dir.join("gy.toml").is_file() {
return Ok(dir);
}
match dir.parent() {
Some(parent) => dir = parent.to_path_buf(),
None => return Err(Error::invalid("no gy.toml found; pass -C <dir>")),
}
}
}
pub fn open(ledger: &Path) -> Result<Repository<FileStore>> {
if !ledger.join(format::FILE).is_file() {
return Err(Error::invalid(format!("no ledger at {}", ledger.display())));
}
let store = FileStore::open_with(ledger, |_| Some(READ_ACTOR.to_string()))?;
Ok(Repository::new(store))
}
pub fn open_write(ledger: &Path) -> Result<Repository<FileStore>> {
Actor::from_env()?;
if !ledger.join(format::FILE).is_file() {
std::fs::create_dir_all(ledger)?;
format::write(ledger, FormatVersion::CURRENT)?;
}
Ok(Repository::new(FileStore::open(ledger)?))
}