use std::path::Path;
use bv_core::cache::CacheLayout;
use bv_index::GitIndex;
pub const DEFAULT_REGISTRY: &str = "https://github.com/mlberkeley/bv-registry";
pub fn resolve_registry_url(
flag: Option<&str>,
bv_toml: Option<&bv_core::project::BvToml>,
) -> String {
flag.map(|s| s.to_string())
.or_else(|| std::env::var("BV_REGISTRY").ok())
.or_else(|| {
bv_toml
.and_then(|t| t.registry.as_ref())
.map(|r| r.url.clone())
})
.unwrap_or_else(|| DEFAULT_REGISTRY.to_string())
}
pub fn open_index(url: &str, cache: &CacheLayout) -> GitIndex {
GitIndex::new(url, cache.index_dir("default"))
}
pub const STALE_TTL: std::time::Duration = std::time::Duration::from_secs(300);
pub fn maybe_print_refresh(refreshed: bool) {
use owo_colors::{OwoColorize, Stream};
if refreshed {
eprintln!(
" {} index {}",
"Updating".if_supports_color(Stream::Stderr, |t| t.cyan().bold().to_string()),
"done".if_supports_color(Stream::Stderr, |t| t.dimmed().to_string()),
);
}
}
pub fn require_index(index: &GitIndex, _registry_url: &str) -> anyhow::Result<()> {
if !index.is_available() {
anyhow::bail!(
"registry index not cloned yet\n \
Run `bv add <tool>` to clone the registry and resolve your tools\n \
Or if you already have a bv.lock, run `bv sync` to pull images directly"
);
}
Ok(())
}
pub fn git_remote_url(repo_path: &Path) -> Option<String> {
let out = std::process::Command::new("git")
.args([
"-C",
&repo_path.to_string_lossy(),
"remote",
"get-url",
"origin",
])
.output()
.ok()?;
out.status
.success()
.then(|| String::from_utf8_lossy(&out.stdout).trim().to_string())
}