swiftide_docker_executor/
docker_tool_executor.rs1use std::{collections::HashMap, path::PathBuf, time::Duration};
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 pub(crate) default_timeout: Option<Duration>,
19 pub(crate) workdir: PathBuf,
20}
21
22impl Default for DockerExecutor {
23 fn default() -> Self {
24 Self {
25 container_uuid: Uuid::new_v4(),
26 context_path: ".".into(),
27 image_name: "docker-executor".into(),
28 dockerfile: Some("Dockerfile".into()),
29 user: None,
30 env: HashMap::new(),
31 env_clear: false,
32 remove_env: vec![],
33 retain_on_drop: false,
34 default_timeout: None,
35 workdir: "/app".into(),
36 }
37 }
38}
39
40impl DockerExecutor {
41 pub fn with_context_path(&mut self, path: impl Into<PathBuf>) -> &mut Self {
43 self.context_path = path.into();
44
45 self
46 }
47
48 pub fn with_workdir(&mut self, path: impl Into<PathBuf>) -> &mut Self {
50 self.workdir = path.into();
51
52 self
53 }
54
55 pub fn retain_on_drop(&mut self, retain: bool) -> &mut Self {
57 self.retain_on_drop = retain;
58
59 self
60 }
61
62 pub fn clear_env(&mut self) -> &mut Self {
64 self.env_clear = true;
65
66 self
67 }
68
69 pub fn remove_env(&mut self, env: impl Into<String>) -> &mut Self {
71 self.remove_env.push(env.into());
72
73 self
74 }
75
76 pub fn with_env(&mut self, key: impl Into<String>, value: impl Into<String>) -> &mut Self {
78 self.env.insert(key.into(), value.into());
79
80 self
81 }
82
83 pub fn with_envs(&mut self, envs: impl Into<HashMap<String, String>>) -> &mut Self {
85 self.env.extend(envs.into());
86
87 self
88 }
89
90 pub fn with_default_timeout(&mut self, timeout: Duration) -> &mut Self {
92 self.default_timeout = Some(timeout);
93
94 self
95 }
96
97 pub fn clear_default_timeout(&mut self) -> &mut Self {
99 self.default_timeout = None;
100
101 self
102 }
103
104 pub fn with_user(&mut self, user: impl Into<String>) -> &mut Self {
106 self.user = Some(user.into());
107
108 self
109 }
110
111 pub fn with_existing_image(&mut self, path: impl Into<String>) -> &mut Self {
114 self.image_name = path.into();
115
116 self.dockerfile = None;
118
119 self
120 }
121
122 pub fn with_image_name(&mut self, name: impl Into<String>) -> &mut Self {
124 self.image_name = name.into();
125
126 self
127 }
128
129 pub fn with_container_uuid(&mut self, uuid: impl Into<Uuid>) -> &mut Self {
131 self.container_uuid = uuid.into();
132
133 self
134 }
135
136 pub fn with_dockerfile(&mut self, path: impl Into<PathBuf>) -> &mut Self {
138 self.dockerfile = Some(path.into());
139 self
140 }
141
142 pub async fn start(self) -> Result<RunningDockerExecutor, DockerExecutorError> {
146 RunningDockerExecutor::start(&self).await
147 }
148}