use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class Convert method run.
@SuppressWarnings("unchecked")
void run(String... args) {
try {
ArgumentParser cli = options.getArgumentParser("pvd", "Providence Converter");
try {
cli.parse(args);
if (options.showHelp()) {
System.out.println(cli.getProgramDescription());
System.out.println("Usage: " + cli.getSingleLineUsage());
System.out.println();
System.out.println("Example code to run:");
System.out.println("$ cat call.json | pvd -I thrift/ -S cal.Calculator");
System.out.println("$ pvd -i binary,file:my.data -o json_protocol -I thrift/ cal.Operation");
System.out.println();
System.out.println("Note that when handling service calls, only 1 call can be converted.");
System.out.println();
cli.printUsage(System.out);
System.out.println();
System.out.println("Available formats are:");
for (Format format : Format.values()) {
System.out.println(String.format(" - %-20s : %s", format.name(), format.desc));
}
return;
}
if (options.showVersion()) {
System.out.println(cli.getProgramDescription());
return;
}
if (options.listTypes) {
ProgramRegistry registry = options.getProgramRegistry();
for (ProgramTypeRegistry pr : registry.getLoadedRegistries()) {
CProgram program = pr.getProgram();
System.out.println(program.getProgramFilePath() + ":");
for (PDeclaredDescriptor dd : program.getDeclaredTypes()) {
if (dd instanceof CStructDescriptor) {
System.out.println(" struct " + dd.getQualifiedName());
} else if (dd instanceof CUnionDescriptor) {
System.out.println(" union " + dd.getQualifiedName());
} else if (dd instanceof CExceptionDescriptor) {
System.out.println(" exception " + dd.getQualifiedName());
}
}
for (PService s : program.getServices()) {
System.out.println(" service " + s.getQualifiedName());
}
}
return;
}
cli.validate();
if (options.getDefinition() == null) {
MessageReader in = options.getServiceInput();
MessageWriter out = options.getServiceOutput();
PService service = options.getServiceDefinition();
PServiceCall call = in.read(service);
in.close();
out.write(call);
out.separator();
out.close();
// TODO: Validate we don't have garbage data after call.
if (options.out.base64mime && options.out.file == null) {
System.out.println();
}
} else {
AtomicInteger num = new AtomicInteger(0);
int size = options.getInput().peek(m -> num.incrementAndGet()).collect(options.getOutput());
if (num.get() == 0 || size == 0) {
throw new IOException("No data");
}
if (options.out.base64mime && options.out.file == null) {
System.out.println();
}
}
return;
} catch (ArgumentException e) {
System.err.println(e.getMessage());
System.out.println("Usage: " + cli.getSingleLineUsage());
System.err.println();
System.err.println("Run $ pvd --help # for available options.");
if (options.verbose()) {
System.err.println();
e.printStackTrace();
}
} catch (SerializerException e) {
System.out.flush();
System.err.println();
System.err.println(e.asString());
if (options.verbose()) {
System.err.println();
e.printStackTrace();
}
} catch (UncheckedIOException | IOException e) {
System.out.flush();
System.err.println();
System.err.println("I/O error: " + e.getMessage());
if (options.verbose()) {
System.err.println();
e.printStackTrace();
}
}
} catch (Exception e) {
System.out.flush();
System.err.println();
System.err.println("Unchecked exception: " + e.getMessage());
if (options.verbose()) {
System.err.println();
e.printStackTrace();
}
}
exit(1);
}
Aggregations