简体   繁体   中英

How can I merge a map into values.yaml wit yq

I am trying to create a multiple map in a values.yaml as follows.

FILES=$(ls -1 | grep "values-.*.yaml") 
for i in $FILES; do
   yq e '.applications *= {
            "'$i'":{
               "enabled":"true",
               "destination":{
                  "namespace":"x",
                  "server":"wwww"
               },
               "source":{
                  "chart":"'$i'"
               }
            }
         }' values.yaml
done

and this is the next output:

Error: Cannot multiply !!null with !!map

The yaml should look like this:

applications:
  a:
    enabled: "true"
    destination:
      namespace: x
      server: wwww
    source:
      chart: a
  b:
    enabled: "true"
    destination:
      namespace: x
      server: wwww
    source:
      chart: b

What am I doing wrong?

With your code you get the error when the file values.yaml is empty at the beginning

applications:

Instead use this and the error Cannot multiply !!null with !!map disappears:

applications: {}

Apart from that, your code has some problems. I would like to suggest passing the filename to yq as a parameter and accessing it within the code with strenv

FILES=$(ls -1 | grep "values-.*.yaml")
for i in $FILES; do
  file="$i" yq '.applications *=
    {strenv(file): {
       "enabled":"true",
       "destination": {
          "namespace":"x",
          "server":"www"
       },
       "source": {
          "chart": strenv(file)
       }
    }
  }' values.yaml
done

This code breaks, if a file name contains whitespaces, eg values-3. gap.yaml values-3. gap.yaml . To address this issue think about using find... exec instead of a for loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM