FY-TOOL(1) | libfyaml | FY-TOOL(1) |
fy-tool - fy-tool documentation
fy-tool [OPTIONS] [<file> ...]
fy-dump [OPTIONS] [<file> ...]
fy-testsuite [OPTIONS] <file>
fy-filter [OPTIONS] [-f*FILE*] [<path> ...]
fy-join [OPTIONS] [-T*PATH*] [-F*PATH*] [-t*PATH*] [<file> ...]
fy-ypath [OPTIONS] <ypath-expression> [<*file> ...]
fy-compose [OPTIONS] [<file> ...]
fy-tool is a general YAML/JSON manipulation tool using libfyaml. Its operation is different depending on how it's called, either via aliases named fy-dump, fy-testsuite, fy-filter, fy-join, or via the --dump, --testsuite, --filter, --join, --ypath and --compose options.
A number of options are common for all the different modes of operation and they are as follows: Common options.INDENT 0.0
Internal library debugging messages. No output is produced when the library was compiled with --disable-debug
Informational messages about the internal operation of the library.
Messsages that could require attention.
A warning message, something is wrong, but operation can continue. This is the default value.
A fatal error occured, it is impossible to continue.
The default level is 3 (WARNING), which means that messages with level 3 and higher will be displayed.
The input files are always in YAML mode.
The input files are always set to JSON mode.
The input files are set to JSON mode automatically when the file's extension is .json. This is the default.
JSON support is complete so all valid JSON files are parsed
according to JSON rules, even where those differ with YAML rules.
Note that in streaming mode:
The original formatting used in the input. This is default mode.
The output is forced to be in block mode. All flow constructs will be converted to block mode.
The output is forced to be in flow mode. All block constructs will be converted to flow mode.
The output is forced to be in flow mode, but no newlines will be emitted; the output is going to be a (potentially very) long line.
The output is forced to be in JSON mode. Note that it is impossible to output an arbitrary YAML file as JSON, so this may fail.
The output is forced to be in JSON mode and in a single line.
Output is in block YAML mode but with special care to convert JSON quoted strings in as non-idiomatic YAML as possible. For example { foo: "this is a test" } will be emitted as foo: this is a test. YAML can handle scalars without using excessive quoting.
Never colorize output.
Always colorize output.
Automatically colorize output when the output is a terminal. This is the default.
In this mode, all files provided in the command line will be dumped in one continuous stream, to the standard output, using document start indicators to mark the start of end new file.
If the file provided is - then the input is the standard input.
In this mode, a single YAML file is read and an event stream is generated which is the format used for yaml-testsuite compliance.
If the file provided is - then the input is the standard input.
In this mode, a single YAML file is read from the standard input for each path that is provided in the command line a document will be produced to the standard output. To use file instead of standard input use the -f/--file option.
If the file provided is - then the input is the standard input.
If first character of FILE is > the the input is the content of the option that follows. For example --file ">foo: bar" is as --file file.yaml with file.yaml "foo: bar"
In this mode, multiple YAML files are joined into a single document, emitted to the standard output.
If the file provided is - then the input is the standard input.
If first character of FILE is > the the input is the content of the option that follows.
If first character of FILE is > the the input is the content of the option that follows.
If first character of FILE is > the the input is the content of the option that follows.
Example input files
We're going to be using a couple of YAML files in our examples.
invoice.yaml
# invoice.yaml invoice: 34843 date : !!str 2001-01-23 bill-to: &id001 given : Chris family : Dumars address:
lines: |
458 Walkman Dr.
Suite #292
simple-anchors.yaml
# simple-anchors.yaml foo: &label { bar: frooz } baz: *label
mergekeyspec.yaml
--- - &CENTER { x: 1, y: 2 } - &LEFT { x: 0, y: 2 } - &BIG { r: 10 } - &SMALL { r: 1 } # All the following maps are equal: - # Explicit keys
x: 1
y: 2
r: 10
label: center/big - # Merge one map
<< : *CENTER
r: 10
label: center/big - # Merge multiple maps
<< : [ *CENTER, *BIG ]
label: center/big - # Override
<< : [ *BIG, *LEFT, *SMALL ]
x: 1
label: center/big
bomb.yaml
a: &a ["lol","lol","lol","lol","lol","lol","lol","lol","lol"] b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a] c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b] d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c] e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d] f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e] g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f]
Parse and dump generated YAML document tree in the original YAML form
$ fy-dump invoice.yaml
invoice: 34843 date: !!str 2001-01-23 bill-to: &id001 given: Chris family: Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
Parse and dump generated YAML document tree in flow YAML form
$ fy-dump -mflow invoice.yaml
{
invoice: 34843,
date: !!str 2001-01-23,
bill-to: &id001 {
given: Chris,
family: Dumars,
address: {
lines: "458 Walkman Dr.\nSuite #292\n"
}
} }
Parse and dump generated YAML document from the input string
$ fy-dump -mjson ">foo: bar"
{
"foo": "bar" }
Using the resolve option on the simple-anchors.yaml
$ fy-dump -r simple-anchor.yaml
foo: &label {
bar: frooz
} baz: {
bar: frooz
}
Stripping the labels too:
$ fy-dump -r --strip-label simple-anchor.yaml
foo: {
bar: frooz
} baz: {
bar: frooz
}
Merge key support:
$ fy-dump -r --strip-label mergekeyspec.yaml
--- - {
x: 1,
y: 2
} - {
x: 0,
y: 2
} - {
r: 10
} - {
r: 1
} - x: 1
y: 2
r: 10
label: center/big - y: 2
x: 1
r: 10
label: center/big - r: 10
y: 2
x: 1
label: center/big - y: 2
r: 10
x: 1
label: center/big
Sorting option:
$ fy-dump -s invoice.yaml
bill-to: &id001
address:
lines: |
458 Walkman Dr.
Suite #292
family: Dumars
given: Chris date: !!str 2001-01-23 invoice: 34843
An example using the testsuite mode generates the following event stream from invoice.yaml
Parse and dump test-suite event format
$ fy-testsuite invoice.yaml
Filter out from the /bill-to path of invoice.yaml
$ cat invoice.yaml | fy-filter /bill-to
&id001 given: Chris family: Dumars address:
lines: |
458 Walkman Dr.
Suite #292
Filter example with arrays (and use the --file option)
$ fy-filter --file=mergekeyspec.yaml /5
--- <<: *CENTER r: 10 label: center/big
Follow anchors example
$ fy-filter --file=simple-anchors.yaml /baz/bar
frooz
Handle YAML bombs (if you can spare the memory and cpu time)
$ fy-filter --file=bomb.yaml -r / | wc -l 6726047
You don't have to, you can just follow links to retrieve data.
$ fy-filter --file=stuff/bomb.yaml -l --strip-label /g/0/1/2/3/4/5/6
"lol"
Following links works with merge keys too:
$ fy-filter --file=mergekeyspec.yaml -l --strip-label /5/x
--- 1
Joining two YAML files that have root mappings.
$ fy-join simple-anchors.yaml invoice.yaml
foo: &label {
bar: frooz
} baz: *label invoice: 34843 date: !!str 2001-01-23 bill-to: &id001
given: Chris
family: Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
Join two files with sequences at root:
$ fy-join -mblock ">[ foo, bar ]" ">[ baz ]"
- foo - bar - baz
Pantelis Antoniou <pantelis.antoniou@konsulko.com>
2019, Pantelis Antoniou
January 14, 2022 |