Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow env placeholders for the podman pilot #57

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading