Skip to content

Commit

Permalink
refactor: move dbt cli to dynamic properties (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgabelle authored Jan 2, 2025
1 parent 7262746 commit 4daec1c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/main/java/io/kestra/plugin/dbt/cli/AbstractDbt.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,19 @@ public void setDockerOptions(Property<DockerOptions> dockerOptions) {

private Object inputFiles;

private List<String> outputFiles;
private Property<List<String>> outputFiles;

protected abstract java.util.List<String> dbtCommands(RunContext runContext) throws IllegalVariableEvaluationException;

@Override
public ScriptOutput run(RunContext runContext) throws Exception {
var renderedOutputFiles = runContext.render(this.outputFiles).asList(String.class);

CommandsWrapper commandsWrapper = new CommandsWrapper(runContext)
.withEnv(runContext.render(this.getEnv()).asMap(String.class, String.class))
.withNamespaceFiles(namespaceFiles)
.withInputFiles(inputFiles)
.withOutputFiles(outputFiles)
.withOutputFiles(renderedOutputFiles.isEmpty() ? null : renderedOutputFiles)
.withRunnerType(runContext.render(this.getRunner()).as(RunnerType.class).orElse(null))
.withDockerOptions(runContext.render(this.getDocker()).as(DockerOptions.class).orElse(null))
.withContainerImage(runContext.render(this.getContainerImage()).as(String.class).orElseThrow())
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/kestra/plugin/dbt/cli/DbtCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public class DbtCLI extends AbstractExecScript {
.build();

@Builder.Default
protected String containerImage = DEFAULT_IMAGE;
protected Property<String> containerImage = Property.of(DEFAULT_IMAGE);

@Schema(
title = "Store manifest.",
Expand All @@ -345,7 +345,7 @@ protected DockerOptions injectDefaults(DockerOptions original) {

var builder = original.toBuilder();
if (original.getImage() == null) {
builder.image(this.getContainerImage());
builder.image(this.getContainerImage().toString());
}
if (original.getEntryPoint() == null) {
builder.entryPoint(new ArrayList<>());
Expand Down Expand Up @@ -399,7 +399,7 @@ public void accept(String line, Boolean isStdErr) {
//Create and run commands
List<String> commandsArgs = ScriptService.scriptCommands(
this.interpreter,
this.getBeforeCommandsWithOptions(),
this.getBeforeCommandsWithOptions(runContext),
runContext.render(this.commands).stream().map(command -> command.startsWith("dbt") ? command.concat(" --log-format json") : command).toList()
);

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/kestra/plugin/dbt/cli/Setup.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public class Setup extends AbstractExecScript implements RunnableTask<ScriptOutp
protected TaskRunner taskRunner = Docker.instance();

@Builder.Default
protected String containerImage = DEFAULT_IMAGE;
protected Property<String> containerImage = Property.of(DEFAULT_IMAGE);

@Schema(title = "Deprecated, use the `docker` property instead", deprecated = true)
@Deprecated
Expand All @@ -163,7 +163,7 @@ public void setDockerOptions(DockerOptions dockerOptions) {
protected DockerOptions injectDefaults(DockerOptions original) {
var builder = original.toBuilder();
if (original.getImage() == null) {
builder.image(this.getContainerImage());
builder.image(this.getContainerImage().toString());
}

return builder.build();
Expand Down Expand Up @@ -197,7 +197,7 @@ public ScriptOutput run(RunContext runContext) throws Exception {

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

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 @@ -75,7 +75,7 @@ void run() throws Exception {
.type(DbtCLI.class.getName())
.profiles(Property.of(PROFILES)
)
.containerImage("ghcr.io/kestra-io/dbt-bigquery:latest")
.containerImage(new Property<>("ghcr.io/kestra-io/dbt-bigquery:latest"))
.commands(List.of("dbt build"))
.build();

Expand All @@ -97,7 +97,7 @@ void testDbtCliWithStoreManifest_manifestShouldBePresentInKvStore() throws Excep
.type(DbtCLI.class.getName())
.profiles(Property.of(PROFILES)
)
.containerImage("ghcr.io/kestra-io/dbt-bigquery:latest")
.containerImage(new Property<>("ghcr.io/kestra-io/dbt-bigquery:latest"))
.commands(List.of("dbt build"))
.storeManifest(
DbtCLI.KvStoreManifest.builder()
Expand Down Expand Up @@ -130,7 +130,7 @@ void testDbtWithLoadManifest_manifestShouldBeLoadedFromKvStore() throws Exceptio
.type(DbtCLI.class.getName())
.profiles(Property.of(PROFILES))
.projectDir(Property.of("unit-kestra"))
.containerImage("ghcr.io/kestra-io/dbt-bigquery:latest")
.containerImage(new Property<>("ghcr.io/kestra-io/dbt-bigquery:latest"))
.commands(List.of("dbt build --project-dir unit-kestra"))
.loadManifest(
DbtCLI.KvStoreManifest.builder()
Expand Down

0 comments on commit 4daec1c

Please sign in to comment.