use of com.github.fge.jsonpatch.Patch in project OpenUnison by TremoloSecurity.
the class GitUtils method applyFiles.
public void applyFiles(List<GitFile> files) throws IOException, NoFilepatternException, GitAPIException, JsonPatchException, ParseException {
for (GitFile file : files) {
File targetFile = new File(this.tmpdir.getAbsolutePath() + File.separator + "gitrepo" + file.getDirName() + File.separator + file.getFileName());
if (file.isDelete()) {
logger.info("Deleting '" + targetFile.getAbsolutePath() + "'");
FileUtils.forceDelete(targetFile);
git.rm().addFilepattern(file.getDirName().substring(1) + File.separator + file.getFileName()).call();
}
if (file.isPatch()) {
logger.info("Patching '" + targetFile.getAbsolutePath() + "'");
InputStream in = new FileInputStream(targetFile);
Yaml yaml = new Yaml();
Map<String, Object> map = (Map<String, Object>) yaml.load(in);
JSONObject jsonObject = new JSONObject(map);
ObjectMapper mapper = new ObjectMapper();
JsonNode toBePatched = mapper.readValue(jsonObject.toJSONString(), JsonNode.class);
Patch patch = null;
if (file.getPatchType().equalsIgnoreCase("json")) {
patch = mapper.readValue(file.getData(), JsonPatch.class);
} else if (file.getPatchType().equalsIgnoreCase("merge")) {
patch = mapper.readValue(file.getData(), JsonMergePatch.class);
} else {
throw new IOException("Unsupported merge strategy " + file.getPatchType());
}
JsonNode patched = patch.apply(toBePatched);
String patchedJson = patched.toString();
String newYaml = yaml.dump(new JSONParser().parse(patchedJson));
FileOutputStream out = new FileOutputStream(targetFile);
out.write(newYaml.getBytes("UTF-8"));
out.flush();
out.close();
} else {
logger.info("Creating '" + targetFile.getAbsolutePath() + "'");
Files.createParentDirs(targetFile);
FileOutputStream out = new FileOutputStream(targetFile);
out.write(file.getData().getBytes("UTF-8"));
out.flush();
out.close();
}
}
for (GitFile file : files) {
File targetFile = new File(this.tmpdir.getAbsolutePath() + File.separator + "gitrepo" + file.getDirName() + File.separator + file.getFileName());
if (file.isDelete()) {
if (file.isNamespace()) {
logger.info("Deleting namespace, removing directory '" + file.getDirName() + "'");
// git.rm().addFilepattern("." + file.getDirName()).call();
FileUtils.forceDelete(new File(this.tmpdir.getAbsolutePath() + File.separator + "gitrepo" + file.getDirName()));
git.rm().addFilepattern(file.getDirName().substring(1)).call();
}
}
}
}
Aggregations