From 31433ece7cd332cff2d3dfe2941b5aa076e7ceae Mon Sep 17 00:00:00 2001 From: Tong Sun Date: Wed, 2 May 2018 20:20:59 -0400 Subject: [PATCH 1/4] - [-] remove command-line replacing, and no use cli flages --- cmd/easygen/easygen.yaml | 17 ----------------- cmd/easygen/flags.go | 14 -------------- cmd/easygen/main.go | 1 - cmd/easygen/main_test.go | 3 --- config.go | 3 --- template.go | 3 --- tf-strings.go | 22 +--------------------- 7 files changed, 1 insertion(+), 62 deletions(-) diff --git a/cmd/easygen/easygen.yaml b/cmd/easygen/easygen.yaml index 61d5e20..f4321e2 100644 --- a/cmd/easygen/easygen.yaml +++ b/cmd/easygen/easygen.yaml @@ -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 @@ -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 diff --git a/cmd/easygen/flags.go b/cmd/easygen/flags.go index 990aba5..39e9882 100644 --- a/cmd/easygen/flags.go +++ b/cmd/easygen/flags.go @@ -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", "", @@ -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`") @@ -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") - } } diff --git a/cmd/easygen/main.go b/cmd/easygen/main.go index e0b8e10..29a8de0 100644 --- a/cmd/easygen/main.go +++ b/cmd/easygen/main.go @@ -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()). diff --git a/cmd/easygen/main_test.go b/cmd/easygen/main_test.go index 0389c0c..14ab601 100644 --- a/cmd/easygen/main_test.go +++ b/cmd/easygen/main_test.go @@ -75,9 +75,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") diff --git a/config.go b/config.go index a63f1aa..185c9f3 100644 --- a/config.go +++ b/config.go @@ -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` } diff --git a/template.go b/template.go index fec2158..dcefd3b 100644 --- a/template.go +++ b/template.go @@ -98,9 +98,6 @@ var egFuncMap = FuncMap{ "regexpReplaceAllStringFunc": regexpReplaceAllStringFunc, "regexpSplit": regexpSplit, - "replace": replace, - "replacec": replacec, - "quote4shell": quote4shell, "minus1": minus1, diff --git a/tf-strings.go b/tf-strings.go index 07be339..4822ead 100644 --- a/tf-strings.go +++ b/tf-strings.go @@ -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' From 41bf2bf9e41916e9b93348ccf23aa1770628bc20 Mon Sep 17 00:00:00 2001 From: Tong Sun Date: Wed, 2 May 2018 20:58:03 -0400 Subject: [PATCH 2/4] - [*] make it take arbitrary data, and remove .ENV access --- easygen.go | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/easygen.go b/easygen.go index 874c5b4..51b940f 100644 --- a/easygen.go +++ b/easygen.go @@ -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 //////////////////////////////////////////////////////////////////////////// /* @@ -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"} @@ -50,10 +50,9 @@ 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 @@ -61,7 +60,7 @@ 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) @@ -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...) @@ -147,7 +136,6 @@ 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) } @@ -155,7 +143,6 @@ func Execute(t Template, wr io.Writer, fileNameT string, m EgData) error { 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) From 329a7996199fe9914d97abcf51b7b29efa27ab49 Mon Sep 17 00:00:00 2001 From: Tong Sun Date: Wed, 2 May 2018 21:07:23 -0400 Subject: [PATCH 3/4] - [+] provide ENV access back as a func --- cmd/easygen/main_test.go | 1 + template.go | 2 ++ test/list0E.ref | 3 +++ test/list0E.tmpl | 4 ++-- 4 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 test/list0E.ref diff --git a/cmd/easygen/main_test.go b/cmd/easygen/main_test.go index 14ab601..8173bde 100644 --- a/cmd/easygen/main_test.go +++ b/cmd/easygen/main_test.go @@ -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") diff --git a/template.go b/template.go index dcefd3b..9f042cb 100644 --- a/template.go +++ b/template.go @@ -8,6 +8,7 @@ package easygen import ( "io" + "os" "strings" "text/template" ) @@ -98,6 +99,7 @@ var egFuncMap = FuncMap{ "regexpReplaceAllStringFunc": regexpReplaceAllStringFunc, "regexpSplit": regexpSplit, + "ENV": os.Getenv, "quote4shell": quote4shell, "minus1": minus1, diff --git a/test/list0E.ref b/test/list0E.ref new file mode 100644 index 0000000..8904fbb --- /dev/null +++ b/test/list0E.ref @@ -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, diff --git a/test/list0E.tmpl b/test/list0E.tmpl index 49d818e..148e5dd 100644 --- a/test/list0E.tmpl +++ b/test/list0E.tmpl @@ -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}} From cadadf1693228f23760ac1b4b6fde335e0cdeaaa Mon Sep 17 00:00:00 2001 From: Tong Sun Date: Thu, 3 May 2018 20:35:17 -0400 Subject: [PATCH 4/4] - [*] update README --- README.e.md | 19 +++++++++++++++++++ README.md | 35 ++++++++++++++++++++++++++--------- cmd/easygen/main.go | 2 +- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/README.e.md b/README.e.md index 943cfba..242a197 100644 --- a/README.e.md +++ b/README.e.md @@ -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 diff --git a/README.md b/README.md index c69fbde..6e1adc7 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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...] @@ -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 @@ -187,6 +184,8 @@ import ( //////////////////////////////////////////////////////////////////////////// // Global variables definitions +var version = "git-master" + //////////////////////////////////////////////////////////////////////////// // Main @@ -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()). @@ -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 diff --git a/cmd/easygen/main.go b/cmd/easygen/main.go index 29a8de0..e3e2b22 100644 --- a/cmd/easygen/main.go +++ b/cmd/easygen/main.go @@ -37,7 +37,7 @@ import ( //////////////////////////////////////////////////////////////////////////// // Global variables definitions -var version = "master" +var version = "git-master" //////////////////////////////////////////////////////////////////////////// // Main