Skip to content

Commit

Permalink
fix(cli): add pretty option for JSON output type
Browse files Browse the repository at this point in the history
  • Loading branch information
fhussonnois committed Nov 19, 2024
1 parent 7452aa9 commit c159ca8
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@

public class ExecOptionsMixin {

@Option(names = { "--output", "-o" },
defaultValue = "TEXT",
description = "Prints the output in the specified format. Valid values: ${COMPLETION-CANDIDATES} (default: ${DEFAULT-VALUE})."
@Option(names = {"--output", "-o"},
defaultValue = "TEXT",
description = "Prints the output in the specified format. Valid values: ${COMPLETION-CANDIDATES} (default: ${DEFAULT-VALUE})."
)
public Printers format;

@Option(names = {"--pretty"},
description = "Pretty print the output (only supported for JSON)."
)
public boolean pretty;

@Option(names = "--dry-run",
description = "Execute command in Dry-Run mode."
description = "Execute command in Dry-Run mode."
)
public boolean dryRun;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.streamthoughts.jikkou.client.command.FileOptionsMixin;
import io.streamthoughts.jikkou.client.command.SelectorOptionsMixin;
import io.streamthoughts.jikkou.client.command.validate.ValidationErrorsWriter;
import io.streamthoughts.jikkou.client.printer.Printers;
import io.streamthoughts.jikkou.core.JikkouApi;
import io.streamthoughts.jikkou.core.ReconciliationContext;
import io.streamthoughts.jikkou.core.ReconciliationMode;
Expand Down Expand Up @@ -56,7 +57,8 @@ public Integer call() throws IOException {
getReconciliationMode(),
getReconciliationContext()
);
return execOptions.format.print(results, Jikkou.getExecutionTime());
Printers printers = execOptions.format;
return printers.print(results, Jikkou.getExecutionTime(), execOptions.pretty);
} catch (ValidationException exception) {
System.out.println(ValidationErrorsWriter.write(exception.errors()));
return CommandLine.ExitCode.SOFTWARE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public Integer call() throws IOException {
mode,
getReconciliationContext()
);
return execOptions.format.print(results, Jikkou.getExecutionTime());
return execOptions.format.print(results, Jikkou.getExecutionTime(), execOptions.pretty);
} catch (ValidationException exception) {
System.out.println(ValidationErrorsWriter.write(exception.errors()));
return CommandLine.ExitCode.SOFTWARE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ public interface Printer {
*
* @param result the reconciliation changes to print.
* @param executionTimeMs the execution time in milliseconds.
* @param pretty Use a pretty-printer.
* @return the exit name.
*/
int print(ApiChangeResultList result, long executionTimeMs);
int print(ApiChangeResultList result, long executionTimeMs, boolean pretty);

static int getNumberOfFailedChange(final List<ChangeResult> results) {
return (int) results.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public enum Printers implements Printer {

/** {@inheritDoc} **/
@Override
public int print(ApiChangeResultList result, long executionTimeInMillis) {
return printer.print(result, executionTimeInMillis);
public int print(ApiChangeResultList result, long executionTimeInMillis, boolean pretty) {
return printer.print(result, executionTimeInMillis, pretty);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ public SerializePrinter(ObjectMapper mapper) {
* {@inheritDoc}
**/
@Override
public int print(ApiChangeResultList result, long executionTimeMs) {
public int print(ApiChangeResultList result, long executionTimeMs, boolean pretty) {
final String json;
try {
json = mapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(result);
if (pretty) {
json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result);
} else {
json = mapper.writeValueAsString(result);
}
System.out.print(json);
} catch (JsonProcessingException e) {
throw new JikkouRuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public TextPrinter(boolean printChangeDetail) {
* {@inheritDoc}
**/
@Override
public int print(ApiChangeResultList result,
long executionTimeMs) {
public int print(ApiChangeResultList result, long executionTimeMs, boolean pretty) {
int ok = 0;
int created = 0;
int changed = 0;
Expand Down

0 comments on commit c159ca8

Please sign in to comment.