public static void main()

in runtime/src/main/java/com/google/cloud/verticals/foundations/dataharmonization/Main.java [78:133]


  public static void main(String[] args) throws Exception {
    try {
      CommandLineParser parser = new DefaultParser();
      CommandLine cmd = parser.parse(options, args);

      if (cmd.hasOption("help")) {
        printHelp();
        return;
      }

      Path outputDir =
          FileSystems.getDefault().getPath(cmd.hasOption("o") ? cmd.getOptionValue("o") : STDOUT);
      if (cmd.hasOption("o")) {
        File outputDirFile = outputDir.toFile();
        if (!outputDirFile.exists()) {
          if (!outputDirFile.mkdirs()) {
            throw new IOException(String.format("Failed to create directory %s.", outputDir));
          }
        } else if (!outputDirFile.isDirectory()) {
          throw new IllegalArgumentException(
              String.format("Output directory %s exists but was not a directory.", outputDir));
        }
      }

      Map<Path, Data> inputData = new HashMap<>();
      if (cmd.hasOption("i")) {
        String[] filePaths = cmd.getOptionValues("i");
        inputData =
            stream(filePaths)
                .collect(Collectors.toMap(FileSystems.getDefault()::getPath, Main::readJson));
      } else {
        inputData.put(outputDir.resolve("default.json"), NullData.instance);
      }

      Path mappingPath = FileSystems.getDefault().getPath(cmd.getOptionValue("m"));
      ImportPath mappingImportPath =
          ImportPath.of(FileLoader.NAME, mappingPath, mappingPath.getParent());

      for (Path inputPath : inputData.keySet()) {
        Data input = inputData.get(inputPath);
        try (Engine engine =
            new Engine.Builder(ExternalConfigExtractor.of(mappingImportPath))
                .initialize()
                .build()) {
          Data output = engine.transform(input);
          writeJson(
              outputDir.resolve(
                  inputPath.getFileName().toString().replace(".json", ".output.json")),
              output);
        }
      }
    } catch (ParseException ex) {
      System.err.println(ex.getMessage());
      printHelp();
    }
  }