Skip to content

Commit 09bd985

Browse files
cvgaviaofjtirado
andauthored
Support RunShell's directory property from spec (#1696)
* Support RunShell's directory property from spec open-workflow-specification/specification#1191 Signed-off-by: Cristiano Gavião <cvgaviao@gmail.com> * Review comments Signed-off-by: Francisco Javier Tirado Sarti <ftirados@ibm.com> --------- Signed-off-by: Cristiano Gavião <cvgaviao@gmail.com> Signed-off-by: Francisco Javier Tirado Sarti <ftirados@ibm.com> Co-authored-by: Francisco Javier Tirado Sarti <ftirados@ibm.com>
1 parent c8f334a commit 09bd985

5 files changed

Lines changed: 52 additions & 4 deletions

File tree

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

Lines changed: 16 additions & 1 deletion
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,18 +33,29 @@
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,
4143
Optional<WorkflowValueResolver<Map<String, Object>>> shellEnv,
42-
Optional<ProcessReturnType> returnType) {
44+
Optional<ProcessReturnType> returnType,
45+
Optional<WorkflowValueResolver<String>> shellDirectory) {
4346
this.shellCommand = shellCommand;
4447
this.shellArguments = shellArguments;
4548
this.shellEnv = shellEnv;
4649
this.returnType = returnType;
50+
this.shellDirectory = shellDirectory;
51+
}
52+
53+
public RunShellExecutor(
54+
WorkflowValueResolver<String> shellCommand,
55+
List<WorkflowValueResolver<String>> shellArguments,
56+
Optional<WorkflowValueResolver<Map<String, Object>>> shellEnv,
57+
Optional<ProcessReturnType> returnType) {
58+
this(shellCommand, shellArguments, shellEnv, returnType, Optional.empty());
4759
}
4860

4961
@Override
@@ -64,6 +76,9 @@ public CompletableFuture<WorkflowModel> apply(
6476
shellArguments.forEach(f -> commandAndArgs.add(f.apply(workflowContext, taskContext, model)));
6577

6678
ProcessBuilder builder = new ProcessBuilder(commandAndArgs);
79+
shellDirectory.ifPresent(
80+
directory ->
81+
builder.directory(new File(directory.apply(workflowContext, taskContext, model))));
6782
shellEnv.ifPresent(
6883
map -> ScriptUtils.addEnviromment(builder, map.apply(workflowContext, taskContext, model)));
6984

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ public CallableTask build(RunShell taskConfiguration, WorkflowDefinition definit
4444
WorkflowUtils.buildMapResolver(
4545
definition.application(), shell.getEnvironment().getAdditionalProperties()))
4646
: Optional.empty(),
47-
taskConfiguration.isAwait()
48-
? Optional.of(taskConfiguration.getReturn())
47+
taskConfiguration.isAwait() ? Optional.of(taskConfiguration.getReturn()) : Optional.empty(),
48+
shell.getDirectory() != null
49+
? Optional.of(
50+
WorkflowUtils.buildStringFilter(definition.application(), shell.getDirectory()))
4951
: Optional.empty());
5052
}
5153

‎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)