How to copy a block of content and paste in yml

Viewed 10

I have a some k8 specific config files each has some complex structure and some formatting issues. But those files are used in app without any issues. Now I need to copy content of the document and paste it in same file. I am looking for any library which can support this.

Ex

deployment.yml
dev:
  apiVersion: v1
  kind: Account
  metadata:
    name: ::api::
it:
  apiVersion: v1
  kind: Account
  metadata:
    name: ::api::

From above file I have to create a new env like test and copy it content in test.

deployment.yml
dev:
  apiVersion: v1
  kind: Account
  metadata:
    name: ::api::
it:
  apiVersion: v1
  kind: Account
  metadata:
    name: ::api::
test:
  apiVersion: v1
  kind: Account
  metadata:
    name: ::api::

Expecting above output. When I tried with SnakeYml with Java its complaining with parsing error at name: ::api:: due to : To make it work(parse) I have to change this like name: "::api::" manually.

InputStream inputStream = new FileInputStream(FILE_PATH);
            
            Yaml yaml = new Yaml();
            
            Map<String,Object> ymlData = new LinkedHashMap<>();
            ymlData = yaml.load(inputStream);
            
            if(ymlData.containsKey("it")) {
                Map<String,Object> updatedYmlData = new LinkedHashMap<>();
                
                ymlData.entrySet().forEach( keySet -> {
                    updatedYmlData.put(keySet.getKey(), keySet.getValue());
                });
                updatedYmlData.put("test", ymlData.get("it"));
                
                PrintWriter ymlWriter = new PrintWriter(FILE_PATH);
                
                yaml.dump(updatedYmlData,ymlWriter);

Is there any other way we can achieve this? any library which copy paste the content as is?

0 Answers
Related