Skip to content

Commit 8888f35

Browse files
committed
fix: use same summary format as in js
Signed-off-by: Ruben Romero Montes <rromerom@redhat.com>
1 parent 8a8a643 commit 8888f35

1 file changed

Lines changed: 36 additions & 28 deletions

File tree

  • src/main/java/com/redhat/exhort/cli

src/main/java/com/redhat/exhort/cli/App.java

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525
import com.redhat.exhort.Api;
2626
import com.redhat.exhort.api.v4.AnalysisReport;
2727
import com.redhat.exhort.api.v4.ProviderReport;
28-
import com.redhat.exhort.api.v4.Source;
28+
import com.redhat.exhort.api.v4.SourceSummary;
2929
import com.redhat.exhort.impl.ExhortApi;
3030
import java.io.IOException;
3131
import java.nio.file.Files;
3232
import java.nio.file.Path;
3333
import java.nio.file.Paths;
34+
import java.util.HashMap;
35+
import java.util.Map;
3436
import java.util.concurrent.CompletableFuture;
3537
import java.util.concurrent.ExecutionException;
3638

@@ -91,28 +93,32 @@ private static CliArgs parseArgs(String[] args) {
9193

9294
private static Command parseCommand(String commandStr) {
9395
switch (commandStr) {
94-
case "stack":
96+
case "stack" -> {
9597
return Command.STACK;
96-
case "component":
98+
}
99+
case "component" -> {
97100
return Command.COMPONENT;
98-
default:
99-
throw new IllegalArgumentException(
100-
"Unknown command: " + commandStr + ". Use 'stack' or 'component'");
101+
}
102+
default ->
103+
throw new IllegalArgumentException(
104+
"Unknown command: " + commandStr + ". Use 'stack' or 'component'");
101105
}
102106
}
103107

104108
private static OutputFormat parseOutputFormat(Command command, String formatArg) {
105109
switch (formatArg) {
106-
case "--summary":
110+
case "--summary" -> {
107111
return OutputFormat.SUMMARY;
108-
case "--html":
112+
}
113+
case "--html" -> {
109114
if (command != Command.STACK) {
110115
throw new IllegalArgumentException("HTML format is only supported for stack command");
111116
}
112117
return OutputFormat.HTML;
113-
default:
114-
throw new IllegalArgumentException(
115-
"Unknown option for " + command + " command: " + formatArg);
118+
}
119+
default ->
120+
throw new IllegalArgumentException(
121+
"Unknown option for " + command + " command: " + formatArg);
116122
}
117123
}
118124

@@ -129,30 +135,33 @@ private static Path validateFile(String filePath) {
129135

130136
private static CompletableFuture<String> executeCommand(CliArgs args) throws IOException {
131137
switch (args.command) {
132-
case STACK:
138+
case STACK -> {
133139
return executeStackAnalysis(args.filePath.toAbsolutePath().toString(), args.outputFormat);
134-
case COMPONENT:
140+
}
141+
case COMPONENT -> {
135142
return executeComponentAnalysis(
136143
args.filePath.toAbsolutePath().toString(), args.outputFormat);
137-
default:
138-
throw new AssertionError();
144+
}
145+
default -> throw new AssertionError();
139146
}
140147
}
141148

142149
private static CompletableFuture<String> executeStackAnalysis(
143150
String filePath, OutputFormat outputFormat) throws IOException {
144151
Api api = new ExhortApi();
145152
switch (outputFormat) {
146-
case JSON:
153+
case JSON -> {
147154
return api.stackAnalysis(filePath).thenApply(App::toJsonString);
148-
case HTML:
155+
}
156+
case HTML -> {
149157
return api.stackAnalysisHtml(filePath).thenApply(bytes -> new String(bytes));
150-
case SUMMARY:
158+
}
159+
case SUMMARY -> {
151160
return api.stackAnalysis(filePath)
152161
.thenApply(App::extractSummary)
153162
.thenApply(App::toJsonString);
154-
default:
155-
throw new AssertionError();
163+
}
164+
default -> throw new AssertionError();
156165
}
157166
}
158167

@@ -161,7 +170,8 @@ private static CompletableFuture<String> executeComponentAnalysis(
161170
Api api = new ExhortApi();
162171
CompletableFuture<AnalysisReport> analysis = api.componentAnalysis(filePath);
163172
if (outputFormat.equals(OutputFormat.SUMMARY)) {
164-
analysis = analysis.thenApply(App::extractSummary);
173+
var summary = analysis.thenApply(App::extractSummary);
174+
return summary.thenApply(App::toJsonString);
165175
}
166176
return analysis.thenApply(App::toJsonString);
167177
}
@@ -174,9 +184,8 @@ private static String toJsonString(Object obj) {
174184
}
175185
}
176186

177-
private static AnalysisReport extractSummary(AnalysisReport report) {
178-
AnalysisReport summary = new AnalysisReport();
179-
summary.setScanned(report.getScanned());
187+
private static Map<String, SourceSummary> extractSummary(AnalysisReport report) {
188+
Map<String, SourceSummary> summary = new HashMap<>();
180189
if (report.getProviders() == null) {
181190
return summary;
182191
}
@@ -194,12 +203,11 @@ private static AnalysisReport extractSummary(AnalysisReport report) {
194203
.entrySet()
195204
.forEach(
196205
sourceEntry -> {
197-
var source = new Source();
198-
source.setSummary(sourceEntry.getValue().getSummary());
199-
provider.putSourcesItem(sourceEntry.getKey(), source);
206+
if (sourceEntry.getValue().getSummary() != null) {
207+
summary.put(sourceEntry.getKey(), sourceEntry.getValue().getSummary());
208+
}
200209
});
201210
}
202-
summary.putProvidersItem(entry.getKey(), provider);
203211
});
204212
return summary;
205213
}

0 commit comments

Comments
 (0)