use appcore_contracts::{NetworkConfig, ProviderConfig};
use appcore_provider::ResolvedSecret;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
pub struct DeploymentContext {
paths: BTreeMap<String, PathBuf>,
volumes: Vec<ResolvedVolumeMount>,
adapters: BTreeMap<String, ProviderConfig>,
network: NetworkConfig,
environment: BTreeMap<String, DeploymentEnvironmentValue>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedVolumeMount {
name: String,
source: PathBuf,
target: PathBuf,
read_only: bool,
}
pub enum DeploymentEnvironmentValue {
Literal(String),
Secret(ResolvedSecret),
}
impl DeploymentContext {
#[doc(hidden)]
pub fn from_resolved(
paths: BTreeMap<String, PathBuf>,
volumes: Vec<ResolvedVolumeMount>,
adapters: BTreeMap<String, ProviderConfig>,
network: NetworkConfig,
environment: BTreeMap<String, DeploymentEnvironmentValue>,
) -> Self {
Self {
paths,
volumes,
adapters,
network,
environment,
}
}
pub fn path(&self, name: &str) -> Option<&Path> {
self.paths.get(name).map(PathBuf::as_path)
}
pub fn volumes(&self) -> &[ResolvedVolumeMount] {
&self.volumes
}
pub fn adapters(&self) -> &BTreeMap<String, ProviderConfig> {
&self.adapters
}
pub fn network(&self) -> &NetworkConfig {
&self.network
}
pub fn environment(&self, name: &str) -> Option<&DeploymentEnvironmentValue> {
self.environment.get(name)
}
}
impl ResolvedVolumeMount {
#[doc(hidden)]
pub fn new(name: String, source: PathBuf, target: PathBuf, read_only: bool) -> Self {
Self {
name,
source,
target,
read_only,
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn source(&self) -> &Path {
&self.source
}
pub fn target(&self) -> &Path {
&self.target
}
pub fn is_read_only(&self) -> bool {
self.read_only
}
}
impl DeploymentEnvironmentValue {
pub fn expose(&self) -> &str {
match self {
Self::Literal(value) => value,
Self::Secret(value) => value.expose(),
}
}
pub fn is_secret(&self) -> bool {
matches!(self, Self::Secret(_))
}
}
impl std::fmt::Debug for DeploymentEnvironmentValue {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Literal(value) => formatter.debug_tuple("Literal").field(value).finish(),
Self::Secret(_) => formatter.write_str("Secret(REDACTED)"),
}
}
}