From 7d303f8abe931506af8b3fcd667d1e59584323ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernhard=20Str=C3=A4hle?= Date: Thu, 24 Oct 2024 23:39:19 +0200 Subject: [PATCH] Make debug text platform independent and flush error output stream in exception handler --- .../crd/generator/cli/CRDGeneratorCLI.java | 38 +++++++++++++------ ...CRDGeneratorExecutionExceptionHandler.java | 22 ++++++----- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/CRDGeneratorCLI.java b/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/CRDGeneratorCLI.java index 6e1ebc4c4e0..6dfae64b344 100644 --- a/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/CRDGeneratorCLI.java +++ b/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/CRDGeneratorCLI.java @@ -232,29 +232,43 @@ CRDGenerationInfo getCrdGenerationInfo() { return crdGenerationInfo; } - String getDiagText() { + /** + * Get details as text about the internal state. + * + * @return the debug text. + */ + String getDebugText() { + final String lineSeparator = System.lineSeparator(); StringBuilder sb = new StringBuilder(); - sb.append("\n"); + sb.append(lineSeparator); if (!customResourceClassNames.isEmpty()) { - sb.append("Custom Resource class names:\n"); - customResourceClassNames.forEach(fqcn -> sb.append(" ").append(fqcn).append("\n")); + sb.append("Custom Resource class names:"); + sb.append(lineSeparator); + customResourceClassNames.forEach(fqcn -> sb.append(" ").append(fqcn).append(lineSeparator)); + sb.append(lineSeparator); } if (filesToScan.isEmpty()) { - sb.append("Scan Paths: []\n"); + sb.append("Scan Paths: []"); + sb.append(lineSeparator); } else { - sb.append("Scan Paths:\n"); - filesToScan.forEach(f -> sb.append(" ").append(f.getPath()).append("\n")); + sb.append("Scan Paths:"); + sb.append(lineSeparator); + filesToScan.forEach(f -> sb.append(" ").append(f.getPath()).append(lineSeparator)); + sb.append(lineSeparator); } List allClasspathElements = getClasspathElements(); if (allClasspathElements.isEmpty()) { - sb.append("Classpath: []\n"); + sb.append("Classpath: []"); + sb.append(lineSeparator); } else { - sb.append("\nClasspath:\n"); - allClasspathElements.forEach(cpe -> sb.append(" ").append(cpe).append("\n")); + sb.append("Classpath:"); + sb.append(lineSeparator); + allClasspathElements.forEach(cpe -> sb.append(" ").append(cpe).append(lineSeparator)); + sb.append(lineSeparator); } - sb.append("\n"); + sb.append(lineSeparator); return sb.toString(); } @@ -311,7 +325,7 @@ static CommandLine createCommandLine() { static CommandLine createCommandLine(CRDGeneratorCLI crdGeneratorCLI) { return new CommandLine(crdGeneratorCLI) - .setExecutionExceptionHandler(new CRDGeneratorExecutionExceptionHandler(crdGeneratorCLI::getDiagText)); + .setExecutionExceptionHandler(new CRDGeneratorExecutionExceptionHandler(crdGeneratorCLI::getDebugText)); } } diff --git a/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/CRDGeneratorExecutionExceptionHandler.java b/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/CRDGeneratorExecutionExceptionHandler.java index d96eca6ff86..a715b35ad51 100644 --- a/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/CRDGeneratorExecutionExceptionHandler.java +++ b/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/CRDGeneratorExecutionExceptionHandler.java @@ -26,10 +26,10 @@ class CRDGeneratorExecutionExceptionHandler implements CommandLine.IExecutionExc private static final Logger log = LoggerFactory.getLogger(CRDGeneratorExecutionExceptionHandler.class); - private final Supplier diagTextSupplier; + private final Supplier debugTextSupplier; - CRDGeneratorExecutionExceptionHandler(Supplier diagTextSupplier) { - this.diagTextSupplier = diagTextSupplier; + CRDGeneratorExecutionExceptionHandler(Supplier debugTextSupplier) { + this.debugTextSupplier = debugTextSupplier; } @Override @@ -42,11 +42,13 @@ public int handleExecutionException( if (ex instanceof CustomResourceClassLoaderException) { commandLine.getErr().println(); - commandLine.getErr().println("The classloader could not load the Custom Resource class.\n" + + commandLine.getErr().println("The classloader could not load the Custom Resource class."); + commandLine.getErr().println( "Check the list of classpath elements and add further JAR archives " + - "or directories containing required classes " + - "e.g. with `-cp my-dep.jar` or `-cp target/classes/`."); - commandLine.getErr().print(diagTextSupplier.get()); + "or directories containing required classes " + + "e.g. with `-cp my-dep.jar` or `-cp target/classes/`."); + commandLine.getErr().print(debugTextSupplier.get()); + commandLine.getErr().flush(); return CRDGeneratorExitCode.CR_CLASS_LOADING; } @@ -55,15 +57,17 @@ public int handleExecutionException( commandLine.getErr().println("Check JAR files and directories considered to be scanned " + "as well as your filters. At least one Custom Resource class " + "must be retained after filtering."); - commandLine.getErr().print(diagTextSupplier.get()); + commandLine.getErr().print(debugTextSupplier.get()); + commandLine.getErr().flush(); return CRDGeneratorExitCode.NO_CR_CLASSES_RETAINED; } if (log.isDebugEnabled()) { - commandLine.getErr().println(diagTextSupplier.get()); + commandLine.getErr().println(debugTextSupplier.get()); } log.trace(ex.getMessage(), ex); + commandLine.getErr().flush(); return CRDGeneratorExitCode.SOFTWARE; } }