Documentation ¶
Overview ¶
Package easygen is an easy to use universal code/text generator library.
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.
Many examples have been provided to showcase its functionality, and different ways to use it.
Index ¶
- Variables
- func Generate(HTML bool, fileName string) string
- func Generate0(HTML bool, strTempl string, fileName string) string
- func Generate2(HTML bool, fileNameTempl string, fileNames ...string) (ret string)
- func ParseFiles(HTML bool, filenames ...string) (template, error)
- func TFStringsInit()
- func Usage()
- type Options
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Opts = Options{ExtYaml: ".yaml", ExtTmpl: ".tmpl"}
Opts holds the actual values from the command line parameters
Functions ¶
func Generate ¶
Generate will produce output from the template according to the corresponding driving data, fileName is for both template and data file name
Example (CommandLineCobraViper) ¶
Test the provided Cobra/Viper command line flag auto generation
package main import ( "fmt" "github.com/go-easygen/easygen" ) func main() { // Equivalent testing on commandline: // easygen test/commandlineCV fmt.Print(easygen.Generate(false, "test/commandlineCV")) }
Output: flags.Bool("debug", false, "Turn on debugging.") viper.BindPFlag("debug", flags.Lookup("debug")) flags.String("addr", "localhost:5002", "Address of the service.") viper.BindPFlag("addr", flags.Lookup("addr")) flags.String("smtp-addr", "localhost:25", "Address of the SMTP server.") viper.BindPFlag("smtp-addr", flags.Lookup("smtp-addr")) flags.String("smtp-user", "", "User for the SMTP server.") viper.BindPFlag("smtp-user", flags.Lookup("smtp-user")) flags.String("smtp-password", "", "Password for the SMTP server.") viper.BindPFlag("smtp-password", flags.Lookup("smtp-password")) flags.String("email-from", "noreply@abc.com", "The from email address.") viper.BindPFlag("email-from", flags.Lookup("email-from"))
Example (CommandLineCobraViperInit) ¶
Test the provided Cobra/Viper command line flag init() function auto generation
package main import ( "fmt" "github.com/go-easygen/easygen" ) func main() { // Equivalent testing on commandline: // easygen test/commandlineCVFull fmt.Print(easygen.Generate(false, "test/commandlineCVFull")) }
Output: func init() { viper.SetEnvPrefix("DISPATCH") viper.AutomaticEnv() /* When AutomaticEnv called, Viper will check for an environment variable any time a viper.Get request is made. It will apply the following rules. It will check for a environment variable with a name matching the key uppercased and prefixed with the EnvPrefix if set. */ flags := mainCmd.Flags() flags.Bool("Debug", false, "Turn on debugging.") viper.BindPFlag("Debug", flags.Lookup("Debug")) flags.String("addr", "localhost:5002", "Address of the service.") viper.BindPFlag("addr", flags.Lookup("addr")) flags.String("smtp-addr", "localhost:25", "Address of the SMTP server.") viper.BindPFlag("smtp-addr", flags.Lookup("smtp-addr")) flags.String("smtp-user", "", "User for the SMTP server.") viper.BindPFlag("smtp-user", flags.Lookup("smtp-user")) flags.String("smtp-password", "", "Password for the SMTP server.") viper.BindPFlag("smtp-password", flags.Lookup("smtp-password")) flags.String("email-from", "noreply@abc.com", "The from email address.") viper.BindPFlag("email-from", flags.Lookup("email-from")) // Viper supports reading from yaml, toml and/or json files. Viper can // search multiple paths. Paths will be searched in the order they are // provided. Searches stopped once Config File found. viper.SetConfigName("CommandLineCV") // name of config file (without extension) viper.AddConfigPath("/tmp") viper.AddConfigPath(".") err := viper.ReadInConfig() if err != nil { println("No config file found. Using built-in defaults.") } }
Example (Listfunc1) ¶
Test the provided listfunc1, template and data
package main import ( "fmt" "github.com/go-easygen/easygen" ) func main() { // Equivalent testing on commandline: // easygen test/listfunc1 fmt.Print(easygen.Generate(false, "test/listfunc1")) }
Output: red, blue, white.
Example (Listfunc2) ¶
Test the provided listfunc2, template and data
package main import ( "fmt" "github.com/go-easygen/easygen" ) func main() { // Equivalent testing on commandline: // easygen test/listfunc2 fmt.Print(easygen.Generate(false, "test/listfunc2")) }
Output: some-init-method 5 5 someInitMethod SomeInitMethod
func Generate0 ¶
Generate0 will produce output according from driving data without a template file, using the string from strTempl as the template
Example (List0StrTemplate) ¶
Test string template with list0 data
package main import ( "fmt" "github.com/go-easygen/easygen" ) func main() { // Equivalent testing on commandline: // easygen -ts '{{range .Colors}}{{.}}, {{end}}' test/list0 fmt.Print(easygen.Generate0(false, "{{range .Colors}}{{.}}, {{end}}", "test/list0")) }
Output: red, blue, white,
Example (Split0) ¶
Test the string split function in template
package main import ( "fmt" "github.com/go-easygen/easygen" ) func main() { // Equivalent testing on commandline: // easygen -ts '{{split .Colorlist}}' test/list0 fmt.Print(easygen.Generate0(false, `{{split .Colorlist}}`, "test/list0")) }
Output: [red blue white]
Example (Split1) ¶
Test the string split function in template again
package main import ( "fmt" "github.com/go-easygen/easygen" ) func main() { // Equivalent testing on commandline: // easygen -ts '{{range ... {{end}}' test/list0 fmt.Print(easygen.Generate0(false, `{{range (split .Colorlist)}}{{.}} {{end}}`, "test/list0")) }
Output: red blue white
Example (StringsCmp) ¶
Test string comparison in template
package main import ( "fmt" "github.com/go-easygen/easygen" ) func main() { // Equivalent testing on commandline: // easygen -ts '{{The {{if ... {{end}}.' test/strings0 fmt.Print(easygen.Generate0(false, `The {{if eq .StrTest "-AB-axxb- HTML Html html"}}eq says Yea{{else}}eq says Nay{{end}} but {{if eqf .StrTest "-AB-axxb- HTML Html html"}}eqf says Yea{{else}}eqf says Nay{{end}}.`, "test/strings0")) }
Output: The eq says Nay but eqf says Yea.
func Generate2 ¶
Generate2 will produce output according to the given template and driving data files, specified via fileNameTempl and fileNames (for data) respectively.
Example (Listfunc1List0) ¶
Test the provided listfunc1 template with list0 data
package main import ( "fmt" "github.com/go-easygen/easygen" ) func main() { // Equivalent testing on commandline: // easygen -tf test/listfunc1 test/list0 fmt.Print(easygen.Generate2(false, "test/listfunc1", "test/list0")) }
Output: red, blue, white.
func ParseFiles ¶
ParseFiles wraps parsing text or HTML template files into a single function, dictated by the first parameter "HTML". By Matt Harden @gmail.com
func TFStringsInit ¶
func TFStringsInit()
TFStringsInit does initialization for strings related template functions
Types ¶
type Options ¶
type Options struct { HTML bool // treat the template file as html instead of text TemplateStr string // template string (in text) TemplateFile string // .tmpl template file `name` (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` }
The Options struct defines the structure to hold the commandline values