PowerShell wrapper for yq, enables a better PowerShell pipeline experience.
yq - a lightweight and portable command-line YAML processor, whose aim is to be the jq or sed of yaml files.
- GitHub - https://github.com/mikefarah/yq
- GitBook - https://mikefarah.gitbook.io/yq/
Not all of the
yqfeatures are wrapped in PowerShell yet.
yq needs to be installed. You can install it using: choco install yq.
Or, check here for other install options: https://mikefarah.gitbook.io/yq/#install
docker-compose.yml
version: '3'
services:
powershellmicroservice:
build: .\powershell
ports:
- 8080:8080
pythonmicroservice:
build: .\python
ports:
- 8081:8081
denomicroservice:
build: .\deno
ports:
- 8082:8082
dotnetcoremicroservice:
build: .\dotnetcore
ports:
- 3000:80Do the Import-Yaml, and print the services property.
$r = Import-Yaml docker-compose.yml
$r.servicesdenomicroservice : @{build=.\deno; ports=System.Object[]}
dotnetcoremicroservice : @{build=.\dotnetcore; ports=System.Object[]}
powershellmicroservice : @{build=.\powershell; ports=System.Object[]}
pythonmicroservice : @{build=.\python; ports=System.Object[]}
yq also supports a path expression and returns the matching nodes the given yaml file.
Using the docker-compose.yml example above, and the path expression services.dotnetcoremicroservice.
Import-Yaml docker-compose.yml services.dotnetcoremicroservicebuild ports
----- -----
.\dotnetcore {3000:80}
This is equivalent to to (Import-Yaml .\sample-docker-compose.yml).services.dotnetcoremicroservice in PowerShell, but the PoweShell approach is more typing.
ConvertTo-Yaml '{"a":"Easy! as one two three","b":{"c":2,"d":[3,4]}}' - Or
'{"a":"Easy! as one two three","b":{"c":2,"d":[3,4]}}' | ConvertTo-Yaml a: Easy! as one two three
b:
c: 2
d:
- 3
- 4
Deeply compare two yaml documents.
Compares the matching yaml nodes at path expression in the two yaml documents. See path expression for more details. Difference calculated line by line, and is printed out line by line where the first character of each line is either:
- a
space, indicating no change at this line -a minus ,indicating the line is not present in the second document (it's removed)+a plus, indicating that the line is not present in the first document (it's added) If there are differences then yq will print out the differences and exit with code 1. If there are no differences, then nothing will be printed and the exit code will be 0.
animal.yml
animals:
- cats
- dog
- cheetahdifferent-animals.yml
animals:
- cats
- bird
- cheetahDo the comparison.
Compare-Yaml animal.yml different-animals.ymlcatsandcheetahno changedognot present in the second filebirdnot present in the first file, and was added
animals:
- cats
- - dog
+ - bird
- cheetah
Merge multiple yaml files into a one.
Yaml files can be merged using the 'merge' command. Each additional file merged with the first file will set values for any key not existing already or where the key has no value.
data1.yml
a: simple
b: [1, 2]data2.yml
a: other
c:
test: 1Merge-Yaml data1.yaml data2.yamla: simple
b: [1, 2]
c:
test: 1
data1.yml
a: simple
b: [1, 2]
d: hidata2.yml
a: something
b: [3, 4]
c:
test: 2
other: trueMerge-Yaml data1Append.yml data2Append.yml -Appenda: simple
b: [1, 2, 3, 4]
d: hi
c:
test: 2
other: true