Folder structure aware template engine, that mirrors the source folder structure to target, while processing go template files.
You give me a structure source folder, that looks like a desired target, and I will:
- recreate the folder structure on target directory
- copy the files if it is not a template
- process template files to target
It is based on go template with addition of Sprig template functions (http://masterminds.github.io/sprig/).
Under release (https://github.com/lorands/tymlate/releases) pick the latest archive of apropritate flavor
untar and copy tymlate
to a folder that is on your PATH
(on Linux e.g. /usr/local/bin
).
Alternatively you can use go:
go get github.com/lorands/tymlate
Usage:
tymlate [flags]
Flags:
-c, --configuration string path to configuration file
-d, --datasource strings Datasource in name=file format
-h, --help help for tymlate
-s, --source string path to template source folder
-t, --target string path to target folder
In order to use tymlate
you need:
- folder containing source tree with files to be templated
- from
v1.1.0
even file or folder names can be templated
- from
- target folder where the files will be generated
- configuration YAML inside of source folder with name
.tymlate.yml
or provided with-c
flag - optionally data YAML files
Here is an example configuration file:
data: #direct data
meta:
package: "github.com/lorands/tymlate"
name: MyEzample
Dat1: # the name of the map
key1: "value1"
key2: 22
key3: true
Dat2:
key11: "value eleven"
key2: 23
key3: false
include:
cfgName1: subcfg1.yml
cfgNameTwo: subconf/subcfg2.yml
templates:
includes:
- ".*\\.tpl"
- ".*\\.go"
- LICENSE
excludes:
- ".*\\.inv"
suffix: ".tpl"
processFilename: true
Where:
data
(optional): contains the variables to be used in context, from the example you would access {{.meta.name}} from your templateinclude
(optional): include additional yaml data files, the key will be the key from the yaml file (e.g.cfgName1
)templates
(required)includes
- files to include (regexp)excludes
- files to ignore (regexp)suffix
(required): the template file suffixprocessFilename
(optional): true if filenames and folder names should be processed as go templates (by default false)
See: generator/testdata
Under the source
you will find a source we use to test,
and under target
you can see the desired output.
The configuration is provided in conf.yml
- Create a template folder structure.
- Create a configuration that defines how to process the structure.
- Define folder structure
- Define context data file(s)
- Run tymlate with configuration
Let's assume you would like to generate this structure each time you have a new project:
.
|___ cmd /
| |___ main.go
|___ model /
| |___ model.go
|___ README.md
|___ LICENSE
|___ Dockerfile
|___ go.mod
Just create a structure that will be mirrored to target.
Decide on what will be the template file extension,
we use .tpl
in this example (see: tymlate/generator/testdata/source
).
.
|___ cmd /
| |___ main.go.tpl
|___ model /
| |___ model.go.tpl
|___ README.md.tpl
|___ LICENSE
|___ Dockerfile.tpl
|___ go.mod.tpl
Each template file (.tpl in this case), is a go template file.
Each template file is "just" a go template.
We included Sprig (http://masterminds.github.io/sprig/) functions for your convenience.
The simplest case:
tymlate -s path/to/source -t path/to/target
In this case the configuration should be provided
in .tymlate.yml
file inside the source.
Simple case, with an external config:
tymlate -c path/to/config.yml -s path/to/source -t path/to/target
Additional data sources
tymlate -c path/to/config.yml -d ds=path/to/data.yml -s path/to/source -t path/to/target
More data sources
tymlate -c path/to/config.yml -d ds1=path/to/data1.yml -d ds2=path/to/data2.yml -s path/to/source -t path/to/target