-
Couldn't load subscription status.
- Fork 49
[Fix #898] RunShell minor improvements #910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,8 +53,8 @@ private interface ProcessBuilderSupplier { | |
| public CompletableFuture<WorkflowModel> apply( | ||
| WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) { | ||
| ProcessBuilder processBuilder = this.processBuilderSupplier.apply(workflowContext, taskContext); | ||
| return CompletableFuture.completedFuture( | ||
| this.shellResultSupplier.apply(taskContext, input, processBuilder)); | ||
| return CompletableFuture.supplyAsync( | ||
| () -> this.shellResultSupplier.apply(taskContext, input, processBuilder)); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -69,48 +69,44 @@ public void init(RunShell taskConfiguration, WorkflowDefinition definition) { | |
| (workflowContext, taskContext) -> { | ||
| WorkflowApplication application = definition.application(); | ||
|
|
||
| String command = | ||
| ExpressionUtils.isExpr(shellCommand) | ||
| ? WorkflowUtils.buildStringResolver( | ||
| application, shellCommand, taskContext.input().asJavaObject()) | ||
| .apply(workflowContext, taskContext, taskContext.input()) | ||
| : shellCommand; | ||
|
|
||
| StringBuilder commandBuilder = new StringBuilder(command); | ||
| StringBuilder commandBuilder = | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just saving a variable declaration |
||
| new StringBuilder( | ||
| ExpressionUtils.isExpr(shellCommand) | ||
| ? WorkflowUtils.buildStringResolver( | ||
| application, shellCommand, taskContext.input().asJavaObject()) | ||
| .apply(workflowContext, taskContext, taskContext.input()) | ||
| : shellCommand); | ||
|
|
||
| if (shell.getArguments() != null | ||
| && shell.getArguments().getAdditionalProperties() != null) { | ||
| for (Map.Entry<String, Object> entry : | ||
| shell.getArguments().getAdditionalProperties().entrySet()) { | ||
|
|
||
| String argKey = | ||
| ExpressionUtils.isExpr(entry.getKey()) | ||
| ? WorkflowUtils.buildStringResolver( | ||
| application, entry.getKey(), taskContext.input().asJavaObject()) | ||
| .apply(workflowContext, taskContext, taskContext.input()) | ||
| : entry.getKey(); | ||
|
|
||
| if (entry.getValue() == null) { | ||
| commandBuilder.append(" ").append(argKey); | ||
| continue; | ||
| commandBuilder | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to avoid the use of continue, which in this case was possible |
||
| .append(" ") | ||
| .append( | ||
| ExpressionUtils.isExpr(entry.getKey()) | ||
| ? WorkflowUtils.buildStringResolver( | ||
| application, entry.getKey(), taskContext.input().asJavaObject()) | ||
| .apply(workflowContext, taskContext, taskContext.input()) | ||
| : entry.getKey()); | ||
| if (entry.getValue() != null) { | ||
|
|
||
| commandBuilder | ||
| .append("=") | ||
| .append( | ||
| ExpressionUtils.isExpr(entry.getValue()) | ||
| ? WorkflowUtils.buildStringResolver( | ||
| application, | ||
| entry.getValue().toString(), | ||
| taskContext.input().asJavaObject()) | ||
| .apply(workflowContext, taskContext, taskContext.input()) | ||
| : entry.getValue().toString()); | ||
| } | ||
|
|
||
| String argValue = | ||
| ExpressionUtils.isExpr(entry.getValue()) | ||
| ? WorkflowUtils.buildStringResolver( | ||
| application, | ||
| entry.getValue().toString(), | ||
| taskContext.input().asJavaObject()) | ||
| .apply(workflowContext, taskContext, taskContext.input()) | ||
| : entry.getValue().toString(); | ||
|
|
||
| commandBuilder.append(" ").append(argKey).append("=").append(argValue); | ||
| } | ||
| } | ||
|
|
||
| // TODO: support Windows cmd.exe | ||
| ProcessBuilder builder = new ProcessBuilder("sh", "-c", commandBuilder.toString()); | ||
|
|
||
| if (shell.getEnvironment() != null | ||
| && shell.getEnvironment().getAdditionalProperties() != null) { | ||
| for (Map.Entry<String, Object> entry : | ||
|
|
@@ -128,21 +124,16 @@ public void init(RunShell taskConfiguration, WorkflowDefinition definition) { | |
| builder.environment().put(entry.getKey(), value); | ||
| } | ||
| } | ||
|
|
||
| return builder; | ||
| }; | ||
|
|
||
| this.shellResultSupplier = | ||
| (taskContext, input, processBuilder) -> { | ||
| try { | ||
| Process process = processBuilder.start(); | ||
|
|
||
| if (taskConfiguration.isAwait()) { | ||
| return buildResultFromProcess(taskConfiguration, definition, process); | ||
| } else { | ||
| return input; | ||
| } | ||
|
|
||
| return taskConfiguration.isAwait() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, this one was not needed, but I love ternary operator and could not refrain, sorry ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😆 |
||
| ? buildResultFromProcess(taskConfiguration, definition, process) | ||
| : input; | ||
| } catch (IOException | InterruptedException e) { | ||
| throw new WorkflowException(WorkflowError.runtime(taskContext, e).build(), e); | ||
| } | ||
|
|
@@ -156,17 +147,12 @@ public void init(RunShell taskConfiguration, WorkflowDefinition definition) { | |
| private WorkflowModel buildResultFromProcess( | ||
| RunShell taskConfiguration, WorkflowDefinition definition, Process process) | ||
| throws IOException, InterruptedException { | ||
|
|
||
| int exitCode = process.waitFor(); | ||
|
|
||
| String stdout = new String(process.getInputStream().readAllBytes(), StandardCharsets.UTF_8); | ||
| String stderr = new String(process.getErrorStream().readAllBytes(), StandardCharsets.UTF_8); | ||
|
|
||
| RunTaskConfiguration.ProcessReturnType returnType = taskConfiguration.getReturn(); | ||
|
|
||
| WorkflowModelFactory modelFactory = definition.application().modelFactory(); | ||
|
|
||
| return switch (returnType) { | ||
| return switch (taskConfiguration.getReturn()) { | ||
| case ALL -> modelFactory.fromAny(new ProcessResult(exitCode, stdout.trim(), stderr.trim())); | ||
| case NONE -> modelFactory.fromNull(); | ||
| case CODE -> modelFactory.from(exitCode); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot about that, nice