Skip to main content

sal_virt/buildah/
containers.rs

1use super::BuildahError;
2use crate::buildah::execute_buildah_command;
3use sal_process::CommandResult;
4
5/// Create a container from an image
6pub fn from(image: &str) -> Result<CommandResult, BuildahError> {
7    execute_buildah_command(&["from", image])
8}
9
10/// Run a command in a container
11///
12/// # Arguments
13///
14/// * `container` - The container ID or name
15/// * `command` - The command to run
16pub fn run(container: &str, command: &str) -> Result<CommandResult, BuildahError> {
17    execute_buildah_command(&["run", container, "sh", "-c", command])
18}
19
20/// Run a command in a container with specified isolation
21///
22/// # Arguments
23///
24/// * `container` - The container ID or name
25/// * `command` - The command to run
26/// * `isolation` - Isolation method (e.g., "chroot", "rootless", "oci")
27pub fn bah_run_with_isolation(
28    container: &str,
29    command: &str,
30    isolation: &str,
31) -> Result<CommandResult, BuildahError> {
32    execute_buildah_command(&[
33        "run",
34        "--isolation",
35        isolation,
36        container,
37        "sh",
38        "-c",
39        command,
40    ])
41}
42
43/// Copy files into a container
44pub fn bah_copy(container: &str, source: &str, dest: &str) -> Result<CommandResult, BuildahError> {
45    execute_buildah_command(&["copy", container, source, dest])
46}
47
48pub fn bah_add(container: &str, source: &str, dest: &str) -> Result<CommandResult, BuildahError> {
49    execute_buildah_command(&["add", container, source, dest])
50}
51
52/// Commit a container to an image
53pub fn bah_commit(container: &str, image_name: &str) -> Result<CommandResult, BuildahError> {
54    execute_buildah_command(&["commit", container, image_name])
55}
56
57/// Remove a container
58pub fn bah_remove(container: &str) -> Result<CommandResult, BuildahError> {
59    execute_buildah_command(&["rm", container])
60}
61
62/// List containers
63pub fn bah_list() -> Result<CommandResult, BuildahError> {
64    execute_buildah_command(&["containers"])
65}
66
67/// Build an image from a Containerfile/Dockerfile
68///
69/// # Arguments
70///
71/// * `tag` - Optional tag for the image (e.g., "my-app:latest")
72/// * `context_dir` - The directory containing the Containerfile/Dockerfile (usually ".")
73/// * `file` - Optional path to a specific Containerfile/Dockerfile
74/// * `isolation` - Optional isolation method (e.g., "chroot", "rootless", "oci")
75pub fn bah_build(
76    tag: Option<&str>,
77    context_dir: &str,
78    file: &str,
79    isolation: Option<&str>,
80) -> Result<CommandResult, BuildahError> {
81    let mut args = Vec::new();
82    args.push("build");
83
84    if let Some(tag_value) = tag {
85        args.push("-t");
86        args.push(tag_value);
87    }
88
89    if let Some(isolation_value) = isolation {
90        args.push("--isolation");
91        args.push(isolation_value);
92    }
93
94    args.push("-f");
95    args.push(file);
96
97    args.push(context_dir);
98
99    execute_buildah_command(&args)
100}