Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge two files with anchors and getting reference errors #1694

Open
fabioluciano opened this issue Jun 14, 2023 · 1 comment
Open

Merge two files with anchors and getting reference errors #1694

fabioluciano opened this issue Jun 14, 2023 · 1 comment
Labels

Comments

@fabioluciano
Copy link

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
@mikefarah
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants