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 value of $HOME of the calling user.
If the given placeholder cannot be translated into an
existing environment variable it will be turned into the
variable name, $HOME in the above example.
  • Loading branch information
schaefi committed Dec 17, 2024
1 parent f75fddd commit 707023d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
10 changes: 9 additions & 1 deletion doc/podman-pilot.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,20 @@ 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
value of $HOME of the calling user. If the given placeholder
cannot be translated into an existing environment variable it
will be turned into the variable name, $HOME in the above
example

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
23 changes: 21 additions & 2 deletions podman-pilot/src/podman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,28 @@ 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 var_pattern = Regex::new(r"%([A-Z]+)").unwrap();
while var_pattern.captures(&arg_value.clone()).is_some() {
for capture in var_pattern.captures_iter(&arg_value.clone()) {
// replace %VAR placeholder(s) with the respective
// environment variable value if possible.
// If not possible replace by the variable name
let var_name = capture.get(1).unwrap().as_str();
let var_value = env::var(var_name)
.unwrap_or(format!("${}", var_name));
arg_value = arg_value.replace(
&format!("%{}", var_name), &var_value
);
}
}
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 707023d

Please sign in to comment.