Command easygen
is an easy to use universal code/text generator.
It can be used as a text or html generator for arbitrary purposes with arbitrary data and templates. It is a good GSL replacement, as it
- is more easy to define driving data, in form of YML instead of XML
- has more powerful template engine that based on Go template. You can also write your own function in Go to customize your template.
You can even use easygen as a generic Go template testing tool using the -ts
commandline option, and much more.
Note this document is for easygen
versions 4.0+. For historic versions check out the Different Versions section.
easygen version 4.0.0
Usage:
easygen [flags] template_name [data_filename [data_filename...]]
Flags:
-debug level
debugging level
-ej extension
extension of json file (default ".json")
-et extension
extension of template file (default ".tmpl")
-ey extension
extension of yaml file (default ".yaml")
-ts string
template string (in text)
data_filename(s): The name for the .yaml or .json data.
- If omitted derive from the template_name.
- Can have the extension or without it. If withot extension,
will try .yaml first then .json
- Can include the path as well.
template_name: The name for the template file.
- Can have the extension or without it.
- Can include the path as well.
- Can be a comma-separated list giving many template files, in which case
at least one data_filename must be given.
Flag defaults can be overridden by corresponding environment variable, e.g.:
EASYGEN_EY=.yml EASYGEN_ET=.tpl easygen ...
apt install easygen
go get github.com/go-easygen/easygen/...
ls -l $GOPATH/bin
You should find an easygen
executable newly created in there.
export PATH=$PATH:$GOPATH/bin
$ easygen $GOPATH/src/github.com/go-easygen/easygen/test/list0
The colors are: red, blue, white, .
cd $GOPATH/src/github.com/go-easygen/easygen
$ easygen test/list1
The quoted colors are: "red", "blue", "white", .
$ easygen test/listfunc1 test/list0
red, blue, white.
And also check out the provided more examples in the document.
It can be used as a code generator, for example, command line parameter handling code generator, or anything that is structurally repetitive, like the following:
- Introduction to easygen and its philosophy
- Easygen is now coding itself
- Showcasing the power of easygen with ffcvt
- Easygen for HTML mock-up
- Moving beyond code-gen and mock-up, using easygen in real life creating GPT partitions
Ready to get started? Then check out Getting Started to start building your way to turn your data into any form, any way you want.
As explained above, one practical use of easygen
is to auto-generating Go code for command line parameter handling, for both viper
and cobra
, and Go's built-in flag
package.
Currently, easygen
's command line parameter handling is built on top of Go's built-in flag
package, and the handling code is entirely generated by easygen
itself. Thus, showing how easygen
is handling the command line parameters itself also means showing what functionality the auto-generated command line parameter handling code can do for you.
Currently, there are three tiers program parameters can be given:
- Default values defined within the program, so that program parameters can have meaningful defaults to start with
- Values defined in environment variables
- Values passed from command line
The latter will have higher priority and will override values defined formerly. I.e., the values from command line will override that in environment variables, which in turn override program defaults.
We will use the -ts
, template string, as an example to illustrate. The program defaults is empty, which means using the .tmpl
template file the same as the .yaml
data file. We will override that first by environment variable, then from command line, illustrated in next section.
See,
A cookbook on how to jump-start a cli
based command line handling program
Check here for more on using easygen
the command line tool.
The easygen
is a library as well as a command line tools. Not only it is super easy to use, it is super easy to extend as well.
The restructured easygen
can now be a building block that people can easily extend, any extra functionalities, or extra feature that it depends on, or any external dependencies are now moved out to sub modules. Thus the library users can now pick and choose exactly what they want from the library.
- The egVar package example shows how to add the variable name manipulation on top of the default library.
- The egCal package example shows how to add the variable name manipulation and generic calculation functionalities, together with the default functions, all at the same time.
To put them all together, check out the easygen
's main.go
:
package main
import (
"flag"
"os"
"github.com/go-easygen/easygen"
"github.com/go-easygen/easygen/egCal"
"github.com/go-easygen/easygen/egVar"
)
//go:generate sh -v easygen.gen.sh
////////////////////////////////////////////////////////////////////////////
// Main
func main() {
flag.Usage = Usage
flag.Parse()
// One mandatory non-flag arguments
if flag.NArg() < 1 {
Usage()
}
tmpl0 := easygen.NewTemplate().Customize()
tmpl := tmpl0.Funcs(easygen.FuncDefs()).
Funcs(egVar.FuncDefs()).Funcs(egCal.FuncDefs())
args := flag.Args()
if len(easygen.Opts.TemplateStr) > 0 {
easygen.Process0(tmpl, os.Stdout, easygen.Opts.TemplateStr, args...)
} else {
easygen.Process(tmpl, os.Stdout, args...)
}
}
It has been as simple as this up until version 3. I.e., it's quite simple to make use of easygen
as a package.
The easygen
has gone through four different versions whose API are a bit different between them.
To always stay at the latest version, import
"github.com/go-easygen/easygen"
in your Go code. However, to stay within a certain version, import
the following package respectively to what you need:
- V4: "gopkg.in/easygen.v4"
- V3: "gopkg.in/easygen.v3"
- V2: "gopkg.in/easygen.v2"
- V1: "gopkg.in/easygen.v1"
To see the differences between them, check out
Gerrit Renker
https://github.com/grrtrr
All patches welcome.