use super::native_deps::BaseDistro;
pub const DEFAULT_IMAGE_REGISTRY: &str = "ghcr.io/hyperi-io";
pub const DEFAULT_BASE_IMAGE: &str = "debian:trixie-slim";
pub const DEFAULT_BASE_DISTRO: BaseDistro = BaseDistro::Trixie;
#[must_use]
pub fn image_registry_from_cascade() -> String {
#[cfg(feature = "config")]
{
if let Some(cfg) = crate::config::try_get()
&& let Some(s) = cfg.get_string("deployment.image_registry")
&& !s.is_empty()
{
return s;
}
}
DEFAULT_IMAGE_REGISTRY.to_string()
}
#[must_use]
pub fn base_image_from_cascade() -> String {
#[cfg(feature = "config")]
{
if let Some(cfg) = crate::config::try_get()
&& let Some(s) = cfg.get_string("deployment.base_image")
&& !s.is_empty()
{
return s;
}
}
DEFAULT_BASE_IMAGE.to_string()
}
#[must_use]
pub fn base_distro_from_cascade() -> Option<BaseDistro> {
#[cfg(feature = "config")]
{
if let Some(cfg) = crate::config::try_get()
&& let Some(s) = cfg.get_string("deployment.base_distro")
&& !s.is_empty()
&& let Some(distro) = BaseDistro::parse(&s)
{
return Some(distro);
}
}
std::env::var("DEPLOYMENT__BASE_DISTRO")
.ok()
.filter(|s| !s.is_empty())
.as_deref()
.and_then(BaseDistro::parse)
}
#[must_use]
pub fn resolve_base_distro(base_image: &str) -> Option<BaseDistro> {
base_distro_from_cascade().or_else(|| BaseDistro::from_base_image(base_image))
}
#[must_use]
pub fn argocd_repo_url_from_cascade(app_name: &str) -> String {
#[cfg(feature = "config")]
{
if let Some(cfg) = crate::config::try_get()
&& let Some(s) = cfg.get_string("deployment.argocd.repo_url")
&& !s.is_empty()
{
return s;
}
}
format!("https://github.com/hyperi-io/{app_name}")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_are_ghcr_friendly() {
assert_eq!(DEFAULT_IMAGE_REGISTRY, "ghcr.io/hyperi-io");
assert_eq!(DEFAULT_BASE_IMAGE, "debian:trixie-slim");
}
#[test]
fn cascade_falls_back_to_defaults_when_no_config() {
assert_eq!(image_registry_from_cascade(), DEFAULT_IMAGE_REGISTRY);
assert_eq!(base_image_from_cascade(), DEFAULT_BASE_IMAGE);
}
#[test]
fn default_distro_matches_default_base_image() {
assert_eq!(
BaseDistro::from_base_image(DEFAULT_BASE_IMAGE),
Some(DEFAULT_BASE_DISTRO)
);
}
#[test]
fn resolve_falls_through_to_the_base_image() {
assert_eq!(resolve_base_distro("ubuntu:24.04"), Some(BaseDistro::Noble));
}
#[test]
fn resolve_gives_up_on_a_digest_pin() {
assert_eq!(resolve_base_distro("debian@sha256:abc123"), None);
}
#[test]
fn env_answers_when_the_cascade_is_not_loaded() {
temp_env::with_var("DEPLOYMENT__BASE_DISTRO", Some("bookworm"), || {
assert_eq!(
resolve_base_distro("debian@sha256:abc123"),
Some(BaseDistro::Bookworm)
);
});
}
#[test]
fn explicit_distro_beats_the_base_image() {
temp_env::with_var("DEPLOYMENT__BASE_DISTRO", Some("noble"), || {
assert_eq!(
resolve_base_distro("debian:trixie-slim"),
Some(BaseDistro::Noble)
);
});
}
#[test]
fn an_unparseable_or_empty_env_value_is_treated_as_unset() {
temp_env::with_var("DEPLOYMENT__BASE_DISTRO", Some("plucky"), || {
assert_eq!(
resolve_base_distro("debian:trixie-slim"),
Some(BaseDistro::Trixie)
);
assert_eq!(resolve_base_distro("debian@sha256:abc"), None);
});
temp_env::with_var("DEPLOYMENT__BASE_DISTRO", Some(""), || {
assert_eq!(resolve_base_distro("debian@sha256:abc"), None);
});
}
}