public void writeToFile()

in uReplicator-Controller/src/main/java/com/uber/stream/kafka/mirrormaker/controller/core/GitBackUpHandler.java [46:119]


  public void writeToFile(String fileName, String data) throws Exception {
    Repository backupRepo = null;
    BufferedWriter output = null;
    Git git = null;
    Git result = null;
    try {
      try {
        FileUtils.deleteDirectory(new File(localPath));
      } catch (IOException e) {
        LOGGER.error("Error deleting exisiting backup directory");
        throw e;
      }

      try {
        result = Git.cloneRepository().setURI(remotePath).setDirectory(new File(localPath)).call();
      } catch (Exception e) {
        LOGGER.error("Error cloning backup git repo");
        throw e;
      }

      try {
        backupRepo = new FileRepository(localPath + "/.git");
      } catch (IOException e) {
        throw e;
      }

      git = new Git(backupRepo);
      File myfile = new File(localPath + "/" + fileName);

      try {
        output = new BufferedWriter(new FileWriter(myfile));
        output.write(data);
        output.flush();
      } catch (IOException e) {
        LOGGER.error("Error writing backup to the file with name " + fileName);
        throw e;
      }

      try {
        git.add().addFilepattern(".").call();
      } catch (GitAPIException e) {
        LOGGER.error("Error adding files to git");
        throw e;
      }

      try {
        git.commit().setMessage("Taking backup on " + new Date()).call();

      } catch (GitAPIException e) {
        LOGGER.error("Error commiting files to git");
        throw e;
      }

      try {
        git.push().call();
      } catch (GitAPIException e) {
        LOGGER.error("Error pushing files to git");
        throw e;
      }
    } finally {
      if (output != null) {
        output.close();
      }
      if (git != null) {
        git.close();
      }
      if (result != null) {
        result.getRepository().close();
      }
      if (backupRepo != null) {
        backupRepo.close();
      }
    }
  }