https://github.com/lbovet/yglu
Yglu ᕄ !? - YAML glue for structural templating and processing
https://github.com/lbovet/yglu
devops functional igloo kubernetes python structural templating yaml yaql
Last synced: 22 days ago
JSON representation
Yglu ᕄ !? - YAML glue for structural templating and processing
- Host: GitHub
- URL: https://github.com/lbovet/yglu
- Owner: lbovet
- License: mit
- Created: 2020-01-06T21:34:32.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2025-03-09T18:40:46.000Z (about 2 months ago)
- Last Synced: 2025-04-03T16:05:51.802Z (26 days ago)
- Topics: devops, functional, igloo, kubernetes, python, structural, templating, yaml, yaql
- Language: Python
- Homepage: https://yglu.io
- Size: 340 KB
- Stars: 93
- Watchers: 3
- Forks: 6
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
---
title: yglu ReadMe
---# Yglu ᕄ !?
[](https://travis-ci.org/lbovet/yglu)
[](https://badge.fury.io/py/yglu)Yglu is [YAML](https://yaml.org/) augmented with an advanced expression language. Unlike usual text templating, Yglu relies on the YAML structure and leverages its tag system combined with the [YAQL](https://yaql.readthedocs.io/en/latest/) query language.
This association enables templating and functional processing a bit like if YAML nodes were spreadsheet cells.
Yglu input documents are pure and valid YAML using [tags](https://yaml.org/spec/1.2/spec.html#id2784064) for computed nodes.
input
a: 1
b: !? .a + 1
!if .b = 2:
c: 3output
a: 1
b: 2
c: 3
input
names: !-
- 'nginx:1.16'
- 'node:13.6'
- 'couchbase:9.3'
image: !()
!? $.split(':')[0]:
version: !? $.split(':')[1]
images: !?
$_.names
.select(($_.image)($))
.aggregate($1.mergeWith($2), {})output
images:
nginx:
version: '1.16'
node:
version: '13.6'
couchbase:
version: '9.3'
In the example above, the `names` sequence is hidden, `image` is a function (like a template block) and `images` is an expression which iterates through all names, applies the image function to each one and aggregates the individual results by merging them together as a mapping.
As such an operation is often needed, Yglu provides a `!for` tag for merging a sequence iterated over a function:
input
names: !-
- 'nginx:1.16'
- 'node:13.6'
- 'couchbase:9.3'
images:
!for .names: !()
!? $.split(':')[0]:
version: !? $.split(':')[1]output
images:
nginx:
version: '1.16'
node:
version: '13.6'
couchbase:
version: '9.3'
See the test [samples](https://github.com/lbovet/yglu/tree/master/tests/samples) for more examples.
## Install
```
pip3 install yglu
```## Run
```
Usage: yglu [options] []Options:
-v - -version Print version and exit.
-h - -help Print help and exit.
```## Tags
Tags specify an alteration of the document structure.
| **Tag** | **Description** |
|-----------|-----------------|
| `!?` | Evaluate an expression. The result can be a scalar, mapping or sequence. Can also be used in mapping keys. |
| `!-` | Hide the node in the output but keep it accessible from expressions. Can be an expression. |
| `!()` | Make the node reusable in expressions as a function. |
| `!if` | Conditional merge. See [if.yml](https://github.com/lbovet/yglu/tree/master/tests/samples/if.yml). |
| `!for` | Merge the results of a function applied to all items of a sequence . See [for.yml](https://github.com/lbovet/yglu/tree/master/tests/samples/for.yml). |
| `!apply` | Apply a function or function block to a block. See [function.yml](https://github.com/lbovet/yglu/tree/master/tests/samples/function.yml). |## Expressions
Expressions are written in [YAQL](https://yaql.readthedocs.io/en/latest/).
They are evaluated in a context with the following variables defined:
| **Variable**| **Description** |
|-------------|-----------------|
| `$_` | Refers to the current document root. Can be omitted at the beginning of the expression if it starts with a dot. |
| `$` | Implicit argument of functions. |
| `$env` | Gives access to environment variables. Disabled by default. Set the `$YGLU_ENABLE_ENV` environment variable to enable this feature. |
| `$this` | Refers to the current function block node in order to access its children nodes. See [function.yml](https://github.com/lbovet/yglu/tree/master/tests/samples/function.yml) |## Built-in Functions
In addition to the [standard YAQL library](https://yaql.readthedocs.io/en/latest/standard_library.html#), Yglu defines the following functions:
| **Function** | **Description** |
|----------------------|-----------------|
| `$import(filename)` | Imports another document in the current node. By default, it is only permitted to import files from within the directory hierarchy of the input file. Set `$YGLU_IMPORT_ALLOW` to a list of permitted directories. |## Vaguely related projects
* [YAMLScript](https://yamlscript.org)