1use std::collections::HashMap;
2
3use anyhow::{Result, bail};
4use oxdock_fs::{CargoScratch, GuardedPath, PolicyPath};
5#[allow(clippy::disallowed_types, clippy::disallowed_methods)]
6use std::process::ExitStatus;
7
8use std::sync::Arc;
9
10#[cfg(not(miri))]
15pub use oxdock_pipe::{OsPipeReader, OsPipeWriter, create_os_pipe};
16pub use oxdock_pipe::{SharedInput, SharedOutput};
17
18#[derive(Clone, Debug)]
22pub struct CommandContext {
23 cwd: PolicyPath,
24 envs: Arc<HashMap<String, String>>,
25 cargo_target_dir: CargoScratch,
26 workspace_root: GuardedPath,
27 build_context: GuardedPath,
28}
29
30impl CommandContext {
31 pub fn new(
32 cwd: &PolicyPath,
33 envs: Arc<HashMap<String, String>>,
34 cargo_target_dir: &CargoScratch,
35 workspace_root: &GuardedPath,
36 build_context: &GuardedPath,
37 ) -> Self {
38 Self {
39 cwd: cwd.clone(),
40 envs,
41 cargo_target_dir: cargo_target_dir.clone(),
42 workspace_root: workspace_root.clone(),
43 build_context: build_context.clone(),
44 }
45 }
46
47 pub fn from_map(
49 cwd: &PolicyPath,
50 envs: &HashMap<String, String>,
51 cargo_target_dir: &CargoScratch,
52 workspace_root: &GuardedPath,
53 build_context: &GuardedPath,
54 ) -> Self {
55 Self::new(
56 cwd,
57 Arc::new(envs.clone()),
58 cargo_target_dir,
59 workspace_root,
60 build_context,
61 )
62 }
63
64 pub fn cwd(&self) -> &PolicyPath {
65 &self.cwd
66 }
67
68 pub fn envs(&self) -> &Arc<HashMap<String, String>> {
69 &self.envs
70 }
71
72 pub fn cargo_target_dir(&self) -> &CargoScratch {
73 &self.cargo_target_dir
74 }
75
76 pub fn workspace_root(&self) -> &GuardedPath {
77 &self.workspace_root
78 }
79
80 pub fn build_context(&self) -> &GuardedPath {
81 &self.build_context
82 }
83}
84
85pub trait BackgroundHandle: Send {
87 fn try_wait(&mut self) -> Result<Option<ExitStatus>>;
88 fn kill(&mut self) -> Result<()>;
89 fn wait(&mut self) -> Result<ExitStatus>;
90}
91
92#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
93pub enum CommandMode {
94 #[default]
95 Foreground,
96 Background,
97}
98
99#[derive(Clone, Default)]
100pub enum CommandStdout {
101 #[default]
102 Inherit,
103 Stream(SharedOutput),
104 Capture,
105 #[cfg(not(miri))]
110 OsPipe(OsPipeWriter),
111}
112
113#[derive(Clone, Default)]
114pub enum CommandStdin {
115 #[default]
117 Null,
118 Inherit,
119 Stream(SharedInput),
120 #[cfg(not(miri))]
123 OsPipe(OsPipeReader),
124}
125
126impl From<Option<SharedInput>> for CommandStdin {
127 fn from(stdin: Option<SharedInput>) -> Self {
128 match stdin {
129 Some(reader) => CommandStdin::Stream(reader),
130 None => CommandStdin::Null,
131 }
132 }
133}
134
135impl CommandStdin {
136 pub fn is_redirected(&self) -> bool {
141 match self {
142 CommandStdin::Stream(_) => true,
143 #[cfg(not(miri))]
144 CommandStdin::OsPipe(_) => true,
145 CommandStdin::Null | CommandStdin::Inherit => false,
146 }
147 }
148}
149
150#[derive(Clone, Default)]
151pub enum CommandStderr {
152 #[default]
153 Inherit,
154 Stream(SharedOutput),
155 #[cfg(not(miri))]
159 OsPipe(OsPipeWriter),
160}
161
162#[derive(Clone, Default)]
163pub struct CommandOptions {
164 pub mode: CommandMode,
165 pub stdin: CommandStdin,
166 pub stdout: CommandStdout,
167 pub stderr: CommandStderr,
168}
169
170impl CommandOptions {
171 pub fn foreground() -> Self {
172 Self::default()
173 }
174
175 pub fn background() -> Self {
176 Self {
177 mode: CommandMode::Background,
178 ..Self::default()
179 }
180 }
181}
182
183pub enum CommandResult<H> {
184 Completed,
185 Captured(Vec<u8>),
186 Background(H),
187}
188
189pub const INHERIT_STDOUT_ENV_VAR: &str = "OXDOCK_INHERIT_STDOUT";
195
196pub const PROCESS_DEBUG_ENV_VAR: &str = "OXBOOK_DEBUG";
200
201pub trait ProcessManager: Clone + Send + 'static {
206 type Handle: BackgroundHandle + Clone + Send + 'static;
207
208 fn run_command(
209 &mut self,
210 ctx: &CommandContext,
211 script: &str,
212 options: CommandOptions,
213 ) -> Result<CommandResult<Self::Handle>>;
214
215 fn spawn_command(
219 &mut self,
220 ctx: &CommandContext,
221 script: &str,
222 options: CommandOptions,
223 ) -> Result<CommandResult<Self::Handle>> {
224 self.run_command(ctx, script, options)
225 }
226
227 fn run_argv(
232 &mut self,
233 _ctx: &CommandContext,
234 argv: &[String],
235 _options: CommandOptions,
236 ) -> Result<CommandResult<Self::Handle>> {
237 bail!("run_argv not implemented for argv {argv:?}")
238 }
239
240 fn stdin_is_terminal(&self) -> bool {
244 use std::io::IsTerminal;
245 std::io::stdin().is_terminal()
246 }
247
248 fn spawn_argv(
251 &mut self,
252 ctx: &CommandContext,
253 argv: &[String],
254 options: CommandOptions,
255 ) -> Result<CommandResult<Self::Handle>> {
256 self.run_argv(ctx, argv, options)
257 }
258}