Search code examples
yq

Use yq to query the full path in dotted notation


Motivation

I have a simple docker-compose.yaml file which is of this structure

services:
  foo:
    image: docker.registry.url:version
  foo2:
    image: docker.registry.url2:version
  foo3:
    image: docker.registry.url3:version

And I can easly do:

GET

yq '.services.foo.image' docker-compose.yaml
docker.registry.url:version

SET

yq -i '.services.foo.image = "foo"' docker-compose.yaml

Wish

I don't know how many services I'll have but I want to loop over all of them and fix the URL of the registry in case it comes from my registry and needs some updates.

Basically I would like to extract all keys in a way they can be used in a query again - similar to what is in the Motivation as an SET example.

yq <<magic command>> docker-compose.yaml
.services.foo.image .services.foo2.image .services.foo3.image

And using these keys I can then loop over it using:

for key in .asdf.asdf. .asdf.; do echo "Some query using $key"; done

What I tried

yq '.services.*.image | path' docker-compose.yaml
- services
- foo
- image
- services
- foo2
- image
- services
- foo3
- image

yq '.services.*.image | path | .[-2]' docker-compose.yaml
foo
foo2
foo3

Maybe with some query and merging this can print the path in a way it can later be used for a query again.


Solution

  • You were almost there with the path filter; you just have to convert it to a dotpath:

    yq eval '.services.*.image | path | "." + join(".")' docker-compose.yaml
    
    .services.foo.image
    .services.foo2.image
    .services.foo3.image