Skip to content

Commit 7757032

Browse files
committed
Refactor
1 parent b45b5b3 commit 7757032

File tree

1 file changed

+68
-47
lines changed

1 file changed

+68
-47
lines changed

gbfs-validator-java-cli/src/main/java/org/entur/gbfs/validator/cli/GbfsValidatorCli.java

Lines changed: 68 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.entur.gbfs.validator.cli;
22

33
import java.io.File;
4+
import java.io.IOException;
45
import java.io.InputStream;
56
import java.util.HashMap;
67
import java.util.List;
@@ -74,68 +75,28 @@ public Integer call() throws Exception {
7475
Loader loader = null;
7576

7677
try {
77-
// 1. Build authentication
7878
Authentication auth = AuthenticationHandler.buildAuthentication(
7979
authOptions
8080
);
8181

82-
// 2. Create loader and load feeds
8382
loader = new Loader();
8483
List<LoadedFile> loadedFiles = loader.load(feedUrl, auth);
8584

86-
// 3. Check for fatal loader errors
87-
boolean hasFatalLoaderErrors = loadedFiles
88-
.stream()
89-
.anyMatch(file ->
90-
file.loaderErrors() != null && !file.loaderErrors().isEmpty()
91-
);
92-
93-
if (
94-
hasFatalLoaderErrors &&
95-
loadedFiles.stream().noneMatch(file -> file.fileContents() != null)
96-
) {
85+
boolean hasFatalLoaderErrors = hasFatalLoaderErrors(loadedFiles);
86+
if (hasFatalLoaderErrors && hasNoValidContent(loadedFiles)) {
9787
System.err.println("ERROR: Failed to load any feeds from " + feedUrl);
98-
return 2; // System error
88+
return 2;
9989
}
10090

101-
// 4. Convert loaded files to validator input format
102-
Map<String, InputStream> fileMap = new HashMap<>();
103-
for (LoadedFile file : loadedFiles) {
104-
if (file.fileContents() != null) {
105-
fileMap.put(file.fileName(), file.fileContents());
106-
}
107-
}
91+
Map<String, InputStream> fileMap = buildFileMap(loadedFiles);
10892

109-
// 5. Validate
11093
GbfsValidator validator = GbfsValidatorFactory.getGbfsJsonValidator();
11194
ValidationResult result = validator.validate(fileMap);
11295

113-
// 6. Format report
114-
ReportFormatter formatter = createFormatter(format);
115-
String report = formatter.format(result, loadedFiles, verbose);
116-
117-
// 7. Output report
118-
if ("yes".equalsIgnoreCase(printReport)) {
119-
System.out.println(report);
120-
}
121-
122-
if (reportFile != null) {
123-
ReportWriter.writeReport(reportFile, report);
124-
if (!"yes".equalsIgnoreCase(printReport)) {
125-
System.out.println(
126-
"Report saved to: " + reportFile.getAbsolutePath()
127-
);
128-
}
129-
}
96+
String report = formatReport(result, loadedFiles);
97+
outputReport(report);
13098

131-
// 8. Determine exit code
132-
if (hasFatalLoaderErrors) {
133-
return 2; // System error
134-
} else if (result.summary().errorsCount() > 0) {
135-
return 1; // Validation failure
136-
} else {
137-
return 0; // Success
138-
}
99+
return determineExitCode(hasFatalLoaderErrors, result);
139100
} catch (Exception e) {
140101
System.err.println("ERROR: " + e.getMessage());
141102
if (verbose) {
@@ -149,6 +110,66 @@ public Integer call() throws Exception {
149110
}
150111
}
151112

113+
private boolean hasFatalLoaderErrors(List<LoadedFile> loadedFiles) {
114+
return loadedFiles
115+
.stream()
116+
.anyMatch(file ->
117+
file.loaderErrors() != null && !file.loaderErrors().isEmpty()
118+
);
119+
}
120+
121+
private boolean hasNoValidContent(List<LoadedFile> loadedFiles) {
122+
return loadedFiles
123+
.stream()
124+
.noneMatch(file -> file.fileContents() != null);
125+
}
126+
127+
private Map<String, InputStream> buildFileMap(List<LoadedFile> loadedFiles) {
128+
Map<String, InputStream> fileMap = new HashMap<>();
129+
for (LoadedFile file : loadedFiles) {
130+
if (file.fileContents() != null) {
131+
fileMap.put(file.fileName(), file.fileContents());
132+
}
133+
}
134+
return fileMap;
135+
}
136+
137+
private String formatReport(
138+
ValidationResult result,
139+
List<LoadedFile> loadedFiles
140+
) {
141+
ReportFormatter formatter = createFormatter(format);
142+
return formatter.format(result, loadedFiles, verbose);
143+
}
144+
145+
private void outputReport(String report) throws IOException {
146+
if ("yes".equalsIgnoreCase(printReport)) {
147+
System.out.println(report);
148+
}
149+
150+
if (reportFile != null) {
151+
ReportWriter.writeReport(reportFile, report);
152+
if (!"yes".equalsIgnoreCase(printReport)) {
153+
System.out.println(
154+
"Report saved to: " + reportFile.getAbsolutePath()
155+
);
156+
}
157+
}
158+
}
159+
160+
private int determineExitCode(
161+
boolean hasFatalLoaderErrors,
162+
ValidationResult result
163+
) {
164+
if (hasFatalLoaderErrors) {
165+
return 2;
166+
} else if (result.summary().errorsCount() > 0) {
167+
return 1;
168+
} else {
169+
return 0;
170+
}
171+
}
172+
152173
private ReportFormatter createFormatter(String format) {
153174
return switch (format.toLowerCase()) {
154175
case "json" -> new JsonReportFormatter();

0 commit comments

Comments
 (0)