swiftide_docker_executor/
docker_tool_executor.rs1use std::{collections::HashMap, path::PathBuf};
2use uuid::Uuid;
3
4use crate::{DockerExecutorError, RunningDockerExecutor};
5
6#[derive(Clone, Debug)]
8pub struct DockerExecutor {
9 pub(crate) context_path: PathBuf,
10 pub(crate) image_name: String,
11 pub(crate) dockerfile: Option<PathBuf>,
12 pub(crate) container_uuid: Uuid,
13 pub(crate) user: Option<String>,
14 pub(crate) env_clear: bool,
15 pub(crate) remove_env: Vec<String>,
16 pub(crate) env: HashMap<String, String>,
17 pub(crate) retain_on_drop: bool,
18}
19
20impl Default for DockerExecutor {
21 fn default() -> Self {
22 Self {
23 container_uuid: Uuid::new_v4(),
24 context_path: ".".into(),
25 image_name: "docker-executor".into(),
26 dockerfile: Some("Dockerfile".into()),
27 user: None,
28 env: HashMap::new(),
29 env_clear: false,
30 remove_env: vec![],
31 retain_on_drop: false,
32 }
33 }
34}
35
36impl DockerExecutor {
37 pub fn with_context_path(&mut self, path: impl Into<PathBuf>) -> &mut Self {
39 self.context_path = path.into();
40
41 self
42 }
43
44 pub fn retain_on_drop(&mut self, retain: bool) -> &mut Self {
46 self.retain_on_drop = retain;
47
48 self
49 }
50
51 pub fn clear_env(&mut self) -> &mut Self {
53 self.env_clear = true;
54
55 self
56 }
57
58 pub fn remove_env(&mut self, env: impl Into<String>) -> &mut Self {
60 self.remove_env.push(env.into());
61
62 self
63 }
64
65 pub fn with_env(&mut self, key: impl Into<String>, value: impl Into<String>) -> &mut Self {
67 self.env.insert(key.into(), value.into());
68
69 self
70 }
71
72 pub fn with_envs(&mut self, envs: impl Into<HashMap<String, String>>) -> &mut Self {
74 self.env.extend(envs.into());
75
76 self
77 }
78
79 pub fn with_user(&mut self, user: impl Into<String>) -> &mut Self {
81 self.user = Some(user.into());
82
83 self
84 }
85
86 pub fn with_existing_image(&mut self, path: impl Into<String>) -> &mut Self {
89 self.image_name = path.into();
90
91 self.dockerfile = None;
93
94 self
95 }
96
97 pub fn with_image_name(&mut self, name: impl Into<String>) -> &mut Self {
99 self.image_name = name.into();
100
101 self
102 }
103
104 pub fn with_container_uuid(&mut self, uuid: impl Into<Uuid>) -> &mut Self {
106 self.container_uuid = uuid.into();
107
108 self
109 }
110
111 pub fn with_dockerfile(&mut self, path: impl Into<PathBuf>) -> &mut Self {
113 self.dockerfile = Some(path.into());
114 self
115 }
116
117 pub async fn start(self) -> Result<RunningDockerExecutor, DockerExecutorError> {
121 RunningDockerExecutor::start(&self).await
122 }
123}