use of net.morimekta.console.args.ArgumentException in project providence by morimekta.
the class RPCOptions method getDefinition.
public PService getDefinition() throws ParseException, IOException {
Map<String, File> includeMap = FormatUtils.getIncludeMap(getRc(), includes);
if (service.isEmpty()) {
throw new ArgumentException("Missing service type name");
}
Set<File> rootSet = new TreeSet<File>();
for (File file : includeMap.values()) {
rootSet.add(file.getParentFile());
}
String programName = service.substring(0, service.lastIndexOf("."));
TypeLoader loader = new TypeLoader(rootSet, new ThriftProgramParser());
try {
if (!includeMap.containsKey(programName)) {
throw new ArgumentException("No program " + programName + " found in include path.\n" + "Found: " + Strings.join(", ", new TreeSet<Object>(includeMap.keySet())));
}
loader.load(includeMap.get(programName));
} catch (IOException e) {
throw new ArgumentException(e.getLocalizedMessage());
}
String filePath = includeMap.get(programName).getCanonicalFile().getAbsolutePath();
PService srv = loader.getProgramRegistry().getService(service, null);
if (srv == null) {
CProgram document = loader.getProgramRegistry().registryForPath(filePath).getProgram();
Set<String> services = new TreeSet<>(document.getServices().stream().map(s -> s.getQualifiedName()).collect(Collectors.toSet()));
throw new ArgumentException("Unknown service %s in %s.\n" + "Found %s", service, programName, services.size() == 0 ? "none" : Strings.join(", ", services));
}
return srv;
}
use of net.morimekta.console.args.ArgumentException in project providence by morimekta.
the class Config method run.
public void run(String... args) {
ConfigOptions op = new ConfigOptions(tty);
try {
ArgumentParser cli = op.getArgumentParser("pvdcfg", "Providence Config Tool");
try {
cli.parse(args);
if (op.showHelp()) {
System.out.println(cli.getDescription() + " - " + getVersionString());
System.out.println("Usage: " + cli.getSingleLineUsage());
System.out.println();
cli.printUsage(System.out);
System.out.println();
System.out.println("Available Commands:");
System.out.println();
op.getCommandSet().printUsage(System.out);
return;
} else if (op.showVersion()) {
System.out.println(cli.getDescription() + " - " + getVersionString());
return;
}
cli.validate();
op.execute();
return;
} catch (ArgumentException e) {
System.err.println("Invalid argument: " + e.getMessage());
System.err.println("Usage: " + cli.getSingleLineUsage());
if (op.verbose()) {
e.printStackTrace();
}
} catch (ParseException e) {
System.out.flush();
System.err.println(e.asString());
if (op.verbose()) {
System.err.println();
e.printStackTrace();
}
} catch (TokenizerException e) {
System.out.flush();
System.err.println(e.asString());
if (op.verbose()) {
System.err.println();
e.printStackTrace();
}
} catch (SerializerException e) {
System.out.flush();
System.err.println("Serialization error: " + e.toString());
if (op.verbose()) {
System.err.println();
e.printStackTrace();
}
} catch (IOException | RuntimeException e) {
System.out.flush();
System.err.println("IO Error: " + e.toString());
if (op.verbose()) {
System.err.println();
e.printStackTrace();
}
}
} catch (Exception e) {
System.out.flush();
System.err.println("Unhandled exception: " + e.toString());
if (op.verbose()) {
System.err.println();
e.printStackTrace();
}
}
exit(1);
}
use of net.morimekta.console.args.ArgumentException in project providence by morimekta.
the class FormatUtils method getInputStream.
private static InputStream getInputStream(ConvertStream in) throws IOException {
InputStream is;
if (in.file != null) {
File file = in.file.getCanonicalFile();
if (!file.exists()) {
throw new ArgumentException("%s does not exists", file.getAbsolutePath());
}
if (!file.isFile()) {
throw new ArgumentException("%s is not a file", file.getAbsolutePath());
}
is = new FileInputStream(file);
} else {
is = new BufferedInputStream(System.in) {
@Override
public void close() {
// ignore
}
};
}
if (in.base64mime) {
is = Base64.getMimeDecoder().wrap(is);
} else if (in.base64) {
is = Base64.getDecoder().wrap(is);
}
return new BufferedInputStream(is);
}
use of net.morimekta.console.args.ArgumentException in project providence by morimekta.
the class ConvertStreamParser method parse.
@Override
public ConvertStream parse(@Nonnull String next) {
Format format = defaultStream.format;
File file = defaultStream.file;
boolean base64 = defaultStream.base64;
boolean base64mime = defaultStream.base64mime;
for (; ; ) {
if (next.startsWith("file:")) {
file = new File(next.substring(5));
break;
}
String[] parts = next.split("[,]", 2);
if ("base64".equals(parts[0])) {
base64 = true;
base64mime = false;
} else if ("base64mime".equals(parts[0])) {
base64mime = true;
base64 = false;
} else {
try {
format = Format.valueOf(parts[0]);
} catch (IllegalArgumentException iae) {
throw new ArgumentException("No such format '" + parts[0] + "'");
}
}
if (parts.length == 1) {
break;
} else {
next = parts[1];
}
}
return new ConvertStream(format, file, base64, base64mime);
}
use of net.morimekta.console.args.ArgumentException in project providence by morimekta.
the class FormatUtils method collectConfigIncludes.
public static void collectConfigIncludes(File rc, Map<String, File> includes) throws IOException {
if (!rc.exists()) {
return;
}
rc = rc.getCanonicalFile();
if (!rc.isFile()) {
throw new ProvidenceConfigException("Rc file is not a file " + rc.getPath());
}
try {
SimpleTypeRegistry registry = new SimpleTypeRegistry();
registry.registerRecursively(ProvidenceTools.kDescriptor);
ProvidenceConfig loader = new ProvidenceConfig(registry);
ProvidenceTools config = loader.getConfig(rc);
if (config.hasIncludes()) {
File basePath = rc.getParentFile();
if (config.hasIncludesBasePath()) {
String base = config.getIncludesBasePath();
if (base.charAt(0) == '~') {
base = System.getenv("HOME") + base.substring(1);
}
basePath = new File(base);
if (!basePath.exists() || !basePath.isDirectory()) {
throw new ProvidenceConfigException("Includes Base path in " + rc.getPath() + " is not a directory: " + basePath);
}
}
for (String path : config.getIncludes()) {
File include = new File(basePath, path);
collectIncludes(include, includes);
}
}
} catch (SerializerException e) {
System.err.println("Config error: " + e.getMessage());
System.err.println(e.asString());
System.err.println();
throw new ArgumentException(e, "Exception when parsing " + rc.getCanonicalFile());
}
}
Aggregations