Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cli tool to allow schema directory #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
56 changes: 52 additions & 4 deletions src/main/java/com/github/fge/jsonschema/main/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,18 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import static com.github.fge.jsonschema.main.cli.RetCode.*;

public final class Main
{
private static final String DIRECTORY = "directory";

private static final HelpFormatter HELP = new CustomHelpFormatter();

private static final ObjectMapper MAPPER = JacksonUtils.newMapper();
Expand All @@ -63,17 +68,21 @@ public static void main(final String... args)
"no output; exit with the relevant return code (see below)");
parser.accepts("syntax",
"check the syntax of schema(s) given as argument(s)");
parser.accepts(DIRECTORY,
"directory which contains the schema files. If this parameter is provided then you don't need to provide files in argument list")
.withRequiredArg();
parser.accepts("fakeroot",
"pretend that the current directory is absolute URI \"uri\"")
.withRequiredArg();
parser.formatHelpWith(HELP);

final OptionSet optionSet;
final boolean isSyntax;
final int requiredArgs;
int requiredArgs;

Reporter reporter = Reporters.DEFAULT;
String fakeRoot = null;
String schemaDirectoryPath = null;

try {
optionSet = parser.parse(args);
Expand All @@ -100,22 +109,43 @@ public static void main(final String... args)
if (optionSet.has("fakeroot"))
fakeRoot = (String) optionSet.valueOf("fakeroot");

if (optionSet.has(DIRECTORY))
schemaDirectoryPath = (String) optionSet.valueOf(DIRECTORY);

isSyntax = optionSet.has("syntax");
requiredArgs = isSyntax ? 1 : 2;

// If directory argument is present then there is no other argument expected
requiredArgs = optionSet.has(DIRECTORY) ? 0 : requiredArgs;

@SuppressWarnings("unchecked")
final List<String> arguments
= (List<String>) optionSet.nonOptionArguments();

if (arguments.size() < requiredArgs) {
System.err.println("missing arguments");
parser.printHelpOn(System.err);
System.exit(CMD_ERROR.get());
}

final List<File> files = Lists.newArrayList();
for (final String target: arguments)
files.add(new File(target).getCanonicalFile());

if (schemaDirectoryPath != null) {

File directory = new File(schemaDirectoryPath);
if(!directory.isDirectory()) {
System.err.println(String.format("Given directory path(%s) is not a directory",
schemaDirectoryPath));
parser.printHelpOn(System.err);
System.exit(CMD_ERROR.get());
} else {
files.addAll(getFilesFromGivenDirectory(directory));
}

} else {
for (final String target: arguments)
files.add(new File(target).getCanonicalFile());
}

if (optionSet.has("brief"))
reporter = Reporters.BRIEF;
Expand Down Expand Up @@ -143,6 +173,24 @@ else if (optionSet.has("quiet")) {
syntaxValidator = factory.getSyntaxValidator();
}

private static List<File> getFilesFromGivenDirectory(File directory)
throws IOException {
List<File> files = new ArrayList<File>();
Queue<File> queue = new LinkedList<File>();
queue.offer(directory);
while(!queue.isEmpty()) {
File tempDirectory = queue.poll();
for(File entry : tempDirectory.listFiles()) {
if(entry.isFile()) {
files.add(entry.getCanonicalFile());
} else {
queue.offer(entry);
}
}
}
return files;
}

private void proceed(final Reporter reporter, final List<File> files,
final boolean isSyntax)
throws IOException, ProcessingException
Expand Down