filegenerator

package module
v0.0.0-...-19e1fa6 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2021 License: MIT Imports: 6 Imported by: 0

README

filegenerator - a toolbox for file generation

codecov

This toolbox will help you when working with templates and setup of file structures in the context of application generation etc.

Engine

The Engine will act as a container for different kind of Generator that have to implement the Interface with the only method:

Run() error

Sample code how to setup and run an Engine:

var e Engine err := e.Run()

Generator

Generator is an interface and has to implemented for concrete use cases

 // Generator interface for Engine
type Generator interface {
Run() error // runs the generator
}

Implemented Generators are added to the Engine using the method:

e.AddGenerator([Generator](##Generator))

The execution of the method Run() executes all added generators

e.Run()

CopyGenerator

CopyGenerator create a new Generator for copying files from one place into a directory specified by the method:

Add(file, todir string) error

DirectoryGenerator

Creates all directories that are added to the DirectoryGenerator by the method:

Add(path string) error

TemplateGenerator

Creates an new Generator which uses an optional FuncMap to enrich the available functions that can be directly used within templates

New Template generator will be created using the method:

func NewTemplateGenerator(fmap template.FuncMap) *TemplateGenerator

Template creates a *template.Template including a FuncMap when added by calling the NewTemplateGenerator method

Template(name string) (tpl *template.Template)

The method Add adds a template to the generator. It takes a template and an error to be used convinently with existing template functions

Add(tpl *template.Template, err error) error

Before a template can be executed it has to be parsed. For this purpose there are 2 ways implemented

ParseWriter adds the relevant information for getting the right template writing it to an io.Writer. The data is used for template generation

ParseWriter(name string, wr io.Writer, data interface{}) error

ParseFilename holds the relevant information for getting the right template writing it to a file. The Data is used for template generation

ParseFilename(name string, file string, data interface{}) error

Sample:

const letter =` 
    Dear {{.Name}},
    {{- if .Attended}}
    It was a pleasure to see you at the wedding.
    {{- else}}
    It is a shame you couldn't make it to the wedding.
    {{- end}}
    {{with .Gift -}}
    {{title .}}
    Thank you for the lovely {{.}}.
    {{- end}}
    Best wishes,
    Josie
    `

// Prepare some data to insert into the template.
type Recipient struct {
	Name, Gift string
	Attended   bool
}

var recipient = Recipient{
	"Aunt Mildred", "bone china tea set", false}

func main() {
	funcMap := template.FuncMap{
		"title": strings.Title,
	}

	tg := NewTemplateGenerator(funcMap)

	if err := tg.Add(tg.Template("letter").Parse(letter)); err != nil {
		log.Fatal(err)
	}

	if tg.ParseWriter("letter", os.Stdout, recipient) != nil {
		log.Fatal("parse failed")
	}`

	if err := tg.Run(); err != nil {
		log.Fatal(err)
	}
}

Documentation

Overview

scuffold project main.go

scuffold project main.go

Package filegenerator - a toolbox for file generation

This toolbox will help you when working with templates and setup of file structures in the context of application generation etc.

Engine

The Engine will act as a container for different kind of Generator(##Generator) that have to implement the Interface with the only method:

Run() error

Sample code how to setup and run an Engine:

var e Engine
err := e.Run()

Generator

Generator is an interface and has to implemented for concrete use cases

// Generator interface for Engine
type Generator interface {
  Run() error // runs the generator
}

Implemented Generators are added to the Engine using the method:

e.AddGenerator([Generator](##Generator))

The execution of the method Run() executes all added generators

e.Run()

CopyGenerator

CopyGenerator create a new Generator for copying files from one place into a directory specified by the method:

Add(file, todir string) error

DirectoryGenerator Creates all directories that are added to the DirectoryGenerator by the method:

Add(path string) error

TemplateGenerator Creates an new Generator which uses an optional FuncMap to enrich the available functions that can be directly used within templates

New Template generator will be created using the method:

func NewTemplateGenerator(fmap template.FuncMap) *TemplateGenerator

Template creates a *template.Template including a FuncMap when added by calling the NewTemplateGenerator method

Template(name string) (tpl *template.Template)

The method Add adds a template to the generator. It takes a template and an error to be used convinently with existing template functions

Add(tpl *template.Template, err error) error

Before a template can be executed it has to be parsed. For this purpose there are 2 ways implemented

ParseWriter adds the relevant information for getting the right template writing it to an io.Writer. The data is used for template generation

ParseWriter(name string, wr io.Writer, data interface{}) error

ParseFilename holds the relevant information for getting the right template writing it to a file. The Data is used for template generation

ParseFilename(name string, file string, data interface{}) error

Sample: ``` const letter =`

Dear {{.Name}},
{{- if .Attended}}
It was a pleasure to see you at the wedding.
{{- else}}
It is a shame you couldn't make it to the wedding.
{{- end}}
{{with .Gift -}}
{{title .}}
Thank you for the lovely {{.}}.
{{- end}}
Best wishes,
Josie
`

// Prepare some data to insert into the template.

type Recipient struct {
	Name, Gift string
	Attended   bool
}
var recipient = Recipient{
	"Aunt Mildred", "bone china tea set", false}
func main() {
	funcMap := template.FuncMap{
		"title": strings.Title,
	}

	tg := NewTemplateGenerator(funcMap)

	if err := tg.Add(tg.Template("letter").Parse(letter)); err != nil {
		log.Fatal(err)
	}

	if tg.ParseWriter("letter", os.Stdout, recipient) != nil {
		log.Fatal("parse failed")
	}`

	if err := tg.Run(); err != nil {
		log.Fatal(err)
	}
}

```

scuffold project main.go

scuffold project main.go

scuffold project main.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CopyGenerator

type CopyGenerator struct {
	// contains filtered or unexported fields
}

CopyGenerator holds all files that should be copied over into a new location

func NewCopyGenerator

func NewCopyGenerator() *CopyGenerator

NewCopyGenerator createas a new Generator for copying files from one place to another

func (*CopyGenerator) Add

func (d *CopyGenerator) Add(file, todir string) error

Add is used to add another 'file' and target directory 'todir' where the file will be copied into

func (CopyGenerator) GetFiles

func (d CopyGenerator) GetFiles() string

func (CopyGenerator) Run

func (d CopyGenerator) Run() error

Run executes to creation of all paths added to the CopyGenerator

type DirectoryGenerator

type DirectoryGenerator struct {
	// contains filtered or unexported fields
}

DirectoryGenerator creates new directories in the file system or skips when already created. DirectoryGenerator has to be added to Engine

func NewDirectoryGenerator

func NewDirectoryGenerator() *DirectoryGenerator

NewDirectoryGenerator creates a new instance of Directory Generator

func (*DirectoryGenerator) Add

func (d *DirectoryGenerator) Add(path string) error

Add adds a new path to the backlog

func (DirectoryGenerator) Run

func (d DirectoryGenerator) Run() error

Run executes to creation of all paths added to the DirectoryGenerator

type Engine

type Engine struct {
	Name string
	// contains filtered or unexported fields
}

Engine holds all Generators and triggers the run. A name is needed to identify each individual run

func NewEngine

func NewEngine(name string) *Engine

func (*Engine) AddGenerator

func (e *Engine) AddGenerator(g Generator) error

AddGenerator adds a new Generator

func (*Engine) Run

func (e *Engine) Run() error

Run executes all Run() functions of each generator

type Generator

type Generator interface {
	Run() error // runs the generator
}

Generator interface for Engine

type TemplateGenerator

type TemplateGenerator struct {
	Data interface{}
	// contains filtered or unexported fields
}

TemplateGenerator produces content based on go templates

func NewTemplateGenerator

func NewTemplateGenerator(fmap template.FuncMap) *TemplateGenerator

NewTemplateGenerator creates an new Generator which uses an option FuncMap to enrich the available functions that can be directly used within templates

func (*TemplateGenerator) Add

func (tg *TemplateGenerator) Add(tpl *template.Template, err error) error

Add adds a template to the generator. It takes a template and an error to be used convinently with existing template functions

e.g tg.Add(tg.Template("letter").Parse(letter))

func (*TemplateGenerator) ParseFilename

func (tg *TemplateGenerator) ParseFilename(name string, file string, data interface{}) error

ParseFilename holds the relevant information for getting the right template writing it to a file. The Data is used for template generation

func (*TemplateGenerator) ParseWriter

func (tg *TemplateGenerator) ParseWriter(name string, wr io.Writer, data interface{}) error

ParseWriter adds the relevant information for getting the right template writing it to an io.Writer. The data is used for template generation

func (TemplateGenerator) Run

func (tg TemplateGenerator) Run() error

Run will be called by the 'Engine' and creates the files using the data and templates provided

func (*TemplateGenerator) Template

func (tg *TemplateGenerator) Template(name string) (tpl *template.Template)

Template creates a *template.Template with or without FuncMap and returns it

Jump to

Keyboard shortcuts

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