
net.adamcin.oakpal.cli.Command Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of oakpal-cli Show documentation
Show all versions of oakpal-cli Show documentation
Command Line Interface for OakPAL
package net.adamcin.oakpal.cli;
import static net.adamcin.oakpal.core.Fun.compose;
import static net.adamcin.oakpal.core.Fun.inferTest1;
import static net.adamcin.oakpal.core.Fun.result1;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.function.Function;
import net.adamcin.oakpal.core.AbortedScanException;
import net.adamcin.oakpal.core.CheckReport;
import net.adamcin.oakpal.core.DefaultErrorListener;
import net.adamcin.oakpal.core.Nothing;
import net.adamcin.oakpal.core.OakMachine;
import net.adamcin.oakpal.core.OakpalPlan;
import net.adamcin.oakpal.core.Result;
import net.adamcin.oakpal.core.Violation;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
final class Command {
private static final Logger LOGGER = LoggerFactory.getLogger(Command.class);
private static final String NO_OPT_PREFIX = "--no-";
private static final Integer EXIT_GENERAL_ERROR = 1;
private static final Integer EXIT_ABORTED_SCAN = 9;
private static final Integer EXIT_SEVERE_VIOLATION = 10;
private static final Integer EXIT_MAJOR_VIOLATION = 11;
private static final Integer EXIT_MINOR_VIOLATION = 12;
private static final String VERSION_PROPERTIES_NAME = "version.properties";
private static final String COMMAND_HELP_TXT = "help.txt";
IO perform(final @NotNull Console console, final @NotNull String[] args) {
final Result optsResult = parseArgs(console, args);
if (optsResult.getError().isPresent()) {
return console.printLineErr(optsResult.getError().get().getMessage())
.add(printHelp(console::printLineErr))
.add(IO.unit(EXIT_GENERAL_ERROR));
} else {
final Options opts = optsResult.getOrDefault(Options.DEFAULT_OPTIONS);
if (opts.isJustHelp()) {
return printHelp(console::printLine).add(IO.unit(0));
} else if (opts.isJustVersion()) {
return printVersion(console::printLine).add(IO.unit(0));
}
final ClassLoader cl = opts.getScanClassLoader();
final URL planUrl = opts.getPlanUrl();
final Result> scanResult = OakpalPlan.fromJson(planUrl)
.flatMap(result1(plan ->
plan.toOakMachineBuilder(new DefaultErrorListener(), cl)))
.map(OakMachine.Builder::build).flatMap(oak -> runOakScan(opts, oak));
if (scanResult.getError().isPresent()) {
return console.printLineErr(scanResult.teeLogError().getError().get().getMessage())
.add(IO.unit(EXIT_ABORTED_SCAN));
} else {
final List reports = scanResult.getOrDefault(Collections.emptyList());
final Optional highestSeverity = reports.stream()
.flatMap(compose(CheckReport::getViolations, Collection::stream))
.map(Violation::getSeverity)
.reduce(Violation.Severity::maxSeverity)
.filter(inferTest1(opts.getFailOnSeverity().meetsMinimumSeverity()))
.map(severity -> {
switch (severity) {
case SEVERE:
return EXIT_SEVERE_VIOLATION;
case MAJOR:
return EXIT_MAJOR_VIOLATION;
case MINOR:
default:
return EXIT_MINOR_VIOLATION;
}
});
return printReports(reports, opts.getPrinter()).add(IO.unit(highestSeverity.orElse(0)));
}
}
}
IO printReports(final @NotNull List reports,
final @NotNull Function> linePrinter) {
return reports.stream().map(compose(ReportMessage::new, linePrinter)).reduce(IO.empty, IO::add);
}
IO printHelp(final @NotNull Function
© 2015 - 2025 Weber Informatics LLC | Privacy Policy