Skip to content

Commit

Permalink
feat: don't overwrite existing files by default
Browse files Browse the repository at this point in the history
  • Loading branch information
k9withabone committed Apr 22, 2023
1 parent 42b5e8d commit 386a2cf
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
### Added

- Check for existing unit files with the same name as the service generated by quadlet from the podlet generated quadlet file and throw an error if there is a conflict (#14).
- Use `--skip-services-check` to opt out.

### Changed

- Breaking: files are no longer overwritten by default, added `--overwrite` flag if overwriting is desired.

## [0.1.1] - 2023-04-19

Expand Down
2 changes: 1 addition & 1 deletion 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
@@ -1,6 +1,6 @@
[package]
name = "podlet"
version = "0.1.1"
version = "0.2.0-alpha.1"
authors = ["Paul Nettleton <[email protected]>"]
edition = "2021"
description = "Podlet generates podman quadlet (systemd-like) files from a podman command."
Expand Down
46 changes: 33 additions & 13 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use std::{
ffi::OsStr,
fmt::Display,
fs::File,
io::Write,
io::{self, Write},
path::{Path, PathBuf},
};

use clap::{Parser, Subcommand};
use color_eyre::{
eyre::{self, Context},
Help,
Help, Report,
};

use self::{
Expand Down Expand Up @@ -74,14 +74,20 @@ pub struct Cli {
#[arg(short, long, requires = "file_out")]
name: Option<String>,

/// Overwrite existing files when generating a file
///
/// By default, podlet will return an error if a file already exists at the given location.
#[arg(long, alias = "override", requires = "file_out")]
overwrite: bool,

/// Skip the check for existing services of the same name
///
/// By default, podlet will check for existing services with the same name as
/// the service quadlet will generate from the generated quadlet file
/// and return an error if a conflict is found.
/// This option will cause podlet to skip that check.
#[arg(long, requires = "file_out")]
skip_check_existing: bool,
skip_services_check: bool,

/// The \[Unit\] section
#[command(flatten)]
Expand Down Expand Up @@ -116,13 +122,27 @@ impl Cli {
pub fn print_or_write_file(&self) -> eyre::Result<()> {
if self.unit_directory || self.file.is_some() {
let path = self.file_path()?;
let mut file = File::create(&path)
.wrap_err_with(|| format!("Failed to create/open file: {}", path.display()))
.suggestion(
"Make sure the directory exists and you have write permissions for the file",
)?;
write!(file, "{self}").wrap_err("Failed to write to file")?;
println!("Wrote to file: {}", path.display());
let path_display = path.display();
let mut file = File::options()
.write(true)
.create_new(!self.overwrite)
.create(self.overwrite)
.open(&path)
.map_err(|error| match error.kind() {
io::ErrorKind::AlreadyExists => {
eyre::eyre!("File already exists, not overwriting it: {path_display}")
.suggestion("Use `--overwrite` if you wish overwrite existing files.")
}
_ => Report::new(error)
.wrap_err(format!("Failed to create/open file: {path_display}"))
.suggestion(
"Make sure the directory exists \
and you have write permissions for the file",
),
})?;
write!(file, "{self}")
.wrap_err_with(|| format!("Failed to write to file: {path_display}"))?;
println!("Wrote to file: {path_display}");
Ok(())
} else {
print!("{self}");
Expand Down Expand Up @@ -180,18 +200,18 @@ impl Cli {

fn check_existing(&self, name: &str) -> eyre::Result<()> {
#[cfg(unix)]
if !self.skip_check_existing {
if !self.skip_services_check {
if let Ok(unit_files) = systemd_dbus::unit_files() {
let Commands::Podman { command } = &self.command;
let service = command.name_to_service(name);
for systemd_dbus::UnitFile { file_name, status } in unit_files {
if file_name.contains(&service) {
if !(self.overwrite && status == "generated") && file_name.contains(&service) {
return Err(eyre::eyre!(
"File name `{name}` conflicts with existing unit file: {file_name}"
)
.suggestion(
"Change the generated file's name with `--file` or `--name`. \
Alternatively, use the `--skip-check-existing` option if this is ok",
Alternatively, use `--skip-services-check` if this is ok.",
));
}
}
Expand Down

0 comments on commit 386a2cf

Please sign in to comment.