hansva commented on code in PR #9388:
URL: https://github.com/apache/hop/pull/9388#discussion_r4537762333


##########
engine/src/main/java/org/apache/hop/diagram/command/DiagramExportCommand.java:
##########
@@ -233,6 +234,26 @@ private void doExportSingleFile() throws HopException {
     log.logBasic("Exported " + resolvedFile + " -> " + result.getFilename());
   }
 
+  /**
+   * Fails before anything is written when no installed exporter writes the 
given format, so that a
+   * typo or a format without an exporter (PDF) is not silently written as 
another format.
+   */
+  private void checkFormatIsAvailable(String targetFmt) throws HopException {

Review Comment:
   This catches `.pdf`, but any other `-o` extension without `--format` still 
resolves to SVG in `resolveTargetFormat` (line 350) and passes this check, so 
`hop export -f p.hpl -o diagram.png` (or `.puml`, `.foo`) writes SVG markup 
into that file with exit code 0, which is the silent wrong-format write the PR 
and docs say can't happen anymore. An installed exporter whose extension isn't 
hard-coded there (e.g. PlantUML `.puml`) is also never picked from `-o`. 
Resolving the format from the output extension against the installed exporters, 
and failing on an unknown extension, would close this.



##########
core/src/main/java/org/apache/hop/core/diagram/DiagramExportService.java:
##########
@@ -144,25 +146,49 @@ public DiagramExportResult export(
     if (options == null) {
       throw new HopException("No export options provided");
     }
-    IDiagramExporter exporter = null;
-    if (StringUtils.isNotEmpty(options.getFormat())) {
+    List<IDiagramExporter<?>> available = findExportersForSubject(subject);

Review Comment:
   `findExportersForSubject` runs here and again inside `findExporter`, and 
each call goes through `getAllExporters()`, which loads every exporter plugin 
class, so every export loads them twice (three times from the CLI with its 
pre-check, and per file in batch mode). Looking up the exporter in `available` 
would avoid the second pass.



##########
core/src/main/java/org/apache/hop/core/diagram/DiagramExportService.java:
##########
@@ -144,25 +146,49 @@ public DiagramExportResult export(
     if (options == null) {
       throw new HopException("No export options provided");
     }
-    IDiagramExporter exporter = null;
-    if (StringUtils.isNotEmpty(options.getFormat())) {
+    List<IDiagramExporter<?>> available = findExportersForSubject(subject);
+    IDiagramExporter exporter;
+    if (StringUtils.isNotBlank(options.getFormat())) {
+      // A format that was asked for must be honoured: never write another 
format under its name.
       exporter = findExporter(subject, options.getFormat());

Review Comment:
   This now throws whenever the requested format matches nothing, but the GUI 
dialog sends a display label as the format when the selected exporter has no 
`DiagramExportFormat` (`DiagramExportDialogModel` line 174: 
`options.setFormat(format)` with the label from `formatLabel()`). 
`findExporter` only matches the format id or the file extension, so it never 
matches that label, and exporting with such a plugin exporter from Hop GUI now 
fails with 'No diagram exporter for format ...'. Before, `export()` fell back 
to the first supporting exporter. Maybe have the dialog pass the exporter 
itself (or its id), or let `findExporter` also match `getId()`?



##########
engine/src/main/java/org/apache/hop/diagram/command/DiagramExportCommand.java:
##########
@@ -233,6 +234,26 @@ private void doExportSingleFile() throws HopException {
     log.logBasic("Exported " + resolvedFile + " -> " + result.getFilename());
   }
 
+  /**
+   * Fails before anything is written when no installed exporter writes the 
given format, so that a
+   * typo or a format without an exporter (PDF) is not silently written as 
another format.
+   */
+  private void checkFormatIsAvailable(String targetFmt) throws HopException {
+    List<IDiagramExporter<?>> exporters = 
DiagramExportService.getInstance().getAllExporters();
+    for (IDiagramExporter<?> exporter : exporters) {
+      if ((exporter.getFormat() != null && 
exporter.getFormat().getId().equalsIgnoreCase(targetFmt))

Review Comment:
   This copies the id/extension matching of 
`DiagramExportService.findExporter`, and the two already differ (all exporters 
here, only those for the subject there). Both also call `getFormat().getId()` 
without a null check, so an exporter with a null format id makes every 
`--format` export fail with an NPE. A service helper (e.g. 
`findExporterForFormat`) used in both places would fix both.



##########
engine/src/main/java/org/apache/hop/diagram/command/DiagramExportCommand.java:
##########
@@ -251,6 +272,7 @@ private void doBatchExport() throws HopException {
       collectFiles(sourceDir, filesToExport, recursive);
 
       String targetFmt = resolveTargetFormat(null, format);
+      checkFormatIsAvailable(targetFmt);

Review Comment:
   This check looks at all exporters, not at the ones that support each file's 
subject. Since the service now throws for a subject without that format, a 
`--format` that only pipelines support makes the batch export a few `.hpl` 
files and then abort on the first `.hwf` (the exception reaches the outer catch 
at line 305), leaving a partial set of files. Before, a per-file failure was 
logged at line 300 and the loop continued. Catching the exception per file 
would keep the old behaviour.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to