Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apache-rat-core/src/it/java/org/apache/rat/ReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void integrationTest(String testName, Document commandLineDoc) throws Exc
try {
Object value = shell.run(groovyScript, new String[]{outputFile.getAbsolutePath(), logFile.getAbsolutePath()});
if (value != null) {
fail(String.format("%s", value));
fail(String.format("%s: %s", testName, value));
}
} catch (AssertionError e) {
throw new AssertionError(String.format("%s: %s", testName, e.getMessage()), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ myArgs[3] = src.getAbsolutePath()

ReportConfiguration configuration = OptionCollection.parseCommands(src, myArgs, { opts -> })
assertNotNull(configuration)
configuration.validate(DefaultLog.getInstance().&error)
configuration.validate()
Reporter reporter = new Reporter(configuration)
ClaimStatistic statistic = reporter.execute()
ClaimStatistic statistic = reporter.execute().getStatistic()

assertEquals(3, statistic.getCounter(ClaimStatistic.Counter.APPROVED))
assertEquals(2, statistic.getCounter(ClaimStatistic.Counter.ARCHIVES))
Expand Down
14 changes: 4 additions & 10 deletions apache-rat-core/src/main/java/org/apache/rat/OptionCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.util.stream.Collectors;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
Expand Down Expand Up @@ -131,27 +130,22 @@ public static ReportConfiguration parseCommands(final File workingDirectory, fin
final Consumer<Options> helpCmd, final boolean noArgs) throws IOException {

Options opts = buildOptions();
CommandLine commandLine;
ArgumentContext argumentContext;
try {
commandLine = DefaultParser.builder().setDeprecatedHandler(DeprecationReporter.getLogReporter())
.setAllowPartialMatching(true).build().parse(opts, args);
argumentContext = new ArgumentContext(workingDirectory, opts, args);
} catch (ParseException e) {
DefaultLog.getInstance().error(e.getMessage());
DefaultLog.getInstance().error("Please use the \"--help\" option to see a list of valid commands and options.", e);
System.exit(1);
return null; // dummy return (won't be reached) to avoid Eclipse complaint about possible NPE
// for "commandLine"
}

ArgumentContext argumentContext = new ArgumentContext(workingDirectory, commandLine);
Arg.processLogLevel(argumentContext, CLIOptionCollection.INSTANCE);

if (commandLine.hasOption(HELP)) {
if (argumentContext.getCommandLine().hasOption(HELP)) {
helpCmd.accept(opts);
return null;
}

if (commandLine.hasOption(Arg.HELP_LICENSES.option())) {
if (argumentContext.getCommandLine().hasOption(Arg.HELP_LICENSES.option())) {
new Licenses(createConfiguration(argumentContext), new PrintWriter(System.out, false, StandardCharsets.UTF_8)).printHelp();
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ public ArgumentContext parseCommands(final File workingDirectory, final String[]
* @return the CommandLine
* @throws ParseException on option parsing error.
*/
//@VisibleForTesting
CommandLine parseCommandLine(final Options opts, final String[] args) throws ParseException {
public static CommandLine parseCommandLine(final Options opts, final String[] args) throws ParseException {
try {
return DefaultParser.builder().setDeprecatedHandler(DeprecationReporter.getLogReporter())
.setAllowPartialMatching(true).build().parse(opts, args);
Expand All @@ -100,8 +99,7 @@ CommandLine parseCommandLine(final Options opts, final String[] args) throws Par
*/
private ArgumentContext parseCommands(final File workingDirectory, final String[] args,
final Options options) throws IOException, ParseException {
CommandLine commandLine = parseCommandLine(options, args);
ArgumentContext argumentContext = new ArgumentContext(workingDirectory, commandLine);
ArgumentContext argumentContext = new ArgumentContext(workingDirectory, options, args);
Arg.processLogLevel(argumentContext, uiOptionCollection);
populateConfiguration(argumentContext);
if (uiOptionCollection.isSelected(Arg.HELP_LICENSES)) {
Expand Down
64 changes: 51 additions & 13 deletions apache-rat-core/src/main/java/org/apache/rat/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@
package org.apache.rat;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

import org.apache.commons.cli.Options;
import org.apache.commons.io.function.IOSupplier;
import org.apache.rat.commandline.ArgumentContext;
import org.apache.rat.document.RatDocumentAnalysisException;
import org.apache.rat.help.Help;
import org.apache.rat.utils.DefaultLog;
Expand All @@ -42,34 +48,66 @@ public final class Report {
public static void main(final String[] args) throws Exception {
DefaultLog.getInstance().info(new VersionInfo().toString());


if (args == null || args.length == 0) {
DefaultLog.getInstance().info("Please use the \"--help\" option to see a " +
"list of valid commands and options, as you did not provide any arguments.");
System.exit(0);
}

ReportConfiguration configuration = OptionCollection.parseCommands(new File("."), args, Report::printUsage);
if (configuration != null) {
configuration.validate(DefaultLog.getInstance()::error);
Reporter reporter = new Reporter(configuration);
reporter.output();
reporter.writeSummary(DefaultLog.getInstance().asWriter());
Reporter.Output result = generateReport(CLIOptionCollection.INSTANCE, new File("."), args);
if (result != null) {
result.writeSummary(DefaultLog.getInstance().asWriter());

if (configuration.getClaimValidator().hasErrors()) {
configuration.getClaimValidator().logIssues(reporter.getClaimsStatistic());
if (result.getConfiguration().getClaimValidator().hasErrors()) {
result.getConfiguration().getClaimValidator().logIssues(result.getStatistic());
throw new RatDocumentAnalysisException(format("Issues with %s",
String.join(", ",
configuration.getClaimValidator().listIssues(reporter.getClaimsStatistic()))));
result.getConfiguration().getClaimValidator().listIssues(result.getStatistic()))));
}
}
}

/**
* Prints the usage message on {@code System.out}.
* @param opts The defined options.
* Prints the usage message on the specified output stream.
* @param out The OutputStream supplier
*/
private static void printUsage(final Options opts) {
new Help(System.out).printUsage(opts);
private static void printUsage(final Options options, final IOSupplier<OutputStream> out) {
try (OutputStream stream = out.get();
PrintWriter writer = new PrintWriter(stream, false, StandardCharsets.UTF_8)) {
new Help(writer).printUsage(options);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

/**
* Generates the report
* @param workingDirectory the directory that we are executing in
* @param args the arguments from the command line.
* @return The Client output.
* @throws Exception on error.
*/
static Reporter.Output generateReport(final CLIOptionCollection optionCollection, final File workingDirectory, final String[] args) throws Exception {
Reporter.Output output = null;
OptionCollectionParser optionParser = new OptionCollectionParser(optionCollection);
ArgumentContext argumentContext = optionParser.parseCommands(workingDirectory, args);
ReportConfiguration configuration = argumentContext.getConfiguration();
if (configuration != null) {
if (argumentContext.getCommandLine().hasOption(CLIOptionCollection.HELP)) {
printUsage(optionCollection.getOptions(), argumentContext.getConfiguration().getOutput());
} else if (!configuration.hasSource()) {
String msg = "No directories or files specified for scanning. Did you forget to close a multi-argument option?";
DefaultLog.getInstance().error(msg);
printUsage(optionCollection.getOptions(), argumentContext.getConfiguration().getOutput());
} else {
configuration.validate();
Reporter reporter = new Reporter(configuration);
output = reporter.execute();
output.format(configuration);
}
}
return output;
}

private Report() {
Expand Down
Loading
Loading