Skip to content

Commit

Permalink
refactor(compose): quadlet::Globals from compose_spec::Service
Browse files Browse the repository at this point in the history
First, a `cli::GlobalArgs` is created by taking the `runtime` and
`storage_opt` fields from the `compose_spec::Service`.
Next, that is converted to a `quadlet::Globals` which is used in the
generated `quadlet::File`.

Signed-off-by: Paul Nettleton <k9@k9withabone.dev>
  • Loading branch information
k9withabone committed Apr 26, 2024
1 parent 3a9168f commit 916ea15
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/cli/compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use compose_spec::{Compose, Identifier, Network, Networks, Resource, Service, Vo

use crate::quadlet::{self, container::volume::Source, Globals};

use super::{Container, Unit};
use super::{Container, GlobalArgs, Unit};

/// Deserialize [`Compose`] from a file at the given [`Path`], stdin, or a list of default files.
///
Expand Down Expand Up @@ -183,6 +183,8 @@ fn service_try_into_quadlet_file(
}
}

let global_args = GlobalArgs::from_compose(&mut service);

let restart = service.restart;

let mut container = Container::try_from(service)
Expand All @@ -208,7 +210,7 @@ fn service_try_into_quadlet_file(
name: name.into(),
unit,
resource: container.into(),
globals: Globals::default(),
globals: global_args.into(),
service: restart.map(Into::into),
install,
})
Expand Down
25 changes: 24 additions & 1 deletion src/cli/global_args.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ops::Not, path::PathBuf};
use std::{mem, ops::Not, path::PathBuf};

use clap::{Args, ValueEnum};
use serde::Serialize;
Expand Down Expand Up @@ -148,6 +148,29 @@ pub struct GlobalArgs {
volumepath: Option<PathBuf>,
}

impl GlobalArgs {
/// Consruct [`GlobalArgs`] by taking fields from a [`compose_spec::Service`].
///
/// Takes the `runtime` and `storage_opt` fields.
pub fn from_compose(service: &mut compose_spec::Service) -> Self {
Self {
runtime: service.runtime.take().map(Into::into),
storage_opt: mem::take(&mut service.storage_opt)
.into_iter()
.map(|(key, value)| {
let mut opt = String::from(key);
opt.push('=');
if let Some(value) = value {
opt.push_str(&String::from(value));
}
opt
})
.collect(),
..Self::default()
}
}
}

impl From<GlobalArgs> for Globals {
fn from(value: GlobalArgs) -> Self {
let global_args =
Expand Down

0 comments on commit 916ea15

Please # to comment.