easygen

package module
v0.0.0-...-85a57d9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2016 License: MIT Imports: 13 Imported by: 0

README

easygen - Easy to use universal code/text generator

Codeship Status for suntong/easygen Go Report Card MIT License

Install

go get github.com/suntong/easygen
ls -l $GOPATH/bin

Test

export PATH=$PATH:$GOPATH/bin

$ easygen $GOPATH/src/github.com/suntong/easygen/Test/list0
The colors are: red, blue, white, .

cd $GOPATH/src/github.com/suntong/easygen

$ easygen Test/list1 
The quoted colors are: "red", "blue", "white", .

$ easygen -tf test/listfunc1 test/list0
red, blue, white.
$ go test -v 
=== RUN TestList0
--- PASS: TestList0 (0.00s)
        easygen_test.go:17: First and plainest list test
=== RUN TestList1Text
--- PASS: TestList1Text (0.00s)
        easygen_test.go:28: Second test, with text template
=== RUN TestList1HTML
--- PASS: TestList1HTML (0.00s)
        easygen_test.go:36: Second test, with html template
=== RUN TestListFunc1
--- PASS: TestListFunc1 (0.00s)
        easygen_test.go:47: Test custom template function - minus1
=== RUN: ExampleFunc1
--- PASS: ExampleFunc1 (0.00s)
=== RUN: ExampleList0Func1
--- PASS: ExampleList0Func1 (0.00s)
=== RUN: ExampleList0StrTemplate
--- PASS: ExampleList0StrTemplate (0.00s)
=== RUN: ExampleFunc2
--- PASS: ExampleFunc2 (0.00s)
=== RUN: ExampleTestExample
--- PASS: ExampleTestExample (0.00s)
=== RUN: ExampleCommandLineCobraViper
--- PASS: ExampleCommandLineCobraViper (0.00s)
=== RUN: ExampleCommandLineOptInitFull
--- PASS: ExampleCommandLineOptInitFull (0.00s)
PASS
ok      github.com/suntong/easygen   0.014s

Help

$ easygen

Usage:
 easygen [flags] YamlFileName

  -debug=0: debugging level
  -et=".tmpl": extension of template file
  -ey=".yaml": extension of yaml file
  -html=false: treat the template file as html instead of text
  -tf="": .tmpl template file name (default: same as .yaml file)
  -ts="": template string (in text)

YamlFileName: The name for the .yaml data and .tmpl template file
        Only the name part, without extension. Can include the path as well.

Details

My (updated) blog about it is at here, and here.

## Command line flag handling code auto-generation

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:

  1. Default values defined within the program, so that program parameters can have meaningful defaults to start with
  2. Values defined in environment variables
  3. 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.

echo 'Name: some-init-method' > /tmp/var.yaml

$ EASYGEN_TS='{{.Name}}' easygen /tmp/var
some-init-method

I.e., with the environment variable EASYGEN_TS, the .tmpl template file is not used.

$ EASYGEN_TS='{{.Name}}' easygen -ts '{{ck2uc .Name}}' /tmp/var
SomeInitMethod

I.e., command line value takes the highest priority, even overriding the environment variable EASYGEN_TS's value.

As such, if you have a different naming convention than using .tmpl for template file and .yaml for data file, you can override them in environment variables, EASYGEN_ET and EASYGEN_EY, so that you don't need to use -et and/or -ey to override them from command line each time.

echo 'Name: myConstantVariable' > /tmp/var.yml

$ EASYGEN_EY=.yml easygen -ts '{{clc2ss .Name}}' /tmp/var
MY_CONSTANT_VARIABLE
## Tips

You can use easygen as an generic Go template testing tool with the -ts commandline option. For example,

echo "Age: 16" > /tmp/age.yaml

$ easygen -ts "{{.Age}}" /tmp/age
16

$ easygen -ts '{{printf "%x" .Age}}' /tmp/age
10

echo '{FirstName: John, LastName: Doe}' > /tmp/name.yaml

$ easygen -ts '{{.FirstName}}'\''s full name is {{printf "%s%s" .FirstName .LastName | len}} letters long.' /tmp/name
John's full name is 7 letters long.

$ easygen -ts "{{.FirstName}} {{ck2ss .LastName}}'s full name is "'{{len (printf "%s%s" .FirstName .LastName)}} letters long.' /tmp/name
John DOE's full name is 7 letters long.

echo 'Name: some-init-method' > /tmp/var.yaml

$ easygen -ts '{{.Name}} {{6 | minus1}} {{minus1 6}} {{ck2lc .Name}} {{ck2uc .Name}}' /tmp/var
some-init-method 5 5 someInitMethod SomeInitMethod

Author(s)

Tong SUN

suntong from cpan.org

All patches welcome.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(HTML bool, fileName string) string

Generate will produce output from the template according to driving data

func Generate0

func Generate0(HTML bool, strTempl string, fileName string) string

Generate0 will produce output according from driving data without a template file

func Generate2

func Generate2(HTML bool, fileNameTempl string, fileName string) string

Generate2 will produce output according to the given template and driving data files

func TFStringsInit

func TFStringsInit()

TFStringsInit does initialization for strings related template functions

func Usage

func Usage()

Usage function shows help on commandline usage

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
	// contains filtered or unexported fields
}

The Options struct defines the structure to hold the commandline values

var Opts Options

Opts holds the actual values from the command line parameters

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL