yq grab all elements that doesn't contains word powershell

Viewed 131

I would like to retriev list of all images that DO NOT contains cmp in repository section in format repository:tag. I'm trying to use yq but I'm facing few problems.

My yaml file structure:

actions:
    install: ...
    uninstall: ...
images:
    foo:
        imageType:"docker"
        repository: localhost:3000/cmp/foo
        tag: latest
    bar:
        imageType:"docker"
        repository: localhost:3000/bar
        tag: latest
    fizz:
        imageType:"docker"
        repository: localhost:3000/fizz
        tag: latest

My Powershell script:

$keys = yq eval 'del(.images | .. | select(.repository == """*cmp*""")) | .images | keys' $porterPath
foreach($key in $keys) {
    $key = $key.Replace("- ", "") #All elements of $keys need to be strip from "- "
    $repository = yq eval ".images | .$key | .repository"
    $tag = yq eval ".images | .$key | .tag"
    $image = $repository":"$tag
    writeToFile($image)
}

First yq expression is adding - repository to the end of the output list in $keys so it looks like this:

 - bar
 - fizz
 - repository

Why I'm getting - repository in the end of $keys ?

Is there a better way to transform yaml with yq to get output list contains : repository:tag ?

1 Answers
yq '.images[] | select(.repository | contains("cmp") | not) | (.repository + ":" + .tag)' file.yaml

Explanation:

  • traverse into the nodes under 'images' .images[]
  • filter out all the nodes that contain "cmp" in the repository select(.repository | contains("cmp") | not)
  • create a string concatenation of " repository:tag"

Disclaimer: I wrote yq

Related