00 Getting Started - go-easygen/easygen GitHub Wiki
Getting started with data-driven arbitrary text generation.
The driving data
YAML
The data are provided in .YAML format. E.g., example.yaml
:
# individual variables
Name: html2md
ProgramName: html2md
PackageName: main
# nested individual variables
Colormaps:
ColorRed: red
ColorBlue: blue
ColorWhite: white
# Simple string, to demo the string `split` function
Colorlist: red blue white
# Array variable in normal way
Colors:
- red
- blue
- white
# Multi-dimension array variable
Cars:
- Make: Ford
Module: T1
- Make: GM
Module: T2
Examples
list0.tmpl from test cases
The colors are: {{range .Colors}}{{.}}, {{end}}.
The result is in list0.ref:
The colors are: red, blue, white, .
Testing the templates on the fly:
$ easygen -ts 'The color maps are: {{range $key, $value := .Colormaps}}{{$key}}={{$value}}, {{end}}.' example
The color maps are: ColorBlue=blue, ColorRed=red, ColorWhite=white, .
Examples from other test cases
Also check the variation of such transforming:
- list1.tmpl
to use quotes in the driving data
check result: list1.ref - list1HTML.tmpl
to output above quotes in the driving data in html format
check result: list1HTML.ref - listfunc1.tmpl
to eliminate the last comma ",
" before the end
check result: listfunc1.ref - listfunc2.tmpl
to rename variables using different naming conventions via egVar builtin functions
check result: listfunc2.ref - list0E.tmpl
to use environment variables in template
check result: list0E.ref
I'll document more of them when I have time...
Builtin Functions
The above listfunc1.tmpl example showcases using some basic easygen builtin functions. What else are the easygen builtin functions?
The full list of easygen builtin functions is available here.
Real-life Example
To generate sgdisk.sh
like the following,
# format /dev/sdb as GPT, GUID Partition Table
sgdisk -Z /dev/sdb
sgdisk -n 0:0:+200M -t 0:ef02 -c 0:"bios_boot" /dev/sdb
sgdisk -n 0:0:+20G -t 0:8300 -c 0:"linux_boot" /dev/sdb
sgdisk -n 0:0:+30G -t 0:0700 -c 0:"windows" /dev/sdb
sgdisk -n 0:0:+10G -t 0:8200 -c 0:"linux_swap" /dev/sdb
sgdisk -n 0:0:+12G -t 0:8300 -c 0:"os1" /dev/sdb
sgdisk -n 0:0:+12G -t 0:8300 -c 0:"os2" /dev/sdb
sgdisk -n 0:0:+12G -t 0:8300 -c 0:"os3" /dev/sdb
sgdisk -n 0:0:0 -t 0:8300 -c 0:"data" /dev/sdb
sgdisk -p /dev/sdb
# inform the OS of partition table changes
partprobe /dev/sdb
fdisk -l /dev/sdb
check out its
sgdisk.yaml
driving data:Disk: /dev/sdb
Partitions:
- Name: bios_boot
Type: ef02
Size: +200M
- Name: linux_boot
Type: 8300
Size: +20G
- Name: windows
Type: "0700"
Size: +30G
- Name: linux_swap
Type: 8200
Size: +10G
- Name: os1
Type: 8300
Size: +12G
. . .
sgdisk.tmpl
template: # format {{.Disk}} as GPT, GUID Partition Table
sgdisk -Z {{.Disk}}
{{range .Partitions}}
sgdisk -n 0:0:{{.Size}} -t 0:{{.Type}} -c 0:"{{.Name}}" {{$.Disk}}{{end}}
sgdisk -p {{.Disk}}
# inform the OS of partition table changes
partprobe {{.Disk}}
fdisk -l {{.Disk}}
Continue Reading
Testing the templates on the fly
See Testing the templates on the fly
How to include nested templates from other files
See How to include nested templates from other files
Jump start the cli based projects
See