Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

@CommandLine.Command
public class CommandOptions {
@CommandLine.Option(names = "--config", description = "Path to the config file.", defaultValue = "licenserc.toml")
@CommandLine.Option(names = "--config", description = "path to the config file", defaultValue = "licenserc.toml")
public File config;

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

package io.korandoru.hawkeye.command;

import io.korandoru.hawkeye.core.HawkEyeConfig;
import io.korandoru.hawkeye.core.LicenseChecker;
import io.korandoru.hawkeye.core.Report;
import io.korandoru.hawkeye.core.config.HawkEyeConfig;
import io.korandoru.hawkeye.core.report.Report;
import io.korandoru.hawkeye.core.report.ReportConstants;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
Expand All @@ -39,24 +40,29 @@ public class HawkEyeCommandCheck implements Callable<Integer> {

@Override
public Integer call() {
final HawkEyeConfig config = HawkEyeConfig.of(options.config);
final HawkEyeConfig config = HawkEyeConfig.of(options.config).build();
final LicenseChecker checker = new LicenseChecker(config);
final Report report = checker.call();

final List<String> unknownHeaderFiles = report.getResults().entrySet().stream()
.filter(e -> Report.Result.UNKNOWN.equals(e.getValue()))
.filter(e -> ReportConstants.RESULT_UNKNOWN.equals(e.getValue()))
.map(Map.Entry::getKey)
.toList();

final List<String> missingHeaderFiles = report.getResults().entrySet().stream()
.filter(e -> Report.Result.MISSING.equals(e.getValue()))
.filter(e -> ReportConstants.RESULT_MISSING.equals(e.getValue()))
.map(Map.Entry::getKey)
.toList();

if (!unknownHeaderFiles.isEmpty()) {
log.warn("Processing unknown files: {}", unknownHeaderFiles);
}
log.info("Found missing header files: {}", missingHeaderFiles);

if (missingHeaderFiles.isEmpty()) {
log.info("No missing header file has been found.");
} else {
log.info("Found missing header files: {}", missingHeaderFiles);
}

return missingHeaderFiles.isEmpty() ? 0 : 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

package io.korandoru.hawkeye.command;

import io.korandoru.hawkeye.core.HawkEyeConfig;
import io.korandoru.hawkeye.core.LicenseFormatter;
import io.korandoru.hawkeye.core.Report;
import io.korandoru.hawkeye.core.config.HawkEyeConfig;
import io.korandoru.hawkeye.core.report.Report;
import io.korandoru.hawkeye.core.report.ReportConstants;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
Expand All @@ -37,26 +38,34 @@ public class HawkEyeCommandFormat implements Callable<Integer> {
@CommandLine.Mixin
private CommandOptions options;

@CommandLine.Option(names = "--dry-run", description = "whether update file in place")
public boolean dryRun;

@Override
public Integer call() {
final HawkEyeConfig config = HawkEyeConfig.of(options.config);
final HawkEyeConfig config = HawkEyeConfig.of(options.config).dryRun(dryRun).build();
final LicenseFormatter formatter = new LicenseFormatter(config);
final Report report = formatter.call();

final List<String> unknownHeaderFiles = report.getResults().entrySet().stream()
.filter(e -> Report.Result.UNKNOWN.equals(e.getValue()))
.filter(e -> ReportConstants.RESULT_UNKNOWN.equals(e.getValue()))
.map(Map.Entry::getKey)
.toList();

final List<Map.Entry<String, Report.Result>> updatedHeaderFiles = report.getResults().entrySet().stream()
.filter(e -> e.getValue() != Report.Result.UNKNOWN)
.filter(e -> e.getValue() != Report.Result.NOOP)
final List<Map.Entry<String, String>> updatedHeaderFiles = report.getResults().entrySet().stream()
.filter(e -> !ReportConstants.RESULT_UNKNOWN.equals(e.getValue()))
.filter(e -> !ReportConstants.RESULT_NOOP.equals(e.getValue()))
.toList();

if (!unknownHeaderFiles.isEmpty()) {
log.warn("Processing unknown files: {}", unknownHeaderFiles);
}
log.info("Updated header for files: {}", updatedHeaderFiles);

if (updatedHeaderFiles.isEmpty()) {
log.info("All files have proper header.");
} else if (!dryRun) {
log.info("Updated header for files: {}", updatedHeaderFiles);
}

return updatedHeaderFiles.isEmpty() ? 0 : 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

package io.korandoru.hawkeye.command;

import io.korandoru.hawkeye.core.HawkEyeConfig;
import io.korandoru.hawkeye.core.LicenseRemover;
import io.korandoru.hawkeye.core.Report;
import io.korandoru.hawkeye.core.config.HawkEyeConfig;
import io.korandoru.hawkeye.core.report.Report;
import io.korandoru.hawkeye.core.report.ReportConstants;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
Expand All @@ -37,26 +38,34 @@ public class HawkEyeCommandRemove implements Callable<Integer> {
@CommandLine.Mixin
private CommandOptions options;

@CommandLine.Option(names = "--dry-run", description = "whether update file in place")
public boolean dryRun;

@Override
public Integer call() {
final HawkEyeConfig config = HawkEyeConfig.of(options.config);
final HawkEyeConfig config = HawkEyeConfig.of(options.config).dryRun(dryRun).build();
final LicenseRemover remover = new LicenseRemover(config);
final Report report = remover.call();

final List<String> unknownHeaderFiles = report.getResults().entrySet().stream()
.filter(e -> Report.Result.UNKNOWN.equals(e.getValue()))
.filter(e -> ReportConstants.RESULT_UNKNOWN.equals(e.getValue()))
.map(Map.Entry::getKey)
.toList();

final List<String> removedHeaderFiles = report.getResults().entrySet().stream()
.filter(e -> Report.Result.REMOVED.equals(e.getValue()))
.filter(e -> ReportConstants.RESULT_REMOVED.equals(e.getValue()))
.map(Map.Entry::getKey)
.toList();

if (!unknownHeaderFiles.isEmpty()) {
log.warn("Processing unknown files: {}", unknownHeaderFiles);
}
log.info("Removed header for files: {}", removedHeaderFiles);

if (removedHeaderFiles.isEmpty()) {
log.info("No file has been removed header.");
} else if (!dryRun) {
log.info("Removed header for files: {}", removedHeaderFiles);
}

return removedHeaderFiles.isEmpty() ? 0 : 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,25 @@

package io.korandoru.hawkeye.core;

import io.korandoru.hawkeye.core.config.HawkEyeConfig;
import io.korandoru.hawkeye.core.document.Document;
import io.korandoru.hawkeye.core.header.Header;
import io.korandoru.hawkeye.core.report.Report;
import io.korandoru.hawkeye.core.report.ReportConstants;

public class LicenseChecker extends LicenseProcessor {

public LicenseChecker(HawkEyeConfig config) {
super(config, Report.Action.CHECK);
super(config, ReportConstants.ACTION_CHECK);
}

@Override
protected void onHeaderNotFound(Document document, Header header, Report report) {
report.add(document.getFile(), Report.Result.MISSING);
report.add(document.getFile(), ReportConstants.RESULT_MISSING);
}

@Override
protected void onExistingHeader(Document document, Header header, Report report) {
report.add(document.getFile(), Report.Result.PRESENT);
report.add(document.getFile(), ReportConstants.RESULT_PRESENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,34 @@

package io.korandoru.hawkeye.core;

import io.korandoru.hawkeye.core.config.HawkEyeConfig;
import io.korandoru.hawkeye.core.document.Document;
import io.korandoru.hawkeye.core.header.Header;
import io.korandoru.hawkeye.core.report.Report;
import io.korandoru.hawkeye.core.report.ReportConstants;

public class LicenseFormatter extends LicenseProcessor {

public LicenseFormatter(HawkEyeConfig config) {
super(config, Report.Action.FORMAT);
super(config, ReportConstants.ACTION_FORMAT);
}

@Override
protected void onHeaderNotFound(Document document, Header header, Report report) {
if (document.headerDetected()) {
document.removeHeader();
report.add(document.getFile(), Report.Result.REPLACED);
document.updateHeader(header);
report.add(document.getFile(), ReportConstants.RESULT_REPLACED);
} else {
report.add(document.getFile(), Report.Result.ADDED);
document.updateHeader(header);
report.add(document.getFile(), ReportConstants.RESULT_ADDED);
}
document.updateHeader(header);
document.save();

LicenseProcessUtils.save(document, config.isDryRun(), ".formatted");
}

@Override
protected void onExistingHeader(Document document, Header header, Report report) {
report.add(document.getFile(), Report.Result.NOOP);
report.add(document.getFile(), ReportConstants.RESULT_NOOP);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2023 Korandoru Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.korandoru.hawkeye.core;

import io.korandoru.hawkeye.core.document.Document;
import java.io.File;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@UtilityClass
public class LicenseProcessUtils {

public static void save(Document document, boolean dryRun, String suffix) {
if (!dryRun) {
document.save();
return;
}

final String filename = document.getFile().getName() + suffix;
final File copy = new File(document.getFile().getParentFile(), filename);
document.saveTo(copy);

log.info("Result saved to: {}", copy);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

package io.korandoru.hawkeye.core;

import io.korandoru.hawkeye.core.config.HawkEyeConfig;
import io.korandoru.hawkeye.core.document.Document;
import io.korandoru.hawkeye.core.document.DocumentFactory;
import io.korandoru.hawkeye.core.document.DocumentPropertiesLoader;
import io.korandoru.hawkeye.core.document.DocumentType;
import io.korandoru.hawkeye.core.header.Header;
import io.korandoru.hawkeye.core.header.HeaderType;
import io.korandoru.hawkeye.core.report.Report;
import io.korandoru.hawkeye.core.report.ReportConstants;
import io.korandoru.hawkeye.core.resource.HeaderSource;
import io.korandoru.hawkeye.core.resource.ResourceFinder;
import java.io.IOException;
Expand All @@ -38,8 +41,8 @@
@RequiredArgsConstructor
public abstract class LicenseProcessor implements Callable<Report> {

private final HawkEyeConfig config;
private final Report.Action action;
protected final HawkEyeConfig config;
private final String action;

@SneakyThrows
@Override
Expand Down Expand Up @@ -97,7 +100,7 @@ public Report call() {
}

if (document.isNotSupported()) {
report.add(document.getFile(), Report.Result.UNKNOWN);
report.add(document.getFile(), ReportConstants.RESULT_UNKNOWN);
} else if (document.hasHeader(header, config.isStrictCheck())) {
onExistingHeader(document, header, report);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@

package io.korandoru.hawkeye.core;

import io.korandoru.hawkeye.core.config.HawkEyeConfig;
import io.korandoru.hawkeye.core.document.Document;
import io.korandoru.hawkeye.core.header.Header;
import io.korandoru.hawkeye.core.report.Report;
import io.korandoru.hawkeye.core.report.ReportConstants;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LicenseRemover extends LicenseProcessor {

public LicenseRemover(HawkEyeConfig config) {
super(config, Report.Action.REMOVE);
super(config, ReportConstants.ACTION_REMOVE);
}

@Override
Expand All @@ -36,12 +41,14 @@ protected void onExistingHeader(Document document, Header header, Report report)
}

private void remove(Document document, Report report) {
if (document.headerDetected()) {
document.removeHeader();
document.save();
report.add(document.getFile(), Report.Result.REMOVED);
} else {
report.add(document.getFile(), Report.Result.NOOP);
if (!document.headerDetected()) {
report.add(document.getFile(), ReportConstants.RESULT_NOOP);
return;
}

document.removeHeader();
report.add(document.getFile(), ReportConstants.RESULT_REMOVED);

LicenseProcessUtils.save(document, config.isDryRun(), ".removed");
}
}
Loading