Skip to content

Commit

Permalink
feat(cli): Add an output-format flag
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Feb 22, 2023
1 parent 6b20104 commit 1740a6e
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,25 @@ protected int execute() {
return 1;
}

for (JarProcessor.JarFileResult<BytecodeVersions> result : results) {
output(result);
if (results.size() > 1) parent().getOut().println("");
}
output(results);
report(results);

return 0;
}

private void output(Set<JarProcessor.JarFileResult<BytecodeVersions>> results) {
Node root = createRootNode();
for (JarProcessor.JarFileResult<BytecodeVersions> result : results) {
if (null == outputFormat) {
output(result);
} else {
buildReport(outputFormat, root, result);
writeOutput(resolveFormatter(outputFormat).write(root));
}
if (results.size() > 1) parent().getOut().println("");
}
}

private void output(JarProcessor.JarFileResult<BytecodeVersions> result) {
parent().getOut().println($$("output.subject", result.getJarFileName()));
BytecodeVersions bytecodeVersions = result.getResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Consumer;

import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static java.util.stream.Collectors.toList;
import static java.util.Collections.singleton;
import static java.util.stream.Collectors.toSet;
import static org.kordamp.jarviz.cli.internal.Colorizer.bool;
import static org.kordamp.jarviz.cli.internal.Colorizer.colorize;
import static org.kordamp.jarviz.util.StringUtils.isNotBlank;
Expand Down Expand Up @@ -87,7 +88,10 @@ public abstract class AbstractJarvizSubcommand<C extends IO> extends AbstractCom
protected Path reportPath;

@CommandLine.Option(names = {"--report-format"}, paramLabel = "<format>")
String[] reportFormats;
Format[] reportFormats;

@CommandLine.Option(names = {"--output-format"}, paramLabel = "<format>")
protected Format outputFormat;

@Override
protected C parent() {
Expand Down Expand Up @@ -121,14 +125,13 @@ protected Path resolveReportPath(Format format) {
return Paths.get(reportPath.toAbsolutePath() + "." + format.toString().toLowerCase(Locale.ROOT));
}

protected List<Format> validateReportFormats() {
protected Set<Format> validateReportFormats() {
if (null != reportPath && null == reportFormats || reportFormats.length == 0) {
return Collections.singletonList(Format.TXT);
return singleton(Format.TXT);
}

return collectEntries(reportFormats).stream()
.map(Format::of)
.collect(toList());
return Arrays.stream(reportFormats)
.collect(toSet());
}

protected Formatter resolveFormatter(Format format) {
Expand All @@ -153,18 +156,22 @@ protected Node appendSubject(Node root, Path jarPath, String command, Consumer<N
Node subjects = root.getChildren().isEmpty() ? root.array($("report.key.subjects")) : root.getChildren().get(0);
Node resultNode = subjects
.collapsable($("report.key.subject"))
.node($("report.key.command")).value(command).end()
.node($("report.key.jar"))
.node($("report.key.file")).value(jarPath.getFileName()).end()
.node($("report.key.size")).value(fileSize(jarPath)).end()
.node($("report.key.sha256")).value(sha256(jarPath)).end()
.end()
.node($("report.key.result"));
.node($("report.key.command")).value(command).end()
.node($("report.key.jar"))
.node($("report.key.file")).value(jarPath.getFileName()).end()
.node($("report.key.size")).value(fileSize(jarPath)).end()
.node($("report.key.sha256")).value(sha256(jarPath)).end()
.end()
.node($("report.key.result"));
result.accept(resultNode);

return root;
}

protected void writeOutput(String content) {
parent().getOut().println(content);
}

protected void writeReport(String content, Format format) {
Path reportPath = resolveReportPath(format);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,25 @@ protected int execute() {
return 1;
}

for (JarProcessor.JarFileResult<Optional<String>> result : results) {
output(result);
if (results.size() > 1) parent().getOut().println("");
}
output(results);
report(results);

return 0;
}

private void output(Set<JarProcessor.JarFileResult<Optional<String>>> results) {
Node root = createRootNode();
for (JarProcessor.JarFileResult<Optional<String>> result : results) {
if (null == outputFormat) {
output(result);
} else {
buildReport(root, result);
writeOutput(resolveFormatter(outputFormat).write(root));
}
if (results.size() > 1) parent().getOut().println("");
}
}

private void output(JarProcessor.JarFileResult<Optional<String>> result) {
if (result.getResult().isPresent()) {
parent().getOut().println($$("output.subject", result.getJarFileName()));
Expand All @@ -85,7 +95,7 @@ private void report(Set<JarProcessor.JarFileResult<Optional<String>>> results) {
}

private void buildReport(Node root, JarProcessor.JarFileResult<Optional<String>> result) {
appendSubject(createRootNode(), result.getJarPath(), "manifest query", resultNode -> {
appendSubject(root, result.getJarPath(), "manifest query", resultNode -> {
if (isNotBlank(sectionName)) {
resultNode.node($("report.key.section.name")).value(sectionName).end();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,25 @@ protected int execute() {
return 1;
}

for (JarProcessor.JarFileResult<Optional<java.util.jar.Manifest>> result : results) {
output(result);
if (results.size() > 1) parent().getOut().println("");
}
output(results);
report(results);

return 0;
}

private void output(Set<JarProcessor.JarFileResult<Optional<java.util.jar.Manifest>>> results) {
Node root = createRootNode();
for (JarProcessor.JarFileResult<Optional<java.util.jar.Manifest>> result : results) {
if (null == outputFormat) {
output(result);
} else {
buildReport(root, result);
writeOutput(resolveFormatter(outputFormat).write(root));
}
if (results.size() > 1) parent().getOut().println("");
}
}

private void output(JarProcessor.JarFileResult<Optional<java.util.jar.Manifest>> result) {
if (result.getResult().isPresent()) {
parent().getOut().println($$("output.subject", result.getJarFileName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,25 @@ protected int execute() {
return 1;
}

for (JarProcessor.JarFileResult<java.lang.module.ModuleDescriptor> result : results) {
output(result);
if (results.size() > 1) parent().getOut().println("");
}
output(results);
report(results);

return 0;
}

private void output(Set<JarProcessor.JarFileResult<java.lang.module.ModuleDescriptor>> results) {
Node root = createRootNode();
for (JarProcessor.JarFileResult<java.lang.module.ModuleDescriptor> result : results) {
if (null == outputFormat) {
output(result);
} else {
buildReport(outputFormat, root, result);
writeOutput(resolveFormatter(outputFormat).write(root));
}
if (results.size() > 1) parent().getOut().println("");
}
}

private void output(JarProcessor.JarFileResult<java.lang.module.ModuleDescriptor> result) {
parent().getOut().println($$("output.subject", result.getJarFileName()));
java.lang.module.ModuleDescriptor md = result.getResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,25 @@ protected int execute() {
return 1;
}

for (JarProcessor.JarFileResult<org.kordamp.jarviz.core.model.ModuleName> result : results) {
output(result);
if (results.size() > 1) parent().getOut().println("");
}
output(results);
report(results);

return 0;
}

private void output(Set<JarProcessor.JarFileResult<org.kordamp.jarviz.core.model.ModuleName>> results) {
Node root = createRootNode();
for (JarProcessor.JarFileResult<org.kordamp.jarviz.core.model.ModuleName> result : results) {
if (null == outputFormat) {
output(result);
} else {
buildReport(root, result);
writeOutput(resolveFormatter(outputFormat).write(root));
}
if (results.size() > 1) parent().getOut().println("");
}
}

private void output(JarProcessor.JarFileResult<org.kordamp.jarviz.core.model.ModuleName> result) {
parent().getOut().println($$("output.subject", result.getJarFileName()));
org.kordamp.jarviz.core.model.ModuleName moduleName = result.getResult();
Expand Down Expand Up @@ -96,7 +106,7 @@ private void buildReport(Node root, JarProcessor.JarFileResult<org.kordamp.jarvi
.node($("report.key.automatic")).value(!EXPLICIT.equals(resolveSource(moduleName))).end()
.node($("report.key.valid")).value(moduleName.isValid()).end();
if (!moduleName.isValid()) {
createRootNode().node($("report.key.reason")).value(moduleName.getReason()).end();
resultNode.node($("report.key.reason")).value(moduleName.getReason()).end();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,26 @@ protected int execute() {
SplitPackageJarProcessor processor = new SplitPackageJarProcessor(jarFileResolver);

Set<JarProcessor.JarFileResult<Set<String>>> results = processor.getResult();
for (JarProcessor.JarFileResult<Set<String>> result : results) {
output(result);
if (results.size() > 1) parent().getOut().println("");
}

output(results);
report(results);

return 0;
}

private void output(Set<JarProcessor.JarFileResult<Set<String>>> results) {
Node root = createRootNode();
for (JarProcessor.JarFileResult<Set<String>> result : results) {
if (null == outputFormat) {
output(result);
} else {
buildReport(outputFormat, root, result);
writeOutput(resolveFormatter(outputFormat).write(root));
}
if (results.size() > 1) parent().getOut().println("");
}
}

private void output(JarProcessor.JarFileResult<Set<String>> result) {
parent().getOut().println($$("output.subject", result.getJarFileName()));
result.getResult().forEach(parent().getOut()::println);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,25 @@ protected int execute() {
return 1;
}

for (JarProcessor.JarFileResult<Optional<Set<String>>> result : results) {
output(result);
if (results.size() > 1) parent().getOut().println("");
}
output(results);
report(results);

return 0;
}

private void output(Set<JarProcessor.JarFileResult<Optional<Set<String>>>> results) {
Node root = createRootNode();
for (JarProcessor.JarFileResult<Optional<Set<String>>> result : results) {
if (null == outputFormat) {
output(result);
} else {
buildReport(outputFormat, root, result);
writeOutput(resolveFormatter(outputFormat).write(root));
}
if (results.size() > 1) parent().getOut().println("");
}
}

private void output(JarProcessor.JarFileResult<Optional<Set<String>>> result) {
if (result.getResult().isPresent()) {
parent().getOut().println($$("output.subject", result.getJarFileName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,25 @@ protected int execute() {
return 1;
}

for (JarProcessor.JarFileResult<Optional<Set<String>>> result : results) {
output(result);
if (results.size() > 1) parent().getOut().println("");
}
output(results);
report(results);

return 0;
}

private void output(Set<JarProcessor.JarFileResult<Optional<Set<String>>>> results) {
Node root = createRootNode();
for (JarProcessor.JarFileResult<Optional<Set<String>>> result : results) {
if (null == outputFormat) {
output(result);
} else {
buildReport(outputFormat, root, result);
writeOutput(resolveFormatter(outputFormat).write(root));
}
if (results.size() > 1) parent().getOut().println("");
}
}

private void output(JarProcessor.JarFileResult<Optional<Set<String>>> result) {
if (result.getResult().isPresent()) {
parent().getOut().println($$("output.subject", result.getJarFileName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ section-name = Name of a manifest section.
service-name = Name of a service interface.
bytecode-version = Bytecode version to search.
java-version = Java version to search.
output-format = Output format to use.
report-format = Report format to use. 🔁
report-path = Path to report filename (without extension)

Expand Down

0 comments on commit 1740a6e

Please sign in to comment.