template

package
v1.0.0-b001 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2018 License: MIT Imports: 20 Imported by: 0

README

template

Template is a thin wrapper on text/template, the golang templating engine. The primary usecase for the utility is to dynamically modify config templates.

Usage

Typical usage is to read a file and apply a couple variables.

Example Template (ingress.yml.template):

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ .Var "service" }}
  ref: {{ .Env "CURRENT_REF" }}

If we then run the following:

> CURRENT_REF="abcdef" template -f ingress.yml.template --var service="my service"

template will then print to the screen the updated template:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my service
  ref: abcdef

You can alternately read from stdin by omitting the -f flag.

Commandline Flag Reference

-f <TEMPLATE PATH>

The -f flag specifies an input file. If in the form -f - template will read the file from stdin.

-i <TEMPLATE PATH>

The -i flag specifies an addition template file referencable in the master template.

-vars <VARS PATH[.json|(.yml|.yaml|*)]>

The -vars flag specifies an input file with variable definitions. If the filename is .json it will be unmarshalled as json, otherwise it will be unmarshalled as yaml.

-var <KEY>=<VALUE>

The -var flag specifies a variable for the template.

-o <OUTPUT PATH>

The -o flag specifies an output path. If not present, output will be piped to stdout.

-delims "<LEFT DELIMITER>,<RIGHT DELIMITER>

The -delims flag specifies the left and right delimiters for template action, defaults to {{,}}.

Template Function Reference

.Env

Env will return an environment variable. It takes the environment variable name as the first parameter. It can take a default value as a second parameter. If no default is specified, and the environment variable is not present, this will cause an error.

{{ .Env "<var name>" }}

With a default:

{{ .Env "<var name>" "<default value>" }}
.Var

Var will return a variable as set by the commandline. It takes the variable name as the first parameter. It can take a default value as a second parameter. If no default is specified, and the variable is not present, this will cause an error.

{{ .Var "<var name>" }}

With a default:

{{ .Var "<var name>" <default value> }}

Note: Var differs from Env in that var values can be any type, not just strings.

.File

File will return the contents of a given file and inline those contents into the config. Note; the contents of this file will not be processed by the template interpreter, they will appear in the final output as they did on disk.

{{ .File "<file path>" }}

Template pipeline helpers

Template ships with a number of pipeline helpers that can be used with the output of .Var, .Env and even .File.

text/template Reference

More information about the text/template template language can be found here: text template

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Helpers

type Helpers struct{}

Helpers is a namespace for helper functions.

func (Helpers) CreateKey

func (h Helpers) CreateKey(keySize int) string

CreateKey creates an encryption key (base64 encoded).

func (Helpers) UTCNow

func (h Helpers) UTCNow() time.Time

UTCNow returns the current time in utc.

func (Helpers) UUID

func (h Helpers) UUID() string

UUID returns a uuidv4 as a string.

type Template

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

Template is a wrapper for html.Template.

func New

func New() *Template

New creates a new template.

func NewFromFile

func NewFromFile(filepath string) (*Template, error)

NewFromFile creates a new template from a file.

func (*Template) Body

func (t *Template) Body() string

Body returns the template body.

func (*Template) Env

func (t *Template) Env(key string, defaults ...string) (string, error)

Env returns an environment variable.

func (*Template) File

func (t *Template) File(path string) (string, error)

File returns the contents of a file.

func (*Template) HasEnv

func (t *Template) HasEnv(key string) bool

HasEnv returns if an env var is set.

func (*Template) HasFile

func (t *Template) HasFile(path string) bool

HasFile returns if a file exists.

func (*Template) HasVar

func (t *Template) HasVar(key string) bool

HasVar returns if a variable is set.

func (*Template) Helpers

func (t *Template) Helpers() *Helpers

Helpers returns the helpers object.

func (*Template) Name

func (t *Template) Name() string

Name returns the template name if set, or if not set, just "template" as a constant.

func (*Template) Process

func (t *Template) Process(dst io.Writer) error

Process processes the template.

func (*Template) SetVar

func (t *Template) SetVar(key string, value interface{})

SetVar sets a var in the template.

func (*Template) Var

func (t *Template) Var(key string, defaults ...interface{}) (interface{}, error)

Var returns the value of a variable, or panics if the variable is not set.

func (*Template) ViewFuncs

func (t *Template) ViewFuncs() texttemplate.FuncMap

ViewFuncs returns the view funcs.

func (*Template) WithBody

func (t *Template) WithBody(body string) *Template

WithBody sets the template body and returns a reference to the template object.

func (*Template) WithDelims

func (t *Template) WithDelims(left, right string) *Template

WithDelims sets the template action delimiters, treating empty string as default delimiter.

func (*Template) WithInclude

func (t *Template) WithInclude(body string) *Template

WithInclude includes a (sub) template into the rendering assets.

func (*Template) WithName

func (t *Template) WithName(name string) *Template

WithName sets the template name.

func (*Template) WithVar

func (t *Template) WithVar(key string, value interface{}) *Template

WithVar sets a variable and returns a reference to the template object.

func (*Template) WithVars

func (t *Template) WithVars(vars Vars) *Template

WithVars reads a map of variables into the template.

type Vars

type Vars = map[string]interface{}

Vars is a loose type alias to map[string]interface{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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