Yq

From NovaOrdis Knowledge Base
Revision as of 21:12, 15 January 2020 by Ovidiu (talk | contribs) (→‎Read)
Jump to navigation Jump to search

External

Internal

DEPLETE

YqTODELETE

Overview

yq is a lightweight and portable command-line YAML processor. It is written in Go and it consists in a single portable binary. It can be used to query a YAML file using a YAML path, to update a YAML file for a YAML path or using a script file, to create a YAML file or merge YAML files. The update operation creates any missing path elements. yq can also be used to convert YAML files to JSON and JSON files to YAML.

The default output is YAML, unless -j option is used.

Version

yq --version
yq version 2.4.1

Installation

Mac

brew install yq

Linux

yq_version=2.4.1
cd /tmp
wget https://github.com/mikefarah/yq/releases/download/${yq_version}/yq_linux_amd64
mv /tmp/yq_linux_amd64 yq /bin/yq
chmod a+x /bin/yq

Concepts

Command Line Options

-v

Provides additional details on parsing.

-j

Output formatted as JSON.

Operations

Read

https://mikefarah.github.io/yq/read/

Read

yq r <file.yaml> a.b.c
cat ... | yq r - ...

Read

yr r <file> ....

If the path does not exist - either the final element, or intermediary elements - , yq returns literally "null", but the return code is 0.

If the path exists, but the content is empty, yq returns "null".

Checking yq-validity

An empty file or a file that only contains commands is invalid for for yq. This can be checked as follows:

 if ! yq r file.yaml 1>/dev/null 2>/dev/null; then
   echo "file.yaml invalid"
 fi

Create a New YAML Document

https://mikefarah.github.io/yq/create/
yq n <path> <value>

The content is sent to stdout.

Example:

yq n test.color blue > test.yaml

Result:

test:
  color: blue