1
1
package io .kestra .plugin .scripts .exec .scripts .runners ;
2
2
3
3
import io .kestra .core .exceptions .IllegalVariableEvaluationException ;
4
+ import io .kestra .core .models .property .Property ;
4
5
import io .kestra .core .models .tasks .RunnableTaskException ;
5
6
import io .kestra .core .models .tasks .runners .DefaultLogConsumer ;
6
7
import io .kestra .core .models .tasks .runners .*;
20
21
import lombok .AllArgsConstructor ;
21
22
import lombok .Getter ;
22
23
import lombok .With ;
24
+ import org .apache .commons .lang3 .SystemUtils ;
23
25
24
26
import java .io .IOException ;
25
27
import java .io .InputStream ;
26
28
import java .net .URI ;
27
29
import java .nio .file .Path ;
28
30
import java .time .Duration ;
29
- import java .util .HashMap ;
30
- import java . util . List ;
31
- import java . util . Map ;
31
+ import java .util .* ;
32
+
33
+ import static io . kestra . core . utils . Rethrow . throwFunction ;
32
34
33
35
@ AllArgsConstructor
34
36
@ Getter
@@ -42,7 +44,19 @@ public class CommandsWrapper implements TaskCommands {
42
44
private Map <String , Object > additionalVars ;
43
45
44
46
@ With
45
- private List <String > commands ;
47
+ private Property <List <String >> interpreter ;
48
+
49
+ @ With
50
+ private Property <List <String >> beforeCommands ;
51
+
52
+ @ With
53
+ private Property <List <String >> commands ;
54
+
55
+ @ With
56
+ private boolean beforeCommandsWithOptions ;
57
+
58
+ @ With
59
+ private boolean failFast ;
46
60
47
61
private Map <String , String > env ;
48
62
@@ -96,7 +110,11 @@ public CommandsWrapper withEnv(Map<String, String> envs) {
96
110
workingDirectory ,
97
111
getOutputDirectory (),
98
112
additionalVars ,
113
+ interpreter ,
114
+ beforeCommands ,
99
115
commands ,
116
+ beforeCommandsWithOptions ,
117
+ failFast ,
100
118
envs ,
101
119
logConsumer ,
102
120
runnerType ,
@@ -155,7 +173,21 @@ public <T extends TaskRunnerDetailResult> ScriptOutput run() throws Exception {
155
173
RunContextInitializer initializer = ((DefaultRunContext ) runContext ).getApplicationContext ().getBean (RunContextInitializer .class );
156
174
157
175
RunContext taskRunnerRunContext = initializer .forPlugin (((DefaultRunContext ) runContext ).clone (), realTaskRunner );
158
- this .commands = this .render (runContext , commands );
176
+
177
+ List <String > renderedCommands = this .renderCommands (runContext , commands );
178
+ List <String > renderedBeforeCommands = this .renderCommands (runContext , beforeCommands );
179
+ List <String > renderedInterpreter = this .renderCommands (runContext , interpreter );
180
+
181
+ List <String > finalCommands = renderedBeforeCommands .isEmpty () && renderedInterpreter .isEmpty () ?
182
+ renderedCommands :
183
+ ScriptService .scriptCommands (
184
+ renderedInterpreter ,
185
+ this .isBeforeCommandsWithOptions () ? getBeforeCommandsWithOptions (renderedBeforeCommands ) : renderedBeforeCommands ,
186
+ renderedCommands ,
187
+ Optional .ofNullable (targetOS ).orElse (TargetOS .AUTO )
188
+ );
189
+
190
+ this .commands = Property .of (finalCommands );
159
191
160
192
ScriptOutput .ScriptOutputBuilder scriptOutputBuilder = ScriptOutput .builder ()
161
193
.warningOnStdErr (this .warningOnStdErr );
@@ -244,7 +276,18 @@ public String render(RunContext runContext, String command, List<String> interna
244
276
);
245
277
}
246
278
247
- public List <String > render (RunContext runContext , List <String > commands ) throws IllegalVariableEvaluationException , IOException {
279
+ public String render (RunContext runContext , Property <String > command ) throws IllegalVariableEvaluationException , IOException {
280
+ TaskRunner <?> taskRunner = this .getTaskRunner ();
281
+ if (command == null ) {
282
+ return null ;
283
+ }
284
+
285
+ return runContext .render (command ).as (String .class , taskRunner .additionalVars (runContext , this ))
286
+ .map (throwFunction (c -> ScriptService .replaceInternalStorage (runContext , c , taskRunner instanceof RemoteRunnerInterface )))
287
+ .orElse (null );
288
+ }
289
+
290
+ public List <String > renderCommands (RunContext runContext , Property <List <String >> commands ) throws IllegalVariableEvaluationException , IOException {
248
291
TaskRunner <?> taskRunner = this .getTaskRunner ();
249
292
return ScriptService .replaceInternalStorage (
250
293
this .runContext ,
@@ -253,4 +296,31 @@ public List<String> render(RunContext runContext, List<String> commands) throws
253
296
taskRunner instanceof RemoteRunnerInterface
254
297
);
255
298
}
299
+
300
+ protected List <String > getBeforeCommandsWithOptions (List <String > beforeCommands ) throws IllegalVariableEvaluationException {
301
+ if (!this .isFailFast ()) {
302
+ return beforeCommands ;
303
+ }
304
+
305
+ if (beforeCommands == null || beforeCommands .isEmpty ()) {
306
+ return getExitOnErrorCommands ();
307
+ }
308
+
309
+ ArrayList <String > newCommands = new ArrayList <>(beforeCommands .size () + 1 );
310
+ newCommands .addAll (getExitOnErrorCommands ());
311
+ newCommands .addAll (beforeCommands );
312
+ return newCommands ;
313
+ }
314
+
315
+ protected List <String > getExitOnErrorCommands () {
316
+ TargetOS os = this .getTargetOS ();
317
+
318
+ // If targetOS is Windows OR targetOS is AUTO && current system is windows and we use process as a runner.(TLDR will run on windows)
319
+ if (os == TargetOS .WINDOWS ||
320
+ (os == TargetOS .AUTO && SystemUtils .IS_OS_WINDOWS && this .getTaskRunner () instanceof Process )) {
321
+ return List .of ("" );
322
+ }
323
+ // errexit option may be unsupported by non-shell interpreter.
324
+ return List .of ("set -e" );
325
+ }
256
326
}
0 commit comments