Skip to content

Commit 88fc756

Browse files
committed
Support RunShell's directory property from spec
open-workflow-specification/specification#1191 Signed-off-by: Cristiano Gavião <cvgaviao@gmail.com>
1 parent a269eee commit 88fc756

5 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎impl/core/src/main/java/io/serverlessworkflow/impl/executors/RunShellExecutor.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.serverlessworkflow.impl.WorkflowModel;
2424
import io.serverlessworkflow.impl.WorkflowValueResolver;
2525
import io.serverlessworkflow.impl.scripts.ScriptUtils;
26+
import java.io.File;
2627
import java.util.ArrayList;
2728
import java.util.List;
2829
import java.util.Map;
@@ -32,16 +33,19 @@
3233
public class RunShellExecutor implements CallableTask {
3334
private final WorkflowValueResolver<String> shellCommand;
3435
private final List<WorkflowValueResolver<String>> shellArguments;
36+
private final Optional<WorkflowValueResolver<String>> shellDirectory;
3537
private final Optional<WorkflowValueResolver<Map<String, Object>>> shellEnv;
3638
private final Optional<ProcessReturnType> returnType;
3739

3840
public RunShellExecutor(
3941
WorkflowValueResolver<String> shellCommand,
4042
List<WorkflowValueResolver<String>> shellArguments,
43+
Optional<WorkflowValueResolver<String>> shellDirectory,
4144
Optional<WorkflowValueResolver<Map<String, Object>>> shellEnv,
4245
Optional<ProcessReturnType> returnType) {
4346
this.shellCommand = shellCommand;
4447
this.shellArguments = shellArguments;
48+
this.shellDirectory = shellDirectory;
4549
this.shellEnv = shellEnv;
4650
this.returnType = returnType;
4751
}
@@ -64,6 +68,9 @@ public CompletableFuture<WorkflowModel> apply(
6468
shellArguments.forEach(f -> commandAndArgs.add(f.apply(workflowContext, taskContext, model)));
6569

6670
ProcessBuilder builder = new ProcessBuilder(commandAndArgs);
71+
shellDirectory.ifPresent(
72+
directory ->
73+
builder.directory(new File(directory.apply(workflowContext, taskContext, model))));
6774
shellEnv.ifPresent(
6875
map -> ScriptUtils.addEnviromment(builder, map.apply(workflowContext, taskContext, model)));
6976

‎impl/core/src/main/java/io/serverlessworkflow/impl/executors/RunShellExecutorBuilder.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public CallableTask build(RunShell taskConfiguration, WorkflowDefinition definit
3939
.map(s -> WorkflowUtils.buildStringFilter(definition.application(), s))
4040
.toList()
4141
: List.of(),
42+
shell.getDirectory() != null
43+
? Optional.of(
44+
WorkflowUtils.buildStringFilter(definition.application(), shell.getDirectory()))
45+
: Optional.empty(),
4246
shell.getEnvironment() != null
4347
? Optional.of(
4448
WorkflowUtils.buildMapResolver(

‎impl/test/src/test/java/io/serverlessworkflow/impl/test/RunShellExecutorTest.java‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class RunShellExecutorTest {
3939

4040
@BeforeAll
4141
static void init() {
42-
appl = WorkflowApplication.builder().withAllowedCommands(List.of("ls", "echo")).build();
42+
appl = WorkflowApplication.builder().withAllowedCommands(List.of("ls", "echo", "pwd")).build();
4343
}
4444

4545
@AfterAll
@@ -89,6 +89,21 @@ void testEchoWithJqExpression() throws IOException {
8989
});
9090
}
9191

92+
@Test
93+
void testDirectory() throws IOException {
94+
Workflow workflow =
95+
WorkflowReader.readWorkflowFromClasspath("workflows-samples/run-shell/pwd-directory.yaml");
96+
WorkflowModel model = appl.workflowDefinition(workflow).instance(Map.of()).start().join();
97+
98+
SoftAssertions.assertSoftly(
99+
softly -> {
100+
ProcessResult result = model.as(ProcessResult.class).orElseThrow();
101+
softly.assertThat(result.code()).isEqualTo(0);
102+
softly.assertThat(result.stdout().trim()).isEqualTo("/tmp");
103+
softly.assertThat(result.stderr()).isEmpty();
104+
});
105+
}
106+
92107
@Test
93108
void testMissingShellCommand() throws IOException {
94109
Workflow workflow =
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
document:
2+
dsl: '1.0.1'
3+
namespace: test
4+
name: pwd-directory
5+
version: '0.1.0'
6+
do:
7+
- runShell:
8+
run:
9+
shell:
10+
command: pwd
11+
directory: /tmp
12+
return: all

‎types/src/main/resources/schema/workflow.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,10 @@ $defs:
928928
description: A list of the arguments, if any, of the shell command to run.
929929
items:
930930
type: string
931+
directory:
932+
type: string
933+
title: ShellDirectory
934+
description: The working directory, if any, in which to run the shell command.
931935
environment:
932936
type: object
933937
title: ShellEnvironment

0 commit comments

Comments
 (0)