Skip to content

Commit

Permalink
fix: commands wrapper dp (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgabelle authored Feb 19, 2025
1 parent fce18de commit 318d5e7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 28 deletions.
12 changes: 5 additions & 7 deletions src/main/java/io/kestra/plugin/dbt/cli/AbstractDbt.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.kestra.core.models.tasks.runners.ScriptService;
import io.kestra.core.models.tasks.runners.TaskRunner;
import io.kestra.core.runners.RunContext;
import io.kestra.core.serializers.JacksonMapper;
import io.kestra.plugin.dbt.ResultParser;
import io.kestra.plugin.scripts.exec.scripts.models.DockerOptions;
import io.kestra.plugin.scripts.exec.scripts.models.RunnerType;
Expand Down Expand Up @@ -174,18 +175,15 @@ public void accept(String line, Boolean isStdErr) {
);
}

List<String> commandsArgs = ScriptService.scriptCommands(
List.of("/bin/sh", "-c"),
null,
List.of(createDbtCommand(runContext))
);

ScriptOutput run = commandsWrapper
.addEnv(Map.of(
"PYTHONUNBUFFERED", "true",
"PIP_ROOT_USER_ACTION", "ignore"
))
.withCommands(commandsArgs)
.withInterpreter(Property.of(List.of("/bin/sh", "-c")))
.withCommands(new Property<>(JacksonMapper.ofJson().writeValueAsString(
List.of(createDbtCommand(runContext)))
))
.run();

parseResults(runContext, workingDirectory, run);
Expand Down
21 changes: 10 additions & 11 deletions src/main/java/io/kestra/plugin/dbt/cli/DbtCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,7 @@ public class DbtCLI extends AbstractExecScript {
title = "The list of dbt CLI commands to run."
)
@NotNull
@NotEmpty
@PluginProperty(dynamic = true)
private List<String> commands;
private Property<List<String>> commands;

@Schema(
title = "The `profiles.yml` file content.",
Expand Down Expand Up @@ -396,15 +394,10 @@ public void accept(String line, Boolean isStdErr) {
);
}

//Create and run commands
List<String> commandsArgs = ScriptService.scriptCommands(
runContext.render(this.interpreter).asList(String.class),
this.getBeforeCommandsWithOptions(runContext),
runContext.render(this.commands).stream().map(command -> command.startsWith("dbt") ? command.concat(" --log-format json") : command).toList()
);
var renderedCommands = runContext.render(this.commands).asList(String.class);

// check that if a command uses --project-dir, the projectDir must be set
if (commandsArgs.stream().anyMatch(cmd -> cmd.contains("--project-dir")) && this.projectDir == null) {
if (renderedCommands.stream().anyMatch(cmd -> cmd.contains("--project-dir")) && this.projectDir == null) {
runContext.logger().warn("One of the dbt CLI commands uses the `--project-dir` flag, but the `projectDir` task property is not set. Make sure to set the `projectDir` property.");
}

Expand All @@ -413,7 +406,13 @@ public void accept(String line, Boolean isStdErr) {
"PYTHONUNBUFFERED", "true",
"PIP_ROOT_USER_ACTION", "ignore"
))
.withCommands(commandsArgs)
.withInterpreter(this.interpreter)
.withBeforeCommands(Property.of(this.getBeforeCommandsWithOptions(runContext)))
.withCommands(Property.of(
renderedCommands.stream()
.map(command -> command.startsWith("dbt") ? command.concat(" --log-format json") : command)
.toList())
)
.run();

//Parse run results
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/io/kestra/plugin/dbt/cli/Setup.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,14 @@ public ScriptOutput run(RunContext runContext) throws Exception {
new HashMap<>()
);

List<String> commandsArgs = ScriptService.scriptCommands(
runContext.render(this.interpreter).asList(String.class),
this.getBeforeCommandsWithOptions(runContext),
commands
);

return commandsWrapper
.addEnv(Map.of(
"PYTHONUNBUFFERED", "true",
"PIP_ROOT_USER_ACTION", "ignore"
))
.withCommands(commandsArgs)
.withInterpreter(this.interpreter)
.withBeforeCommands(Property.of(this.getBeforeCommandsWithOptions(runContext)))
.withCommands(Property.of(commands))
.run();
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/java/io/kestra/plugin/dbt/cli/DbtCLITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void run() throws Exception {
.profiles(Property.of(PROFILES)
)
.containerImage(new Property<>("ghcr.io/kestra-io/dbt-bigquery:latest"))
.commands(List.of("dbt build"))
.commands(Property.of(List.of("dbt build")))
.build();

RunContext runContext = TestsUtils.mockRunContext(runContextFactory, execute, Map.of());
Expand All @@ -98,7 +98,7 @@ void testDbtCliWithStoreManifest_manifestShouldBePresentInKvStore() throws Excep
.profiles(Property.of(PROFILES)
)
.containerImage(new Property<>("ghcr.io/kestra-io/dbt-bigquery:latest"))
.commands(List.of("dbt build"))
.commands(Property.of(List.of("dbt build")))
.storeManifest(
DbtCLI.KvStoreManifest.builder()
.key(Property.of(MANIFEST_KEY))
Expand Down Expand Up @@ -131,7 +131,7 @@ void testDbtWithLoadManifest_manifestShouldBeLoadedFromKvStore() throws Exceptio
.profiles(Property.of(PROFILES))
.projectDir(Property.of("unit-kestra"))
.containerImage(new Property<>("ghcr.io/kestra-io/dbt-bigquery:latest"))
.commands(List.of("dbt build --project-dir unit-kestra"))
.commands(Property.of(List.of("dbt build --project-dir unit-kestra")))
.loadManifest(
DbtCLI.KvStoreManifest.builder()
.key(Property.of(MANIFEST_KEY))
Expand Down

0 comments on commit 318d5e7

Please sign in to comment.