use std::collections::HashSet;
use crate::compose::types::ComposeFile;
use crate::ui::progress::Kind;
use super::super::network::resolve_network_name;
use super::super::Engine;
impl Engine {
pub(super) fn up_resources(
&self,
file: &ComposeFile,
enabled: &HashSet<String>,
target_set: &Option<HashSet<String>>,
) -> Vec<(Kind, String)> {
let mut out = Vec::new();
for (key, cfg) in &file.networks {
if cfg.as_ref().and_then(|c| c.external).unwrap_or(false) {
continue;
}
out.push((
Kind::Network,
resolve_network_name(key, file, &self.project),
));
}
for (key, cfg) in &file.volumes {
let cfg = cfg.as_ref();
if cfg.and_then(|c| c.external).unwrap_or(false) {
continue;
}
let name = cfg
.and_then(|c| c.name.as_deref())
.map(str::to_string)
.unwrap_or_else(|| format!("{}_{}", self.project, key));
out.push((Kind::Volume, name));
}
let levels = crate::compose::resolve_levels(file)
.unwrap_or_else(|_| vec![file.services.keys().cloned().collect::<Vec<_>>()]);
for level in levels {
for name in level {
if target_set.as_ref().is_some_and(|set| !set.contains(&name)) {
continue;
}
if !enabled.contains(&name) {
continue;
}
let Some(service) = file.services.get(&name) else {
continue;
};
for container in self.replica_names(&name, service) {
out.push((Kind::Container, container));
}
}
}
out
}
pub(super) fn down_resources(
&self,
file: &ComposeFile,
live: &[String],
remove_volumes: bool,
) -> Vec<(Kind, String)> {
let mut out: Vec<(Kind, String)> = live
.iter()
.map(|name| (Kind::Container, name.clone()))
.collect();
for (key, cfg) in &file.networks {
if cfg.as_ref().and_then(|c| c.external).unwrap_or(false) {
continue;
}
out.push((
Kind::Network,
resolve_network_name(key, file, &self.project),
));
}
if remove_volumes {
for (key, cfg) in &file.volumes {
let cfg = cfg.as_ref();
if cfg.and_then(|c| c.external).unwrap_or(false) {
continue;
}
let name = cfg
.and_then(|c| c.name.as_deref())
.map(str::to_string)
.unwrap_or_else(|| format!("{}_{}", self.project, key));
out.push((Kind::Volume, name));
}
}
out
}
}
#[cfg(all(test, unix))]
#[path = "seed_tests.rs"]
mod tests;