Skip to content

Commit

Permalink
Merge pull request #13 from k9withabone/unit-dir
Browse files Browse the repository at this point in the history
Unit directory option flag
  • Loading branch information
k9withabone authored Apr 19, 2023
2 parents e0ff9cf + 3494c9d commit 7f9d1a7
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 11 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Changelog

## [Unreleased]
## [0.1.1] - 2023-04-19

### Added

- A container image of podlet now available on [quay.io](https://quay.io/repository/k9withabone/podlet) and [docker hub](https://hub.docker.com/r/k9withabone/podlet).
- Option flag for outputting to podman unit directory `--unit-directory`.
- Places the generated file in the appropriate directory (i.e. `/etc/containers/systemd`, `~/.config/containers/systemd`) for use by quadlet.

## [0.1.0] - 2023-04-14

Expand All @@ -30,4 +32,4 @@ The initial release of podlet! Designed for podman v4.5.0 and newer.
- Restart=
- [Install]
- WantedBy=
- RequiredBy=
- RequiredBy=
19 changes: 19 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "podlet"
version = "0.1.0"
version = "0.1.1"
authors = ["Paul Nettleton <[email protected]>"]
edition = "2021"
description = "Podlet generates podman quadlet (systemd-like) files from a podman command."
Expand All @@ -14,6 +14,7 @@ categories = ["command-line-utilities"]
clap = { version = "4.2", features = ["derive"] }
color-eyre = "0.6"
ipnet = "2.7"
nix = { version = "0.26", features = ["user"], default-features = false }
shlex = "1.1"
thiserror = "1.0.40"
url = "2.3"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Podlet generates [podman](https://podman.io/) [quadlet](https://docs.podman.io/en/latest/markdown/podman-systemd.unit.5.html) (systemd-like) files from a podman command.

![Made with VHS](https://vhs.charm.sh/vhs-4x04CoFBi5Hj1EZ0zKlSWE.gif)
![Made with VHS](https://vhs.charm.sh/vhs-7GpylCk1SkTulSrL7jp6UV.gif)

## Features

Expand Down Expand Up @@ -44,6 +44,7 @@ Commands:
Options:
-f, --file [<FILE>] Generate a file instead of printing to stdout
-u, --unit-directory Generate a file in the podman unit directory instead of printing to stdout [aliases: unit-dir]
-n, --name <NAME> Override the name of the generated file (without the extension)
-d, --description <DESCRIPTION> Add a description to the unit
--wants <WANTS> Add (weak) requirement dependencies to the unit
Expand Down
63 changes: 56 additions & 7 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use color_eyre::{
eyre::{self, Context},
Help,
};
#[cfg(unix)]
use nix::unistd::Uid;

use self::{
container::Container, install::Install, kube::Kube, network::Network, service::Service,
Expand All @@ -34,21 +36,40 @@ pub struct Cli {
///
/// Optionally provide a path for the file,
/// if no path is provided the file will be placed in the current working directory.
///
/// If not provided, the name of the generated file will be taken from,
/// the `name` parameter for volumes and networks,
/// the filename of the kube file,
/// the container name,
/// or the name of the container image.
#[arg(short, long)]
#[arg(short, long, group = "file_out")]
file: Option<Option<PathBuf>>,

/// Generate a file in the podman unit directory instead of printing to stdout
///
/// Conflicts with the --file option
///
/// Equivalent to `--file $XDG_CONFIG_HOME/containers/systemd/` for non-root users,
/// or `--file /etc/containers/systemd/` for root.
///
/// The name of the file can be specified with the --name option.
#[arg(
short,
long,
visible_alias = "unit-dir",
conflicts_with = "file",
group = "file_out"
)]
unit_directory: bool,

/// Override the name of the generated file (without the extension)
///
/// This only applies if a file was not given to the --file option.
/// This only applies if a file was not given to the --file option,
/// or the --unit-directory option was used.
///
/// E.g. `podlet --file --name hello-world podman run quay.io/podman/hello`
/// will generate a file with the name "hello-world.container".
#[arg(short, long, requires = "file")]
#[arg(short, long, requires = "file_out")]
name: Option<String>,

/// The \[Unit\] section
Expand Down Expand Up @@ -82,11 +103,13 @@ impl Display for Cli {

impl Cli {
pub fn print_or_write_file(&self) -> eyre::Result<()> {
if self.file.is_some() {
if self.unit_directory || self.file.is_some() {
let path = self.file_path()?;
let mut file = File::create(&path)
.wrap_err("Failed to create/open file")
.suggestion("Make sure you have write permissions for the file")?;
.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());
Ok(())
Expand All @@ -98,7 +121,9 @@ impl Cli {

/// Returns the file path for the generated file
fn file_path(&self) -> eyre::Result<Cow<Path>> {
let mut path = if let Some(Some(path)) = &self.file {
let mut path = if self.unit_directory {
unit_dir()
} else if let Some(Some(path)) = &self.file {
if path.is_dir() {
path.clone()
} else {
Expand All @@ -118,6 +143,30 @@ impl Cli {
}
}

#[cfg(unix)]
fn unit_dir() -> PathBuf {
if Uid::current().is_root() {
let path = PathBuf::from("/etc/containers/systemd/");
if path.is_dir() {
path
} else {
PathBuf::from("/usr/share/containers/systemd/")
}
} else {
let mut path: PathBuf = env::var("XDG_CONFIG_HOME")
.or_else(|_| env::var("HOME").map(|home| format!("{home}/.config")))
.unwrap_or_else(|_| String::from("~/.config/"))
.into();
path.push("containers/systemd/");
path
}
}

#[cfg(not(unix))]
fn unit_dir() -> PathBuf {
panic!("Cannot get podman unit directory on non-unix system");
}

#[derive(Subcommand, Debug, Clone, PartialEq)]
enum Commands {
/// Generate a podman quadlet file from a podman command
Expand Down

0 comments on commit 7f9d1a7

Please sign in to comment.