yq icon indicating copy to clipboard operation
yq copied to clipboard

Merge two files with anchors and getting reference errors

Open fabioluciano opened this issue 1 year ago • 1 comments

Describe the bug

I`m trying to merge two documents and i got a error in reference anchors

Version of yq: v4.34.1 Operating system: mac Installed via: homebrew

Input Yaml default.yml

services:
  kafka:
    username: &kafka_username username
    password: &kafka_password password
    servers: &kafka_servers
     - server
     - server
     - server
applications:
  app01:
    version: 1.0.0
    variables:
      kafka_servers: *kafka_servers
      kafka_usernane: *kafka_username
      kafka_password: *kafka_password

other.yml

applications:
  app02:
    version: 2.0.0
  app03:
    version: 1.0.0
    variables:
      kafka_servers: *kafka_servers
      kafka_usernane: *kafka_username
      kafka_password: *kafka_password

Command

yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' defaut.yml other.yml

Actual behavior

Error: bad file 'other.yml': yaml: unknown anchor 'kafka_servers' referenced

Expected behavior

services:
  kafka:
    username: &kafka_username username
    password: &kafka_password password
    servers: &kafka_servers
      - server
      - server
      - server
applications:
  app01:
    version: 1.0.0
    variables:
      kafka_servers: *kafka_servers
      kafka_usernane: *kafka_username
      kafka_password: *kafka_password
  app02:
    version: 2.0.0
    variables:
      kafka_servers: *kafka_servers
      kafka_usernane: *kafka_username
      kafka_password: *kafka_password
  app03:
    version: 1.0.0
    variables:
      kafka_servers: *kafka_servers
      kafka_usernane: *kafka_username
      kafka_password: *kafka_password

fabioluciano avatar Jun 14 '23 20:06 fabioluciano

The issue is that in order to merge the two files, it needs to be able to parse them both. And atm, other.yaml is not a valid file - it references anchors that don't exist.

What you can do is concatenate the two documents together into a single file, separated by the document separator ---

services:
  kafka:
    username: &kafka_username username
    password: &kafka_password password
    servers: &kafka_servers
     - server
     - server
     - server
applications:
  app01:
    version: 1.0.0
    variables:
      kafka_servers: *kafka_servers
      kafka_usernane: *kafka_username
      kafka_password: *kafka_password
---
applications:
  app02:
    version: 2.0.0
  app03:
    version: 1.0.0
    variables:
      kafka_servers: *kafka_servers
      kafka_usernane: *kafka_username
      kafka_password: *kafka_password

That is a valid yaml file. Now you can merge the documents together:

yq ea 'select(di==0) * select(di==1)' file.yaml

mikefarah avatar Jun 20 '23 00:06 mikefarah