Giter Club home page Giter Club logo

psyamlquery's Introduction

PS Yaml Query

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.

Note

Not all of the yq features are wrapped in PowerShell yet.

Install

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

Try it Out

Import-Yaml

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:80

Do the Import-Yaml, and print the services property.

$r = Import-Yaml docker-compose.yml

$r.services

Results

denomicroservice       : @{build=.\deno; ports=System.Object[]}
dotnetcoremicroservice : @{build=.\dotnetcore; ports=System.Object[]}
powershellmicroservice : @{build=.\powershell; ports=System.Object[]}
pythonmicroservice     : @{build=.\python; ports=System.Object[]}

Path Expression

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.dotnetcoremicroservice

Then

build        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

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 

Results

a: Easy! as one two three
b:
  c: 2
  d:
    - 3
    - 4

Compare-Yaml

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
    - cheetah

different-animals.yml

animals:
    - cats
    - bird
    - cheetah

Do the comparison.

Compare-Yaml animal.yml different-animals.yml

Result

  • cats and cheetah no change
  • dog not present in the second file
  • bird not present in the first file, and was added
 animals:
   - cats
-  - dog
+  - bird
   - cheetah

Merge-Yaml

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: 1
Merge-Yaml data1.yaml data2.yaml

Result

a: simple
b: [1, 2]
c:
  test: 1

Merge-Yaml : Append values with arrays

data1.yml

a: simple
b: [1, 2]
d: hi

data2.yml

a: something
b: [3, 4]
c:
  test: 2
  other: true
Merge-Yaml data1Append.yml data2Append.yml -Append

Results

a: simple
b: [1, 2, 3, 4]
d: hi
c:
  test: 2
  other: true

psyamlquery's People

Contributors

dfinke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.