Search code examples
yamlyq

yq for merge strings


I have following prod.yaml file

configMap:
  data: 
    env:
      APP_1: "{{ .Data.data.app_1 }}"
      APP_2: "{{ .Data.data.app_2 }}"
      APP_3: "{{ .Data.data.app_3 }}"
      APP_4: "{{ .Data.data.app_4 }}"

and updated.yaml file:

APP_1:
APP_2:
APP_3:
APP_4:
APP_5:
LOG_DIR:

The expected result is:

configMap:
  data: 
    env:
      APP_1: "{{ .Data.data.app_1 }}"
      APP_2: "{{ .Data.data.app_2 }}"
      APP_3: "{{ .Data.data.app_3 }}"
      APP_4: "{{ .Data.data.app_4 }}"
      APP_5:
      LOG_DIR:

I am using awk to format the fields new data fields

cat .env | awk -F":" '{print $1": \"{{ .Data.data."tolower($1)" }}\""}' > updated.yaml

And awk to merge the new fields

yq '.env[] *=n [load("update.yaml")]' prod.yaml > a.yaml

But I cannot update the prod.yaml file...

I am reading this documentation yq doc


Solution

  • With mikefarah/yq that you are using, its pretty close to what you have. Remember that .env is a !!map type and not a !!seq, so do the recursive merge as the map type, i.e.

    yq '.configMap.data.env *=n load("update.yaml")' prod.yaml
    

    The above does not modify the file in-place. If you are satisfied with the contents of stdout, you could use the -i flag to do in-place substitution of the file