Skip to content

olastor/ymlt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ymlt

ymlt is a command-line tool for processing YAML documents, allowing the use of Go templates in every string field, with added functions t and tt to lookup values from within the same document.

Usage

ymlt [options] [file]

Options

  • -d, --defaults string Set default values from a YAML file
  • -v Display version
  • -h, --help Display help

Installation

You can build ymlt from source:

make build

Examples

Basic Template Usage

ymlt allows you to use Go templates in YAML string fields with special functions t() and tt() to reference other values in the same document:

Input file (data.yaml):

name: John
age: 32
ageCopy: "{{ t \".age\" }}"
nameCopy: '{{ t "$.name" }}'
---
name: Peter
age: 98
nameAndAge: |
  {{ t ".name" }} ({{ t "$.age" }} years old)

Command:

ymlt data.yaml

Output:

name: John
age: 32
ageCopy: "32"
nameCopy: 'John'
---
name: Peter
age: 98
nameAndAge: |
  Peter (98 years old)

Using Defaults

You can provide default values that will be merged into your YAML document. The defaults can also use template variables to reference fields from the main document:

Input file (data.yaml):

name: John
age: 32
---
name: Jelena
age: 59
address: Test Street 4

Defaults file (defaults.yaml):

address: unknown
description: |
  {{ t ".name" }} is {{ t ".age" }} years old

Command:

ymlt -d defaults.yaml data.yaml

Output:

name: John
age: 32
address: unknown
description: |
  John is 32 years old
---
name: Jelena
age: 59
address: Test Street 4
description: |
  Jelena is 59 years old

Complex Template Example

Here's a more complex example showing how to build formatted strings using multiple field references:

Input file (addresses.yaml):

street: "Main Street"
houseNumber: "42"
postalCode: "90210"
country: "USA"
---
street: "Elm Street"
houseNumber: "13A"
postalCode: "10001"
country: "USA"

Defaults file (address-defaults.yaml):

address: |
  {{ t ".street" }} {{ t ".houseNumber"}},
  {{ t ".postalCode" }} ({{ t ".country" }})

Command:

ymlt -d address-defaults.yaml addresses.yaml

Output:

street: "Main Street"
houseNumber: "42"
postalCode: "90210"
country: "USA"
address: |
  Main Street 42,
  90210 (USA)
---
street: "Elm Street"
houseNumber: "13A"
postalCode: "10001"
country: "USA"
address: |
  Elm Street 13A,
  10001 (USA)

Reading from stdin

You can also process YAML from stdin:

cat input.yaml | ymlt -d defaults.yaml

About

CLI tool to reduce duplication in YAML docs.

Topics

Resources

License

Stars

1 star

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors