Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.e.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ To put them all together, check out,

#### > {{cat "cmd/easygen/main.go" | color "go"}}

### Different Versions

The `easygen` has gone through three 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:

- V3: "[gopkg.in/easygen.v3](https://gopkg.in/easygen.v3)"
- V2: "[gopkg.in/easygen.v2](https://gopkg.in/easygen.v2)"
- V1: "[gopkg.in/easygen.v1](https://gopkg.in/easygen.v1)"

To see the differences between them, check out

- [V3 vs V2](https://github.com/go-easygen/easygen/wiki/V3-vs-V2)
- [V2 vs V1](https://github.com/go-easygen/easygen/wiki/V2-vs-V1)

## Author(s) & Contributor(s)

Tong SUN
Expand Down
35 changes: 26 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
- [Command line](#command-line)
- [The library ](#the-library-)
- [> cmd/easygen/main.go](#-cmdeasygenmaingo)
- [Author(s) & Contributor(s)](#author(s)-&-contributor(s))
- [Different Versions](#different-versions)
- [Author(s) & Contributor(s)](#author(s)-&-contributor(s))

# easygen - Easy to use universal code/text generator

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 can be used as a text or html generator for _arbitrary_ purposes with _arbitrary_ data and templates.

It can be used as a code generator, or anything that is structurally repetitive. Some command line parameter handling code generator are provided as examples, including the Go's built-in `flag` package, and the `viper` & `cobra` package.

Expand All @@ -37,6 +38,8 @@ You can even use easygen as a generic Go template testing tool using the `-ts` c

### $ easygen
```sh
easygen version git-master

Usage:
easygen [flags] YamlFileName [YamlFileName...]

Expand All @@ -48,12 +51,6 @@ Flags:
extension of template file (default ".tmpl")
-ey extension
extension of yaml file (default ".yaml")
-html
treat the template file as html instead of text
-rf string
replace from, the from string used for the replace function
-rt string
replace to, the to string used for the replace function
-tf name(s)
.tmpl (comma-separated) template file name(s) (default: same as .yaml file)
-ts string
Expand Down Expand Up @@ -187,6 +184,8 @@ import (
////////////////////////////////////////////////////////////////////////////
// Global variables definitions

var version = "git-master"

////////////////////////////////////////////////////////////////////////////
// Main

Expand All @@ -198,7 +197,6 @@ func main() {
if flag.NArg() < 1 {
Usage()
}
easygen.TFStringsInit() // Done *after* flag parsing!

tmpl0 := easygen.NewTemplate().Customize()
tmpl := tmpl0.Funcs(easygen.FuncDefs()).
Expand All @@ -213,6 +211,25 @@ func main() {
}
```

### Different Versions

The `easygen` has gone through three 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:

- V3: "[gopkg.in/easygen.v3](https://gopkg.in/easygen.v3)"
- V2: "[gopkg.in/easygen.v2](https://gopkg.in/easygen.v2)"
- V1: "[gopkg.in/easygen.v1](https://gopkg.in/easygen.v1)"

To see the differences between them, check out

- [V3 vs V2](https://github.com/go-easygen/easygen/wiki/V3-vs-V2)
- [V2 vs V1](https://github.com/go-easygen/easygen/wiki/V2-vs-V1)

## Author(s) & Contributor(s)

Tong SUN
Expand Down
17 changes: 0 additions & 17 deletions cmd/easygen/easygen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ StructName: Options
StructVar: Opts

Options:
- Name: HTML
Type: bool
Flag: html
Value: false
Usage: treat the template file as html instead of text

- Name: TemplateStr
Type: string
Expand All @@ -50,18 +45,6 @@ Options:
Value: '".tmpl"'
Usage: "`extension` of template file"

- Name: StrFrom
Type: string
Flag: rf
Value: '""'
Usage: "replace from, the from string used for the replace function"

- Name: StrTo
Type: string
Flag: rt
Value: '""'
Usage: "replace to, the to string used for the replace function"

- Name: Debug
Type: int
Flag: debug
Expand Down
14 changes: 0 additions & 14 deletions cmd/easygen/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ const progname = "easygen" // os.Args[0]
func init() {

// set default values for command line parameters
flag.BoolVar(&easygen.Opts.HTML, "html", false,
"treat the template file as html instead of text")
flag.StringVar(&easygen.Opts.TemplateStr, "ts", "",
"template string (in text)")
flag.StringVar(&easygen.Opts.TemplateFile, "tf", "",
Expand All @@ -36,10 +34,6 @@ func init() {
"`extension` of yaml file")
flag.StringVar(&easygen.Opts.ExtTmpl, "et", ".tmpl",
"`extension` of template file")
flag.StringVar(&easygen.Opts.StrFrom, "rf", "",
"replace from, the from string used for the replace function")
flag.StringVar(&easygen.Opts.StrTo, "rt", "",
"replace to, the to string used for the replace function")
flag.IntVar(&easygen.Opts.Debug, "debug", 0,
"debugging `level`")

Expand All @@ -60,14 +54,6 @@ func init() {
len(os.Getenv("EASYGEN_ET")) != 0 {
easygen.Opts.ExtTmpl = os.Getenv("EASYGEN_ET")
}
if len(easygen.Opts.StrFrom) == 0 ||
len(os.Getenv("EASYGEN_RF")) != 0 {
easygen.Opts.StrFrom = os.Getenv("EASYGEN_RF")
}
if len(easygen.Opts.StrTo) == 0 ||
len(os.Getenv("EASYGEN_RT")) != 0 {
easygen.Opts.StrTo = os.Getenv("EASYGEN_RT")
}

}

Expand Down
3 changes: 1 addition & 2 deletions cmd/easygen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
////////////////////////////////////////////////////////////////////////////
// Global variables definitions

var version = "master"
var version = "git-master"

////////////////////////////////////////////////////////////////////////////
// Main
Expand All @@ -50,7 +50,6 @@ func main() {
if flag.NArg() < 1 {
Usage()
}
easygen.TFStringsInit() // Done *after* flag parsing!

tmpl0 := easygen.NewTemplate().Customize()
tmpl := tmpl0.Funcs(easygen.FuncDefs()).
Expand Down
4 changes: 1 addition & 3 deletions cmd/easygen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestExec(t *testing.T) {
testEasygen(t, "list0", "-tf", "list0", "list0")
testEasygen(t, "list0", "-tf", "list0.tmpl", "list0")
testEasygen(t, "list0", "-tf", "list0.tmpl", "list0.yaml")
testEasygen(t, "list0E", "-tf", "list0E", "list0")

testEasygen(t, "list1", "list1")
testEasygen(t, "listfunc1", "listfunc1")
Expand All @@ -75,9 +76,6 @@ func TestExec(t *testing.T) {
//Test HTML
testEasygen(t, "list1HTML", "-tf", "list1HTML", "list1")

//Test String Functions
testEasygen(t, "strings0", "-rf", `a(x*)b`, "-rt", `${1}W`, "strings0")
testEasygen(t, "strings1", "-rf", "HTML", "-rt", "XML", "-tf", "strings1", "strings0")
// varcaser string functions
testEasygen(t, "var0", "-ts", "{{.Name}}", "var0")
testEasygen(t, "var1", "-ts", "{{clk2uc .Name}}", "var0")
Expand Down
3 changes: 0 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ const progname = "easygen" // os.Args[0]

// The Options struct defines the structure to hold the commandline values
type Options struct {
HTML bool // treat the template file as html instead of text
TemplateStr string // template string (in text)
TemplateFile string // .tmpl (comma-separated) template file `name(s)` (default: same as .yaml file)
ExtYaml string // `extension` of yaml file
ExtTmpl string // `extension` of template file
StrFrom string // replace from, the from string used for the replace function
StrTo string // replace to, the to string used for the replace function
Debug int // debugging `level`
}
23 changes: 5 additions & 18 deletions easygen.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
////////////////////////////////////////////////////////////////////////////
// Package: easygen
// Purpose: Easy to use universal code/text generator
// Authors: Tong Sun (c) 2015-17, All rights reserved
// Authors: Tong Sun (c) 2015-18, All rights reserved
////////////////////////////////////////////////////////////////////////////

/*
Expand Down Expand Up @@ -35,7 +35,7 @@ import (
// Global variables definitions

// EgData -- EasyGen driven Data
type EgData map[string]interface{}
type EgData interface{}

// Opts holds the actual values from the command line parameters
var Opts = Options{ExtYaml: ".yaml", ExtTmpl: ".tmpl"}
Expand All @@ -50,18 +50,17 @@ func ReadDataFile(fileName string) EgData {
return ReadYamlFile(fileName + Opts.ExtYaml)
} else if IsExist(fileName) {
return ReadYamlFile(fileName)
} else {
checkError(fmt.Errorf("DataFile '%s' cannot be found", fileName))
}
return make(EgData)
checkError(fmt.Errorf("DataFile '%s' cannot be found", fileName))
return nil
}

// ReadYamlFile reads given YAML file as EgData
func ReadYamlFile(fileName string) EgData {
source, err := ioutil.ReadFile(fileName)
checkError(err)

m := make(EgData)
m := make(map[interface{}]interface{})

err = yaml.Unmarshal(source, &m)
checkError(err)
Expand All @@ -78,16 +77,6 @@ func IsExist(fileName string) bool {
// https://gist.github.com/mastef/05f46d3ab2f5ed6a6787#file-isexist_vs_isnotexist-go-L35-L56
}

// GetEnv returns the Environment variables in a map
func GetEnv() map[string]string {
env := make(map[string]string)
for _, e := range os.Environ() {
sep := strings.Index(e, "=")
env[e[0:sep]] = e[sep+1:]
}
return env
}

// Process will process the standard easygen input: the `fileName` is for both template and data file names, and produce output from the template according to the corresponding driving data.
func Process(t Template, wr io.Writer, fileNames ...string) error {
return Process2(t, wr, fileNames[0], fileNames...)
Expand Down Expand Up @@ -147,15 +136,13 @@ func Execute(t Template, wr io.Writer, fileNameT string, m EgData) error {
tn, err := t.ParseFiles(fileNameT)
checkError(err)

m["ENV"] = GetEnv()
return tn.ExecuteTemplate(wr, filepath.Base(fileNameT), m)
}

// Process0 will produce output according to the driving data *without* a template file, using the string from strTempl as the template
func Process0(t Template, wr io.Writer, strTempl string, fileNames ...string) error {
fileName := fileNames[0]
m := ReadDataFile(fileName)
m["ENV"] = GetEnv()

tmpl, err := t.Parse(strTempl)
checkError(err)
Expand Down
5 changes: 2 additions & 3 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package easygen

import (
"io"
"os"
"strings"
"text/template"
)
Expand Down Expand Up @@ -98,9 +99,7 @@ var egFuncMap = FuncMap{
"regexpReplaceAllStringFunc": regexpReplaceAllStringFunc,
"regexpSplit": regexpSplit,

"replace": replace,
"replacec": replacec,

"ENV": os.Getenv,
"quote4shell": quote4shell,

"minus1": minus1,
Expand Down
3 changes: 3 additions & 0 deletions test/list0E.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The colors are: red, blue, white, .
The system shell is: /bin/bash
Different shells are: /bin/bash-red, /bin/bash-blue, /bin/bash-white,
4 changes: 2 additions & 2 deletions test/list0E.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
The colors are: {{range .Colors}}{{.}}, {{end}}.
The system shell is: {{.ENV.SHELL}}
Different shells are: {{range .Colors}}{{$.ENV.SHELL}}-{{.}}, {{end}}
The system shell is: {{ENV "SHELL"}}
Different shells are: {{range .Colors}}{{ENV "SHELL"}}-{{.}}, {{end}}
22 changes: 1 addition & 21 deletions tf-strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,7 @@ func regexpSplit(s string, regExp string, n int) []string {
}

////////////////////////////////////////////////////////////////////////////
// CLI Function Definitions

// TFStringsInit does initialization for strings related template functions
func TFStringsInit() {
re = regexp.MustCompile(`(?i)` + Opts.StrFrom)
// case sensitive string replace
rec = regexp.MustCompile(Opts.StrFrom)

}

//==========================================================================
// command-line replacing option template function

func replace(replStr string) string {
return re.ReplaceAllString(replStr, Opts.StrTo)
}

// replacec does a case sensitive string replace
func replacec(replStr string) string {
return rec.ReplaceAllString(replStr, Opts.StrTo)
}
// Misc

// quote4shell -- quote file name for shell.
// So "%bob's file" will be quoted as '%bob'\''s file'
Expand Down