Filer an array of objects with yq
Asked Answered
F

1

6

I have a yaml file that looks like this:

apiVersion: v1
entries:
  blue-green-toggle:
  - description: Used to toggle an application between blue and green
    name: blue-green-toggle
    version: 1.0.17
    apiVersion: v2
  - description: Used to toggle an application between blue and green
    name: blue-green-toggle
    version: 1.0.16
    apiVersion: v2
  - description: Used to toggle an application between blue and green
    name: blue-green-toggle
    version: 1.0.15
    apiVersion: v2
  istio-config:
  - description: Used to configure the cluster level settings of istio.
    name: istio-config
    version: 1.0.4
    apiVersion: v2
  - description: Used to configure the cluster level settings of istio.
    name: istio-config
    version: 1.0.1
    apiVersion: v2
  latest-toggle:
    name: latest-toggle
    version: 1.0.5
    apiVersion: v2
  standard-helm-chart:
    name: standard-helm-chart
    version: 1.1.10
    apiVersion: v2
    name: standard-helm-chart
    version: 2.0.1
    apiVersion: v2
    name: standard-helm-chart
    version: 1.0.34
    apiVersion: v2
    name: standard-helm-chart
    version: 1.0.10
    apiVersion: v2
    name: standard-helm-chart
    version: 1.0.9
    apiVersion: v2
generated: 2021-06-22T00:22:33.1554922Z
...

I am trying to list the the version numbers that start with 1.0. and are listed in the standard-helm-chart section.

I have used this so far to get just the entries for the standard-helm-chart:

yq eval '.entries | .standard-arup-helm-chart' index.yaml

That worked fine. So then I then tried to get just to the rows with version matching 1.0.*. I read over the select documentation for yq, but it does not indicate how to match when you are looking at an object instead of just a string.

I tried this:

yq eval '.entries | .standard-arup-helm-chart | select(. == "1.0.*")' index.yaml

But this fails. And I would expect it to, because it can't compare the string of "1.0.*" to the whole object.

I also tried:

yq eval '.entries | .standard-arup-helm-chart | select(.version == "1.0.*")' index.yaml

Thinking that would let yq know that I want to just look at the version. But it said Error: Cannot index array with 'version'

I then figured I needed to try an array style syntax:

yq eval '.entries | .standard-arup-helm-chart | select(.[version] == "1.0.*")' index.yaml

But that fails with a parsing error.

What command can I send yq to get all the version numbers that start with 1.0.?

Forb answered 22/6, 2021 at 1:17 Comment(0)
F
6

Took some trial an error, but this is what I ended up with:

yq eval '.entries.standard-helm-chart.[] | select(.version == "1.0.*") | .version' index.yaml
Forb answered 22/6, 2021 at 4:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.