Skip to content

Commit

Permalink
feat: Allow redirect error output to standard output (#50)
Browse files Browse the repository at this point in the history
This changes removes the null check of error output stream and adds a
method to allow the error output stream to be directed to standard
output
  • Loading branch information
stanleyz authored Oct 15, 2024
1 parent afa3236 commit 9f4e490
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ void setDirectory(File directory) {
assert this.process == null;
this.builder.directory(directory);
}

void setRedirectErrorStream(boolean redirectErrorStream) {
assert this.process == null;
this.builder.redirectErrorStream(redirectErrorStream);
}

void appendCommands(String... commands) {
Stream<String> filteredCommands = Arrays.stream(commands).filter(c -> c != null && c.length() > 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class TerraformClient implements AutoCloseable {
private boolean inheritIO;
private boolean showColor;
private boolean jsonOutput;
private boolean redirectErrorStream;
private String terraformVersion;
private String backendConfig;
private String terraformReleasesUrl;
Expand Down Expand Up @@ -68,7 +69,7 @@ public CompletableFuture<String> version() throws IOException {
return launcher.launch().thenApply((c) -> c == 0 ? version.toString() : null);
}

public CompletableFuture<Boolean> show(@NonNull TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, @NonNull Consumer<String> errorListener) throws IOException {
public CompletableFuture<Boolean> show(@NonNull TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, Consumer<String> errorListener) throws IOException {
checkVarFileParam(terraformProcessData);
checkTerraformVariablesParam(terraformProcessData);
return this.run(
Expand All @@ -83,7 +84,7 @@ public CompletableFuture<Boolean> show() throws IOException {
return this.run(TerraformCommand.show);
}

public CompletableFuture<Boolean> showPlan(@NonNull TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, @NonNull Consumer<String> errorListener) throws IOException {
public CompletableFuture<Boolean> showPlan(@NonNull TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, Consumer<String> errorListener) throws IOException {
checkVarFileParam(terraformProcessData);
checkTerraformVariablesParam(terraformProcessData);
return this.run(
Expand All @@ -98,7 +99,7 @@ public CompletableFuture<Boolean> showPlan() throws IOException {
return this.run(TerraformCommand.showPlan);
}

public CompletableFuture<Boolean> init(TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, @NonNull Consumer<String> errorListener) throws IOException {
public CompletableFuture<Boolean> init(TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, Consumer<String> errorListener) throws IOException {
checkVarFileParam(terraformProcessData);
checkTerraformVariablesParam(terraformProcessData);
return this.run(
Expand All @@ -113,39 +114,39 @@ public CompletableFuture<Boolean> init() throws IOException {
return this.run(TerraformCommand.init);
}

public CompletableFuture<Boolean> plan(TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, @NonNull Consumer<String> errorListener) throws IOException {
public CompletableFuture<Boolean> plan(TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, Consumer<String> errorListener) throws IOException {
return this.run(
terraformProcessData,
outputListener,
errorListener,
TerraformCommand.plan);
}

public CompletableFuture<Integer> planDetailExitCode(TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, @NonNull Consumer<String> errorListener) throws IOException {
public CompletableFuture<Integer> planDetailExitCode(TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, Consumer<String> errorListener) throws IOException {
terraformProcessData.setDetailExitCode(true);
return this.getTerraformLauncher(
terraformProcessData,
outputListener,
errorListener, TerraformCommand.plan).launch();
}

public CompletableFuture<Boolean> statePull(TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, @NonNull Consumer<String> errorListener) throws IOException {
public CompletableFuture<Boolean> statePull(TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, Consumer<String> errorListener) throws IOException {
return this.run(
terraformProcessData,
outputListener,
errorListener,
TerraformCommand.statePull);
}

public CompletableFuture<Boolean> planDestroy(TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, @NonNull Consumer<String> errorListener) throws IOException {
public CompletableFuture<Boolean> planDestroy(TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, Consumer<String> errorListener) throws IOException {
return this.run(
terraformProcessData,
outputListener,
errorListener,
TerraformCommand.planDestroy);
}

public CompletableFuture<Integer> planDestroyDetailExitCode(TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, @NonNull Consumer<String> errorListener) throws IOException {
public CompletableFuture<Integer> planDestroyDetailExitCode(TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, Consumer<String> errorListener) throws IOException {
terraformProcessData.setDetailExitCode(true);
return this.getTerraformLauncher(
terraformProcessData,
Expand All @@ -158,7 +159,7 @@ public CompletableFuture<Boolean> plan() throws IOException {
return this.run(TerraformCommand.plan);
}

public CompletableFuture<Boolean> apply(TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, @NonNull Consumer<String> errorListener) throws IOException {
public CompletableFuture<Boolean> apply(TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, Consumer<String> errorListener) throws IOException {
return this.run(
terraformProcessData,
outputListener,
Expand All @@ -171,7 +172,7 @@ public CompletableFuture<Boolean> apply() throws IOException {
return this.run(TerraformCommand.apply);
}

public CompletableFuture<Boolean> destroy(TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, @NonNull Consumer<String> errorListener) throws IOException {
public CompletableFuture<Boolean> destroy(TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, Consumer<String> errorListener) throws IOException {
checkBackendConfigFile(terraformProcessData);
return this.run(
terraformProcessData,
Expand All @@ -185,7 +186,7 @@ public CompletableFuture<Boolean> destroy() throws IOException {
return this.run(TerraformCommand.destroy);
}

public CompletableFuture<Boolean> output(TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, @NonNull Consumer<String> errorListener) throws IOException {
public CompletableFuture<Boolean> output(TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, Consumer<String> errorListener) throws IOException {
checkBackendConfigFile(terraformProcessData);
checkVarFileParam(terraformProcessData);
checkTerraformVariablesParam(terraformProcessData);
Expand Down Expand Up @@ -405,6 +406,7 @@ private ProcessLauncher getTerraformLauncher(TerraformProcessData terraformProce

launcher.setOutputListener(outputListener);
launcher.setErrorListener(errorListener);
launcher.setRedirectErrorStream(this.redirectErrorStream);
return launcher;
}

Expand Down

0 comments on commit 9f4e490

Please sign in to comment.