From 892e3d00b35bf668b21fa3ba2171912b0a476d5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20Sch=C3=A4fer?= Date: Tue, 17 Dec 2024 10:19:53 +0100 Subject: [PATCH] Allow env placeholders for the podman pilot The podman runtime arguments allows to set environment variable placeholders starting with '%' and followed by the name of the environment variable. For example %HOME will be replaced to the home directory $HOME of the calling user. If the given placeholder cannot be translated into an existing environment variable it will stay as placeholder. --- doc/podman-pilot.rst | 9 ++++++++- podman-pilot/src/podman.rs | 20 ++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/doc/podman-pilot.rst b/doc/podman-pilot.rst index d22bad4..58fab1b 100644 --- a/doc/podman-pilot.rst +++ b/doc/podman-pilot.rst @@ -116,12 +116,19 @@ can be set for the supported container engine: - -ti After reading of the app configuration information the application -will be called using the configured engine. If no runtime +will be called using the configured engine. If no podman runtime arguments exists, the following defaults will apply: - The instance will be removed after the call - The instance allows for interactive shell sessions +The podman runtime arguments allows to set environment variable +placeholders starting with '%' and followed by the name of the +environment variable. For example %HOME will be replaced to the +home directory $HOME of the calling user. If the given placeholder +cannot be translated into an existing environment variable it +will stay as placeholder. + All caller arguments will be passed to the program call inside of the instance except for arguments that starts with the '@' or '%' sign. Caller arguments of this type are only used for diff --git a/podman-pilot/src/podman.rs b/podman-pilot/src/podman.rs index 9234b1e..d65f77e 100644 --- a/podman-pilot/src/podman.rs +++ b/podman-pilot/src/podman.rs @@ -182,9 +182,25 @@ pub fn create( } // create the container with configured runtime arguments - let has_runtime_args = podman.as_ref().map(|p| !p.is_empty()).unwrap_or_default(); - app.args(podman.iter().flatten().flat_map(|x| x.splitn(2, ' '))); + for arg in podman.iter().flatten().flat_map(|x| x.splitn(2, ' ')) { + let mut arg_value = arg.to_string(); + let re = Regex::new(r".*%([A-Z+]).*").unwrap(); + for var_name in re.capture_names() { + // replace %VAR placeholder(s) into their respective environment + // variable if possible. If not possible keep the placeholder + // as it is. For example %HOME will be replaced to the + // home directory $HOME of the calling user + if Some(var_name).is_some() { + arg_value = env::var(var_name.unwrap()) + .unwrap_or(format!("%{}", var_name.unwrap())); + } + } + app.arg(arg_value); + }; + // set default runtime arguments if none configured + let has_runtime_args = podman + .as_ref().map(|p| !p.is_empty()).unwrap_or_default(); if !has_runtime_args { app.arg("--tty").arg("--interactive"); }