swiftide_docker_executor/
docker_tool_executor.rs

1use std::path::PathBuf;
2use uuid::Uuid;
3
4use crate::{DockerExecutorError, RunningDockerExecutor};
5
6/// Build a docker image with bollard and start it up
7#[derive(Clone, Debug)]
8pub struct DockerExecutor {
9    context_path: PathBuf,
10    image_name: String,
11    dockerfile: PathBuf,
12    container_uuid: Uuid,
13}
14
15impl Default for DockerExecutor {
16    fn default() -> Self {
17        Self {
18            container_uuid: Uuid::new_v4(),
19            context_path: ".".into(),
20            image_name: "docker-executor".into(),
21            dockerfile: "Dockerfile".into(),
22        }
23    }
24}
25
26impl DockerExecutor {
27    /// Set the path to build the context from (default ".")
28    pub fn with_context_path(&mut self, path: impl Into<PathBuf>) -> &mut Self {
29        self.context_path = path.into();
30
31        self
32    }
33
34    /// Set the name of the image to build (default "docker-executor")
35    pub fn with_image_name(&mut self, name: impl Into<String>) -> &mut Self {
36        self.image_name = name.into();
37
38        self
39    }
40
41    /// Overwrite the uuid that is added as suffix to the running container
42    pub fn with_container_uuid(&mut self, uuid: impl Into<Uuid>) -> &mut Self {
43        self.container_uuid = uuid.into();
44
45        self
46    }
47
48    /// Overwrite the dockerfile to use (default "Dockerfile")
49    pub fn with_dockerfile(&mut self, path: impl Into<PathBuf>) -> &mut Self {
50        self.dockerfile = path.into();
51        self
52    }
53
54    /// Starts the docker executor
55    ///
56    /// Note that on dropping the `RunningDockerExecutor`, the container will be stopped
57    pub async fn start(self) -> Result<RunningDockerExecutor, DockerExecutorError> {
58        RunningDockerExecutor::start(
59            self.container_uuid,
60            &self.context_path,
61            &self.dockerfile,
62            &self.image_name,
63        )
64        .await
65    }
66}