2021-11-16 04:29:16 +00:00
|
|
|
# Load
|
|
|
|
|
2022-02-15 03:51:22 +00:00
|
|
|
The load operators allows you to load in content from another file.
|
2021-11-16 04:29:16 +00:00
|
|
|
|
|
|
|
Note that you can use string operators like `+` and `sub` to modify the value in the yaml file to a path that exists in your system.
|
|
|
|
|
2022-02-15 03:51:22 +00:00
|
|
|
You can load files of the following supported types:
|
|
|
|
|
|
|
|
|Format | Load Operator |
|
|
|
|
| --- | --- |
|
|
|
|
| Yaml | load |
|
|
|
|
| XML | load_xml |
|
|
|
|
| Properties | load_props |
|
|
|
|
| Plain String | load_str |
|
2021-11-16 04:29:16 +00:00
|
|
|
|
2022-02-22 22:36:03 +00:00
|
|
|
## Samples files for tests:
|
|
|
|
|
|
|
|
### yaml
|
|
|
|
|
|
|
|
`../../examples/thing.yml`:
|
2021-11-16 04:29:16 +00:00
|
|
|
|
|
|
|
```yaml
|
|
|
|
a: apple is included
|
|
|
|
b: cool
|
|
|
|
```
|
2022-02-22 22:36:03 +00:00
|
|
|
|
|
|
|
### xml
|
|
|
|
`small.xml`:
|
2022-02-15 03:51:22 +00:00
|
|
|
|
|
|
|
```xml
|
|
|
|
<this>is some xml</this>
|
|
|
|
```
|
|
|
|
|
2022-02-22 22:36:03 +00:00
|
|
|
### properties
|
|
|
|
`small.properties`:
|
2022-02-15 03:51:22 +00:00
|
|
|
|
|
|
|
```properties
|
|
|
|
this.is = a properties file
|
|
|
|
```
|
2021-11-16 04:29:16 +00:00
|
|
|
|
2022-02-22 22:36:03 +00:00
|
|
|
### base64
|
|
|
|
`base64.txt`:
|
|
|
|
```
|
|
|
|
bXkgc2VjcmV0IGNoaWxsaSByZWNpcGUgaXMuLi4u
|
|
|
|
```
|
|
|
|
|
2022-02-06 03:39:46 +00:00
|
|
|
{% hint style="warning" %}
|
|
|
|
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
|
|
|
|
|
|
|
|
`yq e <exp> <file>`
|
|
|
|
{% endhint %}
|
|
|
|
|
2021-11-16 04:29:16 +00:00
|
|
|
## Simple example
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
myFile: ../../examples/thing.yml
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq 'load(.myFile)' sample.yml
|
2021-11-16 04:29:16 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: apple is included
|
|
|
|
b: cool.
|
|
|
|
```
|
|
|
|
|
|
|
|
## Replace node with referenced file
|
|
|
|
Note that you can modify the filename in the load operator if needed.
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
something:
|
|
|
|
file: thing.yml
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.something |= load("../../examples/" + .file)' sample.yml
|
2021-11-16 04:29:16 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
something:
|
|
|
|
a: apple is included
|
|
|
|
b: cool.
|
|
|
|
```
|
|
|
|
|
|
|
|
## Replace _all_ nodes with referenced file
|
|
|
|
Recursively match all the nodes (`..`) and then filter the ones that have a 'file' attribute.
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
something:
|
|
|
|
file: thing.yml
|
|
|
|
over:
|
|
|
|
here:
|
|
|
|
- file: thing.yml
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '(.. | select(has("file"))) |= load("../../examples/" + .file)' sample.yml
|
2021-11-16 04:29:16 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
something:
|
|
|
|
a: apple is included
|
|
|
|
b: cool.
|
|
|
|
over:
|
|
|
|
here:
|
|
|
|
- a: apple is included
|
|
|
|
b: cool.
|
|
|
|
```
|
|
|
|
|
|
|
|
## Replace node with referenced file as string
|
|
|
|
This will work for any text based file
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
something:
|
|
|
|
file: thing.yml
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-02-15 03:51:22 +00:00
|
|
|
yq '.something |= load_str("../../examples/" + .file)' sample.yml
|
2021-11-16 04:29:16 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
something: |-
|
|
|
|
a: apple is included
|
|
|
|
b: cool.
|
|
|
|
```
|
|
|
|
|
2022-02-15 03:51:22 +00:00
|
|
|
## Load from XML
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
cool: things
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq '.more_stuff = load_xml("../../examples/small.xml")' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
cool: things
|
|
|
|
more_stuff:
|
|
|
|
this: is some xml
|
|
|
|
```
|
|
|
|
|
|
|
|
## Load from Properties
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
cool: things
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq '.more_stuff = load_props("../../examples/small.properties")' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
cool: things
|
|
|
|
more_stuff:
|
|
|
|
this:
|
|
|
|
is: a properties file
|
|
|
|
```
|
|
|
|
|
|
|
|
## Merge from properties
|
|
|
|
This can be used as a convenient way to update a yaml document
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
this:
|
|
|
|
is: from yaml
|
|
|
|
cool: ay
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq '. *= load_props("../../examples/small.properties")' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
this:
|
|
|
|
is: a properties file
|
|
|
|
cool: ay
|
|
|
|
```
|
|
|
|
|
2022-02-22 22:36:03 +00:00
|
|
|
## Load from base64 encoded file
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
cool: things
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq '.more_stuff = load_base64("../../examples/base64.txt")' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
cool: things
|
|
|
|
more_stuff: my secret chilli recipe is....
|
|
|
|
```
|
|
|
|
|