public int run()

in magnolify-tools/src/main/java/magnolify/tools/MagnolifyAvroTool.java [30:70]


  public int run(InputStream in, PrintStream out, PrintStream err, List<String> args) throws Exception {
    OptionParser optionParser = new OptionParser();
    OptionSpec<Integer> widthOption = optionParser
        .accepts("width", "Column width")
        .withOptionalArg()
        .ofType(Integer.class)
        .defaultsTo(80);
    OptionSpec<Void> sourceOption = optionParser
        .accepts("source", "Include source schema.");

    OptionSet optionSet = optionParser.parse(args.toArray(new String[0]));
    int width = optionSet.valueOf(widthOption);
    boolean source = optionSet.has(sourceOption);
    @SuppressWarnings("unchecked")
    List<String> argz = (List<String>) optionSet.nonOptionArguments();
    if (argz.size() != 1) {
      err.println("avro --width <width> --source input-file");
      err.println();
      err.println(getShortDescription());
      return 1;
    }

    Path path = new Path(argz.get(0));
    FSDataInputStream is = path.getFileSystem(new Configuration()).open(path);
    DataFileStream<GenericRecord> stream = new DataFileStream<>(is, new GenericDatumReader<>());
    Schema avroSchema = stream.getSchema();
    Record schema = AvroParser.parse(avroSchema);
    out.println("// Generated by magnolify-tools");
    out.println("// Source: " + path);
    out.println();

    if (source) {
      out.println("/*");
      out.println(avroSchema.toString(true));
      out.println("*/");
      out.println();
    }

    out.println(SchemaPrinter.print(schema, width));
    return 0;
  }