up and down commands

This commit is contained in:
Robin Appelman 2025-11-01 16:50:14 +01:00
commit cd00701028
10 changed files with 77 additions and 16 deletions

27
src/up.rs Normal file
View file

@ -0,0 +1,27 @@
use crate::config::{Config, NamespaceName};
use crate::namespace::NetNs;
use main_error::MainResult;
pub fn up(config: Config) -> MainResult {
let mut namespaces = NetNs::existing()?
.map(NetNs::new)
.collect::<Result<Vec<_>, _>>()?;
for removed in namespaces.extract_if(.., |namespace| {
config.get_namespace(namespace.name()).is_none()
}) {
removed.delete()?;
}
for new in config.namespaces {
if !has_namespace(&namespaces, &new.name) {
namespaces.push(NetNs::new(new.name)?);
}
}
Ok(())
}
fn has_namespace(namespaces: &[NetNs], name: &NamespaceName) -> bool {
namespaces.iter().any(|existing| existing.name() == name)
}