11: Add --severities option
This commit is contained in:
@@ -12,6 +12,7 @@ import com.bartek.esa.formatter.provider.FormatterProvider;
|
|||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class EsaMain {
|
public class EsaMain {
|
||||||
private final CliArgsParser cliArgsParser;
|
private final CliArgsParser cliArgsParser;
|
||||||
@@ -37,9 +38,16 @@ public class EsaMain {
|
|||||||
|
|
||||||
CliArgsOptions options = cliArgsParser.parse(args);
|
CliArgsOptions options = cliArgsParser.parse(args);
|
||||||
Set<Issue> issues = methodDispatcher.dispatchMethod(options, dispatcherActions);
|
Set<Issue> issues = methodDispatcher.dispatchMethod(options, dispatcherActions);
|
||||||
formatterProvider.provide(options).format(issues);
|
Set<Issue> filteredIssues = filterIssuesBySeverity(options, issues);
|
||||||
|
formatterProvider.provide(options).format(filteredIssues);
|
||||||
|
|
||||||
exitWithErrorIfAnyIssueIsAnError(issues);
|
exitWithErrorIfAnyIssueIsAnError(filteredIssues);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<Issue> filterIssuesBySeverity(CliArgsOptions options, Set<Issue> issues) {
|
||||||
|
return issues.stream()
|
||||||
|
.filter(issue -> options.getSeverities().contains(issue.getSeverity().name()))
|
||||||
|
.collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void exitWithErrorIfAnyIssueIsAnError(Set<Issue> issues) {
|
private void exitWithErrorIfAnyIssueIsAnError(Set<Issue> issues) {
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ public class CliArgsOptions {
|
|||||||
private Set<String> excludes;
|
private Set<String> excludes;
|
||||||
private Set<String> plugins;
|
private Set<String> plugins;
|
||||||
private boolean color;
|
private boolean color;
|
||||||
|
private Set<String> severities;
|
||||||
|
|
||||||
public boolean isSourceAnalysis() {
|
public boolean isSourceAnalysis() {
|
||||||
return sourceAnalysisDirectory != null;
|
return sourceAnalysisDirectory != null;
|
||||||
@@ -26,8 +27,6 @@ public class CliArgsOptions {
|
|||||||
|
|
||||||
public static CliArgsOptions empty() {
|
public static CliArgsOptions empty() {
|
||||||
return CliArgsOptions.builder()
|
return CliArgsOptions.builder()
|
||||||
.sourceAnalysisDirectory(null)
|
|
||||||
.apkAuditFile(null)
|
|
||||||
.excludes(emptySet())
|
.excludes(emptySet())
|
||||||
.plugins(emptySet())
|
.plugins(emptySet())
|
||||||
.build();
|
.build();
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
package com.bartek.esa.cli.parser;
|
package com.bartek.esa.cli.parser;
|
||||||
|
|
||||||
import com.bartek.esa.cli.model.CliArgsOptions;
|
import com.bartek.esa.cli.model.CliArgsOptions;
|
||||||
|
import com.bartek.esa.core.model.enumeration.Severity;
|
||||||
import org.apache.commons.cli.*;
|
import org.apache.commons.cli.*;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
|
import static java.util.Arrays.stream;
|
||||||
import static java.util.Collections.emptySet;
|
import static java.util.Collections.emptySet;
|
||||||
|
|
||||||
public class CliArgsParser {
|
public class CliArgsParser {
|
||||||
@@ -16,6 +19,7 @@ public class CliArgsParser {
|
|||||||
private static final String HELP_OPT = "help";
|
private static final String HELP_OPT = "help";
|
||||||
private static final String PLUGINS_OPT = "plugins";
|
private static final String PLUGINS_OPT = "plugins";
|
||||||
private static final String COLOR_OPT = "color";
|
private static final String COLOR_OPT = "color";
|
||||||
|
private static final String SEVERITIES_OPT = "severities";
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public CliArgsParser() {}
|
public CliArgsParser() {}
|
||||||
@@ -34,6 +38,7 @@ public class CliArgsParser {
|
|||||||
|
|
||||||
if (command.hasOption(HELP_OPT)) {
|
if (command.hasOption(HELP_OPT)) {
|
||||||
printHelp();
|
printHelp();
|
||||||
|
|
||||||
return CliArgsOptions.empty();
|
return CliArgsOptions.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,6 +54,7 @@ public class CliArgsParser {
|
|||||||
.plugins(command.hasOption(PLUGINS_OPT) ? new HashSet<>(asList(command.getOptionValues(PLUGINS_OPT))) : emptySet())
|
.plugins(command.hasOption(PLUGINS_OPT) ? new HashSet<>(asList(command.getOptionValues(PLUGINS_OPT))) : emptySet())
|
||||||
.excludes(command.hasOption(EXCLUDE_OPT) ? new HashSet<>(asList(command.getOptionValues(EXCLUDE_OPT))) : emptySet())
|
.excludes(command.hasOption(EXCLUDE_OPT) ? new HashSet<>(asList(command.getOptionValues(EXCLUDE_OPT))) : emptySet())
|
||||||
.color(command.hasOption(COLOR_OPT))
|
.color(command.hasOption(COLOR_OPT))
|
||||||
|
.severities(command.hasOption(SEVERITIES_OPT) ? new HashSet<>(asList((command.getOptionValues(SEVERITIES_OPT)))) : stream(Severity.values()).map(Severity::name).collect(Collectors.toSet()))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,6 +71,7 @@ public class CliArgsParser {
|
|||||||
options.addOption(plugins());
|
options.addOption(plugins());
|
||||||
options.addOption(help());
|
options.addOption(help());
|
||||||
options.addOption(color());
|
options.addOption(color());
|
||||||
|
options.addOption(severities());
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,4 +124,14 @@ public class CliArgsParser {
|
|||||||
.desc("enable colored output")
|
.desc("enable colored output")
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Option severities() {
|
||||||
|
String severities = stream(Severity.values()).map(Severity::name).map(String::toUpperCase).collect(Collectors.joining(", "));
|
||||||
|
return Option.builder()
|
||||||
|
.longOpt(SEVERITIES_OPT)
|
||||||
|
.argName("SEVERITY")
|
||||||
|
.numberOfArgs(Option.UNLIMITED_VALUES)
|
||||||
|
.desc("filter output to selected severities(available: " + severities + ")")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user