Files
ProjectCapture/content-tools/src/main/java/dev/projectcapture/contenttools/ContentTool.java
T

68 lines
2.3 KiB
Java

package dev.projectcapture.contenttools;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Path;
public final class ContentTool {
private ContentTool() {}
public static void main(String[] args) {
int exitCode = run(args, System.out, System.err);
if (exitCode != 0) {
System.exit(exitCode);
}
}
static int run(String[] args, PrintStream out, PrintStream err) {
if (args.length != 2) {
printUsage(err);
return 64;
}
Path contentRoot = Path.of(args[1]).toAbsolutePath().normalize();
return switch (args[0]) {
case "validate" -> validate(contentRoot, out, err);
case "digest" -> digest(contentRoot, out, err);
default -> {
err.println("Unknown command: " + args[0]);
printUsage(err);
yield 64;
}
};
}
private static int validate(Path contentRoot, PrintStream out, PrintStream err) {
ValidationReport report = new ContentValidator().validate(contentRoot);
if (!report.isValid()) {
err.println("Content validation failed with " + report.issues().size() + " issue(s):");
report.issues().forEach(issue -> err.println("- " + issue));
return 2;
}
out.println("Content validation passed: " + contentRoot);
return 0;
}
private static int digest(Path contentRoot, PrintStream out, PrintStream err) {
ValidationReport report = new ContentValidator().validate(contentRoot);
if (!report.isValid()) {
err.println("Digest refused because content validation failed:");
report.issues().forEach(issue -> err.println("- " + issue));
return 2;
}
try {
out.println(ContentBundle.load(contentRoot).digest());
return 0;
} catch (IOException | RuntimeException exception) {
err.println("Could not calculate digest: " + exception.getMessage());
return 2;
}
}
private static void printUsage(PrintStream stream) {
stream.println("Usage:");
stream.println(" content-tools validate <content-root>");
stream.println(" content-tools digest <content-root>");
}
}