Skip to content

Commit

Permalink
Merge pull request #46 from k9withabone/compat
Browse files Browse the repository at this point in the history
feat: set compatibility with `--podman-version`
  • Loading branch information
k9withabone authored Jan 4, 2024
2 parents aeed040 + 72289e8 commit 2af24b3
Show file tree
Hide file tree
Showing 15 changed files with 736 additions and 420 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ unwrap_used = "warn"
verbose_file_reads = "warn"

[dependencies]
clap = { version = "4.2", features = ["derive"] }
clap = { version = "4.2", features = ["derive", "wrap_help"] }
color-eyre = "0.6"
docker-compose-types = "0.7.0"
duration-str = { version = "0.7", default-features = false }
Expand Down
44 changes: 42 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use color_eyre::{
};
use k8s_openapi::api::core::v1::{PersistentVolumeClaim, Pod};

use crate::quadlet;
use crate::quadlet::{self, DowngradeError, PodmanVersion};

use self::{
container::Container, generate::Generate, install::Install, kube::Kube, network::Network,
Expand Down Expand Up @@ -97,6 +97,18 @@ pub struct Cli {
#[arg(long, requires = "file_out")]
skip_services_check: bool,

/// Podman version generated quadlet files should conform to
///
/// An error will occur if the quadlet file cannot be downgraded to the given version.
///
/// Always defaults to the latest supported podman version which added quadlet features.
/// If an earlier version is specified, the quadlet file may not be the most optimal.
///
/// This feature is only supported in a limited way. You should always check quadlet files
/// generated with podlet before running them.
#[arg(short, long, visible_aliases = ["compatibility", "compat"], default_value_t)]
podman_version: PodmanVersion,

/// The \[Unit\] section
#[command(flatten)]
unit: Unit,
Expand Down Expand Up @@ -198,7 +210,20 @@ impl Cli {
let unit = (!self.unit.is_empty()).then_some(self.unit);
let install = self.install.install.then(|| self.install.into());

self.command.try_into_files(self.name, unit, install)
let mut files = self.command.try_into_files(self.name, unit, install)?;

if self.podman_version < PodmanVersion::LATEST {
for file in &mut files {
file.downgrade(self.podman_version).wrap_err_with(|| {
format!(
"error downgrading quadlet to podman v{}",
self.podman_version
)
})?;
}
}

Ok(files)
}
}

Expand Down Expand Up @@ -497,6 +522,21 @@ impl File {
}
}

/// If a quadlet file, downgrade compatibility to `podman_version`.
///
/// This is a one-way transformation, calling downgrade a second time with a higher version
/// will not increase the quadlet options used.
///
/// # Errors
///
/// Returns an error if a used quadlet option is incompatible with the given [`PodmanVersion`].
fn downgrade(&mut self, podman_version: PodmanVersion) -> Result<(), DowngradeError> {
match self {
Self::Quadlet(file) => file.downgrade(podman_version),
Self::KubePod { .. } => Ok(()),
}
}

fn write(&self, path: &FilePath, overwrite: bool) -> color_eyre::Result<()> {
let path = path.to_full(self);
let mut file = open_file(&path, overwrite)?;
Expand Down
Loading

0 comments on commit 2af24b3

Please sign in to comment.