Search in sources :

Example 6 with ArgumentException

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;
}
Also used : TypeLoader(net.morimekta.providence.reflect.TypeLoader) IOException(java.io.IOException) TreeSet(java.util.TreeSet) CProgram(net.morimekta.providence.reflect.contained.CProgram) ArgumentException(net.morimekta.console.args.ArgumentException) File(java.io.File) ThriftProgramParser(net.morimekta.providence.reflect.parser.ThriftProgramParser) PService(net.morimekta.providence.descriptor.PService)

Example 7 with ArgumentException

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);
}
Also used : TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) ArgumentException(net.morimekta.console.args.ArgumentException) ParseException(net.morimekta.providence.reflect.parser.ParseException) IOException(java.io.IOException) ArgumentParser(net.morimekta.console.args.ArgumentParser) SerializerException(net.morimekta.providence.serializer.SerializerException) TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) IOException(java.io.IOException) ParseException(net.morimekta.providence.reflect.parser.ParseException) SerializerException(net.morimekta.providence.serializer.SerializerException) ArgumentException(net.morimekta.console.args.ArgumentException)

Example 8 with ArgumentException

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);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArgumentException(net.morimekta.console.args.ArgumentException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 9 with ArgumentException

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);
}
Also used : ArgumentException(net.morimekta.console.args.ArgumentException) File(java.io.File)

Example 10 with ArgumentException

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());
    }
}
Also used : SimpleTypeRegistry(net.morimekta.providence.util.SimpleTypeRegistry) ProvidenceTools(net.morimekta.providence.tools.common.ProvidenceTools) ArgumentException(net.morimekta.console.args.ArgumentException) ProvidenceConfig(net.morimekta.providence.config.ProvidenceConfig) File(java.io.File) SerializerException(net.morimekta.providence.serializer.SerializerException) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException)

Aggregations

ArgumentException (net.morimekta.console.args.ArgumentException)17 File (java.io.File)9 IOException (java.io.IOException)9 ArgumentParser (net.morimekta.console.args.ArgumentParser)6 PService (net.morimekta.providence.descriptor.PService)5 TreeSet (java.util.TreeSet)4 TypeLoader (net.morimekta.providence.reflect.TypeLoader)4 CProgram (net.morimekta.providence.reflect.contained.CProgram)4 SerializerException (net.morimekta.providence.serializer.SerializerException)4 GeneratorFactory (net.morimekta.providence.generator.GeneratorFactory)3 MessageReader (net.morimekta.providence.mio.MessageReader)3 MessageWriter (net.morimekta.providence.mio.MessageWriter)3 ParseException (net.morimekta.providence.reflect.parser.ParseException)3 Format (net.morimekta.providence.tools.common.formats.Format)3 GenericUrl (com.google.api.client.http.GenericUrl)2 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)2 HttpTransport (com.google.api.client.http.HttpTransport)2 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)2 UncheckedIOException (java.io.UncheckedIOException)2 InetSocketAddress (java.net.InetSocketAddress)2