Skip to content

Commit

Permalink
fix(compose): network_mode accept all podman values
Browse files Browse the repository at this point in the history
- Support all `podman run --network` values for service `network_mode`
- Improve error message for unsupported values, link to the podman docs

Fixes: #38
  • Loading branch information
k9withabone committed Dec 22, 2023
1 parent 23bdcb6 commit 561bd22
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions src/cli/container/quadlet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use std::{
};

use clap::{Args, ValueEnum};
use color_eyre::eyre::{self, Context};
use color_eyre::{
eyre::{self, Context},
owo_colors::OwoColorize,
Section,
};
use docker_compose_types::{MapOrEmpty, Volumes};

use crate::{
Expand Down Expand Up @@ -496,11 +500,7 @@ impl TryFrom<&mut ComposeService> for QuadletOptions {
let network = service
.network_mode
.take()
.map(|mode| match mode.as_str() {
"bridge" | "host" | "none" => Ok(mode),
s if s.starts_with("container") => Ok(mode),
_ => Err(eyre::eyre!("network_mode `{mode}` is unsupported")),
})
.map(filter_network_mode)
.transpose()?
.into_iter()
.chain(map_networks(mem::take(&mut service.networks)))
Expand Down Expand Up @@ -789,6 +789,36 @@ fn volumes_try_into_short(
.collect()
}

/// Filters out unsupported compose service `network_mode`s.
///
/// # Errors
///
/// Returns an error if the given `mode` is not supported by `podman run --network`.
fn filter_network_mode(mode: String) -> color_eyre::Result<String> {
match mode.as_str() {
"host" | "none" | "private" => Ok(mode),
s if s.starts_with("bridge")
|| s.starts_with("container")
|| s.starts_with("slirp4netns")
|| s.starts_with("pasta") =>
{
Ok(mode)
}
s if s.starts_with("service") => Err(eyre::eyre!(
"network_mode `service:` is not supported by podman"
))
.suggestion("try using the `container:` network_mode instead"),
_ => Err(eyre::eyre!("network_mode `{mode}` is not supported")),
}
.with_suggestion(|| {
format!(
"see the --network section of the {} documentation for supported values: \
https://docs.podman.io/en/stable/markdown/podman-run.1.html#network-mode-net",
"podman-run(1)".bold()
)
})
}

fn map_networks(networks: docker_compose_types::Networks) -> Vec<String> {
match networks {
docker_compose_types::Networks::Simple(networks) => networks
Expand Down

0 comments on commit 561bd22

Please sign in to comment.