Skip to content

Commit

Permalink
RAT-320: add -o/--output option to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
jbonofre committed Nov 26, 2023
1 parent 541072f commit 45bf247
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 15 deletions.
7 changes: 7 additions & 0 deletions apache-rat-core/src/main/java/org/apache/rat/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ public static final void main(String[] args) throws Exception {
static ReportConfiguration createConfiguration(String baseDirectory, CommandLine cl) throws IOException {
final ReportConfiguration configuration = new ReportConfiguration();

if (cl.hasOption('o')) {
configuration.setOut(new File(cl.getOptionValue('o')));
}

if (cl.hasOption('a') || cl.hasOption('A')) {
configuration.setAddLicenseHeaders(cl.hasOption('f') ? AddLicenseHeaders.FORCED : AddLicenseHeaders.TRUE);
configuration.setCopyrightMessage(cl.getOptionValue("c"));
Expand Down Expand Up @@ -261,6 +265,9 @@ static Options buildOptions() {
Option help = new Option(HELP, "help", false, "Print help for the RAT command line interface and exit");
opts.addOption(help);

Option out = new Option("o", "out", true, "Define the output file where to write report (default is System.out)");
opts.addOption(out);

String defaultHandlingText = " By default all approved default licenses are used";
Option noDefaults = new Option(null, NO_DEFAULTS, false, "Ignore default configuration." + defaultHandlingText);
opts.addOption(noDefaults);
Expand Down
36 changes: 36 additions & 0 deletions apache-rat-core/src/test/java/org/apache/rat/ReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@

import static org.junit.Assert.assertNotNull;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.ParseException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;

public class ReportTest {
Expand All @@ -45,4 +52,33 @@ public void testDefaultConfiguration() throws ParseException, IOException {
ReportConfigurationTest.validateDefault(config);
}

@Test
public void testOutputOption() throws Exception {
CommandLine cl = new DefaultParser().parse(Report.buildOptions(), new String[]{ "-o", "target/test" });
ReportConfiguration config = Report.createConfiguration("target/test-classes/elements", cl);
Reporter.report(config);
File output = new File("target/test");
Assert.assertTrue(output.exists());
String content = FileUtils.readFileToString(output, StandardCharsets.UTF_8);
Assert.assertTrue(content.contains("2 Unknown Licenses"));
Assert.assertTrue(content.contains("target/test-classes/elements/Source.java"));
Assert.assertTrue(content.contains("target/test-classes/elements/sub/Empty.txt"));
}

@Test
public void testDefaultOutput() throws Exception {
PrintStream origin = System.out;
File output = new File("target/sysout");
System.setOut(new PrintStream(output));
CommandLine cl = new DefaultParser().parse(Report.buildOptions(), new String[]{});
ReportConfiguration config = Report.createConfiguration("target/test-classes/elements", cl);
Reporter.report(config);
Assert.assertTrue(output.exists());
String content = FileUtils.readFileToString(output, StandardCharsets.UTF_8);
Assert.assertTrue(content.contains("2 Unknown Licenses"));
Assert.assertTrue(content.contains("target/test-classes/elements/Source.java"));
Assert.assertTrue(content.contains("target/test-classes/elements/sub/Empty.txt"));
System.setOut(origin);
}

}
26 changes: 18 additions & 8 deletions apache-rat/README-CLI.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ java -jar apache-rat-${project.version}.jar
Command Line Options
====================

usage: java rat.report [options] [DIR|TARBALL]
Options
-A,--addLicense Add the default license header to any file
usage: java -jar apache-rat/target/apache-rat-{project.version}.jar
[options] [DIR|TARBALL]

Available options
-a,--addLicence Add the default license header to any file
with an unknown license that is not in the
exclusion list. By default new files will
be created with the license header, to
force the modification of existing files
use the --force option.
-a,--addlicense Add the default license header to any file
-A,--addLicense Add the default license header to any file
with an unknown license that is not in the
exclusion list. By default new files will
be created with the license header, to
Expand All @@ -63,19 +65,27 @@ Options
"Copyright 2008 Foo"
-d,--dir Used to indicate source when using
--exclude
-E,--exclude-file <fileName> Excludes files matching regular expression
in <file> Note that --dir is required when
using this parameter.
-e,--exclude <expression> Excludes files matching wildcard
<expression>. Note that --dir is required
when using this parameter. Allows multiple
arguments.
-E,--exclude-file <fileName> Excludes files matching regular expression
in <file> Note that --dir is required when
using this parameter.
-f,--force Forces any changes in files to be written
directly to the source files (i.e. new
files are not created)
-h,--help Print help for the Rat command line
-h,--help Print help for the RAT command line
interface and exit
--licenses <arg> File names or URLs for license definitions
--list-approved-families List all defined license families
--list-licenses List all active licenses
--no-default-licenses Ignore default configuration. By default
all approved default licenses are used
-o,--out <arg> Define the output file where to write
report (default is System.out)
-s,--stylesheet <arg> XSLT stylesheet to use when creating the
report. Not compatible with -x
-x,--xml Output the report in raw XML format. Not
compatible with -s

21 changes: 14 additions & 7 deletions apache-rat/src/site/apt/index.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ java -jar apache-rat/target/apache-rat-${project.version}.jar

+------------------------------------------+
usage: java -jar apache-rat/target/apache-rat-${project.version}.jar
[options] [DIR|TARBALL]
[options] [DIR|TARBALL]

Available options
-A,--addLicense Add the default license header to any file
-a,--addLicence Add the default license header to any file
with an unknown license that is not in the
exclusion list. By default new files will
be created with the license header, to
force the modification of existing files
use the --force option.
-a,--addlicense Add the default license header to any file
-A,--addLicense Add the default license header to any file
with an unknown license that is not in the
exclusion list. By default new files will
be created with the license header, to
Expand All @@ -96,18 +96,25 @@ Available options
"Copyright 2008 Foo"
-d,--dir Used to indicate source when using
--exclude
-E,--exclude-file <fileName> Excludes files matching regular expression
in <file> Note that --dir is required when
using this parameter.
-e,--exclude <expression> Excludes files matching wildcard
<expression>. Note that --dir is required
when using this parameter. Allows multiple
arguments.
-E,--exclude-file <fileName> Excludes files matching regular expression
in <file> Note that --dir is required when
using this parameter.
-f,--force Forces any changes in files to be written
directly to the source files (i.e. new
files are not created)
-h,--help Print help for the Rat command line
-h,--help Print help for the RAT command line
interface and exit
--licenses <arg> File names or URLs for license definitions
--list-approved-families List all defined license families
--list-licenses List all active licenses
--no-default-licenses Ignore default configuration. By default
all approved default licenses are used
-o,--out <arg> Define the output file where to write
report (default is System.out)
-s,--stylesheet <arg> XSLT stylesheet to use when creating the
report. Not compatible with -x
-x,--xml Output the report in raw XML format. Not
Expand Down

0 comments on commit 45bf247

Please sign in to comment.