Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(service)!: support network_mode: container:{name} #34

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/service/network_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ pub enum NetworkMode {
/// Gives the container access to the specified service only.
Service(Identifier),

/// Gives the service container access to the specified container.
Container(Identifier),

/// Other network mode.
Other(String),
}
Expand All @@ -217,6 +220,9 @@ impl NetworkMode {
/// [`Self::Service`] string prefix.
const SERVICE_PREFIX: &'static str = "service:";

/// [`Self::Container`] string prefix.
const CONTAINER_PREFIX: &'static str = "container:";

/// Parse a [`NetworkMode`] from a string.
///
/// # Errors
Expand All @@ -234,6 +240,8 @@ impl NetworkMode {
Ok(Self::Host)
} else if let Some(service) = s.strip_prefix(Self::SERVICE_PREFIX) {
service.parse().map(Self::Service).map_err(Into::into)
} else if let Some(container) = s.strip_prefix(Self::CONTAINER_PREFIX) {
container.parse().map(Self::Container).map_err(Into::into)
} else {
Ok(Self::Other(network_mode.into()))
}
Expand Down Expand Up @@ -275,6 +283,26 @@ impl NetworkMode {
}
}

/// Returns `true` if the network mode is [`Container`].
///
/// [`Container`]: NetworkMode::Container
#[must_use]
pub const fn is_container(&self) -> bool {
matches!(self, Self::Container(..))
}

/// Returns [`Some`] if the network mode is [`Container`].
///
/// [`Container`]: NetworkMode::Container
#[must_use]
pub const fn as_container(&self) -> Option<&Identifier> {
if let Self::Container(v) = self {
Some(v)
} else {
None
}
}

/// Returns `true` if the network mode is [`Other`].
///
/// [`Other`]: NetworkMode::Other
Expand All @@ -298,7 +326,7 @@ impl NetworkMode {

/// Error returned when [parsing](NetworkMode::parse()) a [`NetworkMode`] from a string.
#[derive(Error, Debug, Clone, Copy, PartialEq, Eq)]
#[error("error parsing service network mode")]
#[error("error parsing service or container network mode")]
pub struct ParseNetworkModeError(#[from] InvalidIdentifierError);

impl_from_str!(NetworkMode => ParseNetworkModeError);
Expand All @@ -309,6 +337,7 @@ impl Display for NetworkMode {
Self::None => f.write_str(Self::NONE),
Self::Host => f.write_str(Self::HOST),
Self::Service(service) => write!(f, "{}{service}", Self::SERVICE_PREFIX),
Self::Container(container) => write!(f, "{}{container}", Self::CONTAINER_PREFIX),
Self::Other(other) => f.write_str(other),
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/test-full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ services:
network_mode-service:
network_mode: service:service

network_mode-container:
network_mode: container:container

network_mode-other:
network_mode: other

Expand Down