feat: add container hashmap

This commit is contained in:
Alexander Navarro 2025-06-25 16:14:35 -04:00
commit bb1784f259
12 changed files with 1511 additions and 0 deletions

59
src/manager.rs Normal file
View file

@ -0,0 +1,59 @@
use bollard::models::ContainerSummary;
use std::collections::HashMap;
/// Namespace to manage containers together (like compose projects)
type ServiceGroup = String;
const DEFAULT_GROUP: &str = "NONE";
#[derive(Debug)]
struct Services(HashMap<ServiceGroup, Vec<ContainerSummary>>);
impl From<&Vec<ContainerSummary>> for Services {
fn from(value: &Vec<ContainerSummary>) -> Self {
let mut services = HashMap::new();
for container in value.iter().cloned() {
if container
.mounts
.as_deref()
.is_none_or(|mounts| mounts.is_empty())
{
continue;
}
let project = match &container.labels {
None => DEFAULT_GROUP.to_owned(),
Some(labels) => {
// Returns user provided group if exits
if labels.contains_key("epoch.service.group") {
labels
.get("epoch.service.group")
.and_then(|value| Some(value.to_owned()))
.unwrap()
} else {
labels
// Group by compose hash
.get("com.docker.compose.config-hash")
.and_then(|value| Some(value.to_owned()))
// No group found
.unwrap_or(DEFAULT_GROUP.to_owned())
}
}
};
let list: &mut Vec<ContainerSummary> = services.entry(project).or_default();
list.push(container);
}
Self(services)
}
}
pub async fn manage(containers: &Vec<ContainerSummary>) -> crate::Result<()> {
let services = Services::from(containers);
println!("{:#?}", services);
Ok(())
}