10: Add summary to formatters
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package com.bartek.esa.formatter.formatter;
|
package com.bartek.esa.formatter.formatter;
|
||||||
|
|
||||||
import com.bartek.esa.core.desc.provider.DescriptionProvider;
|
import com.bartek.esa.core.desc.provider.DescriptionProvider;
|
||||||
|
import com.bartek.esa.core.model.enumeration.Severity;
|
||||||
import com.bartek.esa.core.model.object.Issue;
|
import com.bartek.esa.core.model.object.Issue;
|
||||||
import com.bartek.esa.formatter.archetype.Formatter;
|
import com.bartek.esa.formatter.archetype.Formatter;
|
||||||
import org.fusesource.jansi.Ansi;
|
import org.fusesource.jansi.Ansi;
|
||||||
@@ -16,6 +17,10 @@ import static org.fusesource.jansi.Ansi.Color.*;
|
|||||||
import static org.fusesource.jansi.Ansi.ansi;
|
import static org.fusesource.jansi.Ansi.ansi;
|
||||||
|
|
||||||
public class ColorFormatter implements Formatter {
|
public class ColorFormatter implements Formatter {
|
||||||
|
private static final Ansi.Color INFO_COLOR = GREEN;
|
||||||
|
private static final Ansi.Color WARNING_COLOR = YELLOW;
|
||||||
|
private static final Ansi.Color ERROR_COLOR = MAGENTA;
|
||||||
|
private static final Ansi.Color VULNERABILITY_COLOR = RED;
|
||||||
|
|
||||||
private final DescriptionProvider descriptionProvider;
|
private final DescriptionProvider descriptionProvider;
|
||||||
|
|
||||||
@@ -39,6 +44,7 @@ public class ColorFormatter implements Formatter {
|
|||||||
.collect(Collectors.joining());
|
.collect(Collectors.joining());
|
||||||
|
|
||||||
System.out.println(format.substring(0, format.length() - 2));
|
System.out.println(format.substring(0, format.length() - 2));
|
||||||
|
System.out.println(printSummary(issues));
|
||||||
AnsiConsole.systemUninstall();
|
AnsiConsole.systemUninstall();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,8 +101,32 @@ public class ColorFormatter implements Formatter {
|
|||||||
return ansi;
|
return ansi;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String printSummary(Set<Issue> issues) {
|
||||||
|
Ansi ansi = ansi();
|
||||||
|
ansi.a("\n--- Total:\n");
|
||||||
|
Arrays.stream(Severity.values())
|
||||||
|
.forEach(severity -> ansi.fg(getColorForSeverity(severity))
|
||||||
|
.a(severity.name())
|
||||||
|
.a(": ")
|
||||||
|
.reset()
|
||||||
|
.a(countIssuesBySeverity(issues, severity))
|
||||||
|
.a("\n"));
|
||||||
|
return ansi.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private long countIssuesBySeverity(Set<Issue> issues, Severity severity) {
|
||||||
|
return issues.stream()
|
||||||
|
.map(Issue::getSeverity)
|
||||||
|
.filter(severity::equals)
|
||||||
|
.count();
|
||||||
|
}
|
||||||
|
|
||||||
private Ansi.Color getColorForSeverity(Issue issue) {
|
private Ansi.Color getColorForSeverity(Issue issue) {
|
||||||
switch (issue.getSeverity()) {
|
return getColorForSeverity(issue.getSeverity());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Ansi.Color getColorForSeverity(Severity severity) {
|
||||||
|
switch (severity) {
|
||||||
case INFO:
|
case INFO:
|
||||||
return GREEN;
|
return GREEN;
|
||||||
case WARNING:
|
case WARNING:
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
package com.bartek.esa.formatter.formatter;
|
package com.bartek.esa.formatter.formatter;
|
||||||
|
|
||||||
import com.bartek.esa.core.desc.provider.DescriptionProvider;
|
import com.bartek.esa.core.desc.provider.DescriptionProvider;
|
||||||
|
import com.bartek.esa.core.model.enumeration.Severity;
|
||||||
import com.bartek.esa.core.model.object.Issue;
|
import com.bartek.esa.core.model.object.Issue;
|
||||||
import com.bartek.esa.formatter.archetype.Formatter;
|
import com.bartek.esa.formatter.archetype.Formatter;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -30,6 +32,7 @@ public class SimpleFormatter implements Formatter {
|
|||||||
.collect(Collectors.joining());
|
.collect(Collectors.joining());
|
||||||
|
|
||||||
System.out.println(format.substring(0, format.length() - 2));
|
System.out.println(format.substring(0, format.length() - 2));
|
||||||
|
System.out.println(printSummary(issues));
|
||||||
}
|
}
|
||||||
|
|
||||||
private String format(Issue issue) {
|
private String format(Issue issue) {
|
||||||
@@ -71,4 +74,23 @@ public class SimpleFormatter implements Formatter {
|
|||||||
format.append(line).append("\n");
|
format.append(line).append("\n");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String printSummary(Set<Issue> issues) {
|
||||||
|
StringBuilder format = new StringBuilder();
|
||||||
|
format.append("\n--- Total:\n");
|
||||||
|
Arrays.stream(Severity.values())
|
||||||
|
.forEach(severity -> format
|
||||||
|
.append(severity.name())
|
||||||
|
.append(": ")
|
||||||
|
.append(countIssuesBySeverity(issues, severity))
|
||||||
|
.append("\n"));
|
||||||
|
return format.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private long countIssuesBySeverity(Set<Issue> issues, Severity severity) {
|
||||||
|
return issues.stream()
|
||||||
|
.map(Issue::getSeverity)
|
||||||
|
.filter(severity::equals)
|
||||||
|
.count();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user