|
23 | 23 | import java.util.Comparator; |
24 | 24 | import java.util.HashSet; |
25 | 25 | import java.util.List; |
| 26 | +import java.util.Map; |
26 | 27 | import java.util.Objects; |
27 | 28 | import java.util.Optional; |
28 | 29 | import java.util.Set; |
@@ -195,6 +196,23 @@ public ExecResult exec(RuntimeContext runtimeContext, String command, Integer ti |
195 | 196 | return doExec(runtimeContext, command, timeout); |
196 | 197 | } |
197 | 198 |
|
| 199 | + /** |
| 200 | + * Delegates to {@link #doExec(RuntimeContext, String, int, Map)} with a fallback timeout. The |
| 201 | + * default {@code doExec} wraps the command in a one-shot shell {@code env} prefix; backends with |
| 202 | + * a native per-command environment (Docker's {@code docker exec -e}) override that method so |
| 203 | + * values never enter the command text. |
| 204 | + */ |
| 205 | + @Override |
| 206 | + public ExecResult exec( |
| 207 | + RuntimeContext runtimeContext, |
| 208 | + String command, |
| 209 | + Integer timeoutSeconds, |
| 210 | + Map<String, String> environment) |
| 211 | + throws Exception { |
| 212 | + int timeout = timeoutSeconds != null ? timeoutSeconds : getDefaultExecTimeoutSeconds(); |
| 213 | + return doExec(runtimeContext, command, timeout, environment); |
| 214 | + } |
| 215 | + |
198 | 216 | @Override |
199 | 217 | public InputStream persistWorkspace() throws Exception { |
200 | 218 | return doPersistWorkspace(); |
@@ -261,6 +279,35 @@ protected int getDefaultExecTimeoutSeconds() { |
261 | 279 | protected abstract ExecResult doExec( |
262 | 280 | RuntimeContext runtimeContext, String command, int timeoutSeconds) throws Exception; |
263 | 281 |
|
| 282 | + /** |
| 283 | + * Executes a shell command with a one-shot environment. The base implementation wraps the |
| 284 | + * command in {@code env 'NAME=VALUE' ... sh -c 'command'}, which works for every POSIX shell |
| 285 | + * backend; backends with native support override this so the values are not part of the command |
| 286 | + * text. |
| 287 | + * |
| 288 | + * @param runtimeContext per-call context; may be {@code null} for internal probes |
| 289 | + * @param command shell command string |
| 290 | + * @param timeoutSeconds maximum execution time |
| 291 | + * @param environment extra environment variables for this command only; {@code null} or empty |
| 292 | + * for none |
| 293 | + * @return execution result |
| 294 | + * @throws Exception if execution fails |
| 295 | + */ |
| 296 | + protected ExecResult doExec( |
| 297 | + RuntimeContext runtimeContext, |
| 298 | + String command, |
| 299 | + int timeoutSeconds, |
| 300 | + Map<String, String> environment) |
| 301 | + throws Exception { |
| 302 | + if (environment == null || environment.isEmpty()) { |
| 303 | + return doExec(runtimeContext, command, timeoutSeconds); |
| 304 | + } |
| 305 | + return doExec( |
| 306 | + runtimeContext, |
| 307 | + SandboxEnvironment.commandWithEnvironment(command, environment), |
| 308 | + timeoutSeconds); |
| 309 | + } |
| 310 | + |
264 | 311 | /** |
265 | 312 | * Creates a tar archive of the current workspace contents. |
266 | 313 | * |
|
0 commit comments