Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.serverlessworkflow.impl.WorkflowModel;
import io.serverlessworkflow.impl.WorkflowValueResolver;
import io.serverlessworkflow.impl.scripts.ScriptUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -32,16 +33,19 @@
public class RunShellExecutor implements CallableTask {
private final WorkflowValueResolver<String> shellCommand;
private final List<WorkflowValueResolver<String>> shellArguments;
private final Optional<WorkflowValueResolver<String>> shellDirectory;
private final Optional<WorkflowValueResolver<Map<String, Object>>> shellEnv;
private final Optional<ProcessReturnType> returnType;

public RunShellExecutor(
WorkflowValueResolver<String> shellCommand,
List<WorkflowValueResolver<String>> shellArguments,
Optional<WorkflowValueResolver<String>> shellDirectory,
Optional<WorkflowValueResolver<Map<String, Object>>> shellEnv,
Optional<ProcessReturnType> returnType) {
this.shellCommand = shellCommand;
this.shellArguments = shellArguments;
this.shellDirectory = shellDirectory;
this.shellEnv = shellEnv;
this.returnType = returnType;
}
Expand All @@ -64,6 +68,9 @@ public CompletableFuture<WorkflowModel> apply(
shellArguments.forEach(f -> commandAndArgs.add(f.apply(workflowContext, taskContext, model)));

ProcessBuilder builder = new ProcessBuilder(commandAndArgs);
shellDirectory.ifPresent(
directory ->
builder.directory(new File(directory.apply(workflowContext, taskContext, model))));
shellEnv.ifPresent(
map -> ScriptUtils.addEnviromment(builder, map.apply(workflowContext, taskContext, model)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public CallableTask build(RunShell taskConfiguration, WorkflowDefinition definit
.map(s -> WorkflowUtils.buildStringFilter(definition.application(), s))
.toList()
: List.of(),
shell.getDirectory() != null
? Optional.of(
WorkflowUtils.buildStringFilter(definition.application(), shell.getDirectory()))
: Optional.empty(),
shell.getEnvironment() != null
? Optional.of(
WorkflowUtils.buildMapResolver(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class RunShellExecutorTest {

@BeforeAll
static void init() {
appl = WorkflowApplication.builder().withAllowedCommands(List.of("ls", "echo")).build();
appl = WorkflowApplication.builder().withAllowedCommands(List.of("ls", "echo", "pwd")).build();
}

@AfterAll
Expand Down Expand Up @@ -89,6 +89,21 @@ void testEchoWithJqExpression() throws IOException {
});
}

@Test
void testDirectory() throws IOException {
Workflow workflow =
WorkflowReader.readWorkflowFromClasspath("workflows-samples/run-shell/pwd-directory.yaml");
WorkflowModel model = appl.workflowDefinition(workflow).instance(Map.of()).start().join();

SoftAssertions.assertSoftly(
softly -> {
ProcessResult result = model.as(ProcessResult.class).orElseThrow();
softly.assertThat(result.code()).isEqualTo(0);
softly.assertThat(result.stdout().trim()).isEqualTo("/tmp");
softly.assertThat(result.stderr()).isEmpty();
});
}

@Test
void testMissingShellCommand() throws IOException {
Workflow workflow =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
document:
dsl: '1.0.1'
namespace: test
name: pwd-directory
version: '0.1.0'
do:
- runShell:
run:
shell:
command: pwd
directory: /tmp
return: all
4 changes: 4 additions & 0 deletions types/src/main/resources/schema/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,10 @@ $defs:
description: A list of the arguments, if any, of the shell command to run.
items:
type: string
directory:
type: string
title: ShellDirectory
description: The working directory, if any, in which to run the shell command.
environment:
type: object
title: ShellEnvironment
Expand Down