Search in sources :

Example 1 with Patch

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();
            }
        }
    }
}
Also used : GitFile(com.tremolosecurity.provisioning.tasks.dataobj.GitFile) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JsonPatch(com.github.fge.jsonpatch.JsonPatch) FileInputStream(java.io.FileInputStream) Yaml(org.yaml.snakeyaml.Yaml) JSONObject(org.json.simple.JSONObject) FileOutputStream(java.io.FileOutputStream) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) File(java.io.File) GitFile(com.tremolosecurity.provisioning.tasks.dataobj.GitFile) Map(java.util.Map) JsonMergePatch(com.github.fge.jsonpatch.mergepatch.JsonMergePatch) Patch(com.github.fge.jsonpatch.Patch) JsonPatch(com.github.fge.jsonpatch.JsonPatch) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JsonPatch (com.github.fge.jsonpatch.JsonPatch)1 Patch (com.github.fge.jsonpatch.Patch)1 JsonMergePatch (com.github.fge.jsonpatch.mergepatch.JsonMergePatch)1 GitFile (com.tremolosecurity.provisioning.tasks.dataobj.GitFile)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Map (java.util.Map)1 JSONObject (org.json.simple.JSONObject)1 JSONParser (org.json.simple.parser.JSONParser)1 Yaml (org.yaml.snakeyaml.Yaml)1