Git Product home page Git Product logo

easygen's Introduction

easygen

MIT License GoDoc Go Report Card travis Status Codeship Status for go-easygen/easygen

TOC

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 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.

Usage

$ easygen

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 ...

Install

Install as Debian/Unbuntu package

apt install easygen

Install from source

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

You should find an easygen executable newly created in there.

Test

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 Go Doc document.

Details

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:

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.

easygen itself

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, illustrated in next section.

cli based

See,

A cookbook on how to jump-start a cli based command line handling program

The easygen usage

Command line

Check here for more on using easygen the command line tool.

The library

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:

> cmd/easygen/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.

Different Versions

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:

To see the differences between them, check out

Author(s) & Contributor(s)

Tong SUN
suntong from cpan.org

Gerrit Renker
https://github.com/grrtrr

All patches welcome.

easygen's People

Contributors

suntong avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.