Skip to content

Commit

Permalink
Allow env placeholders for the podman pilot
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
schaefi committed Dec 17, 2024
1 parent f75fddd commit 892e3d0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
9 changes: 8 additions & 1 deletion doc/podman-pilot.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions podman-pilot/src/podman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down

0 comments on commit 892e3d0

Please sign in to comment.