template

package
v0.0.0-...-da7d6d0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2018 License: Apache-2.0, Apache-2.0 Imports: 18 Imported by: 4

README

rocker/template

Template renderer with additional helpers based on Go's text/template used by rocker and rocker-compose.

Helpers

{{ seq To }} or {{ seq From To }} or {{ seq From To Step }}

Sequence generator. Returns an array of integers of a given sequence. Useful when you need to duplicate some configuration, for example scale containers of the same type. Mostly used in combination with range:

{{ range $i := seq 1 5 2 }}
container-$i
{{ end }}

This template will yield:

container-1
container-3
container-5
String functions

rocker/template exposes some Go's native functions from strings package. Here is the list of them:

Example:

{{ replace "www.google.com" "google" "grammarly" -1 }}

Will yield:

www.grammarly.com
{{ json anything }} or {{ anything | json }}

Marshals given input to JSON.

Example:

ENV={{ .Env | json }}

This template will yield:

ENV={"USER":"johnsnow","DOCKER_MACHINE_NAME":"dev","PATH":"/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin",...}
{{ yaml anything }} or {{ anything | yaml }}

Marshals given input to YAML.

Example:

{{ .Env | yaml }}

This template will yield:

USER: johnsnow
DOCKER_MACHINE_NAME: dev
PATH: /usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin
{{ anything | yaml N }} where N is indentation level

Useful if you want to nest a yaml struct into another yaml file:

foo:
  bar:
    {{ .bar | yaml 2 }}

Will indent yaml-encoded .bar into two levels:

foo:
  bar:
    a: 1
    b: 2
{{ shell string }} or {{ string | shell }}

Escapes given string so it can be substituted to a shell command.

Example:

RUN echo {{ "hello\nworld" | shell }}

This template will yield:

RUN echo $'hello\nworld'
{{ dump anything }}

Pretty-prints any variable. Useful for debugging.

Example:

{{ dump .Env }}

This template will yield:

template.Vars{
    "USER":                       "johnsnow",
    "DOCKER_MACHINE_NAME":        "dev",
    "PATH":                       "/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin",
    ...
}
{{ assert expression }}

Raises an error if given expression is false. Positive value is an existing non-nil value, non-empty slice, non-empty string, and non-zero number.

For example assert is useful to check that passed variables are present.

{{ assert .Version }}

If the Version variable is not given, then template processing will fail with the following error:

Error executing template TEMPLATE_NAME, error: template: TEMPLATE_NAME:1:3: executing \"TEMPLATE_NAME\" at <assert .Version>: error calling assert: Assertion failed
{{ image docker_image_name_with_tag }} or {{ image docker_image_name tag }}

Wrapper that is used to substitute images of particular versions derived by artifacts (TODO: link to artifacts doc).

Example:

FROM {{ image "ubuntu" }}
# OR
FROM {{ image "ubuntu:latest" }}
# OR
FROM {{ image "ubuntu" "latest" }}

Without any additional arguments it will resolve into this:

FROM ubuntu:latest

But if you have an artifact that is resulted by a previous rocker build, that can be fed back to rocker as variable, the artifact will be substituted:

# shorten version of an artifact by rocker
RockerArtifacts:
- Name: ubuntu:latest
  Digest: sha256:ead434cd278824865d6e3b67e5d4579ded02eb2e8367fc165efa21138b225f11
# rocker build -vars artifacts/*
FROM ubuntu@sha256:ead434cd278824865d6e3b67e5d4579ded02eb2e8367fc165efa21138b225f11

This feature is useful when you have a continuous integration pipeline and you want to build images on top of each other with guaranteed immutability. Also, this trick can be used with rocker-compose to run images of particular versions devired by the artifacts.

TODO: also describe semver matching behavior

Variables

rocker/template automatically populates os.Environ to the template along with the variables that are passed from the outside. All environment variables are available under .Env.

Example:

HOME={{ .Env.HOME }}

Load file content to a variable

This template engine also supports loading files content to a variables. rocker and rocker-compose support this through a command line parameters:

rocker build -var key=@key.pem
rocker-compose run -var key=@key.pem

If the file path is relative, it will be resolved according to the current working directory.

Usage options:

key=@relative/file/path.txt
key=@../another/relative/file/path.txt
key=@/absolute/file/path.txt
key=@~/.bash_history
key=\@keep_value_as_is

Development

Please install pre-push git hook that will run tests before every push:

cd rocker-template

To run tests manually:

make test

Or to test something particular:

go test -run TestProcessConfigTemplate_Seq

Authors

License

(c) Copyright 2015 Grammarly, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EscapeShellarg

func EscapeShellarg(value string) string

EscapeShellarg escapes any string so it can be safely passed to a shell

func Process

func Process(name string, reader io.Reader, vars Vars, funs Funs) (*bytes.Buffer, error)

Process renders config through the template processor. vars and additional functions are acceptable.

Types

type Funs

type Funs map[string]interface{}

Funs is the list of additional helpers that may be given to the template

type Vars

type Vars map[string]interface{}

Vars describes the data structure of the build variables

func ParseKvPairs

func ParseKvPairs(pairs []string) (vars Vars)

ParseKvPairs parses Vars from a slice of strings e.g. []string{"KEY=VALUE"}

func VarsFromFile

func VarsFromFile(filename string) (vars Vars, err error)

VarsFromFile reads variables from either JSON or YAML file

func VarsFromFileMulti

func VarsFromFileMulti(files []string) (Vars, error)

VarsFromFileMulti reads multiple files and merge vars

func VarsFromStrings

func VarsFromStrings(pairs []string) (vars Vars, err error)

VarsFromStrings parses Vars through ParseKvPairs and then loads content from files for vars values with "@" prefix

func (Vars) IsSet

func (vars Vars) IsSet(key string) bool

IsSet returns true if the given key is set

func (Vars) MarshalJSON

func (vars Vars) MarshalJSON() ([]byte, error)

MarshalJSON serialize Vars to JSON

func (Vars) Merge

func (vars Vars) Merge(varsList ...Vars) Vars

Merge the current Vars structure with the list of other Vars structs

func (Vars) ReplaceString

func (vars Vars) ReplaceString(str string) string

ReplaceString handle vars replacement

func (Vars) ToMapOfInterface

func (vars Vars) ToMapOfInterface() map[string]interface{}

ToMapOfInterface casts Vars to map[string]interface{}

func (Vars) ToStrings

func (vars Vars) ToStrings() (result []string)

ToStrings converts Vars to a slice of strings line []string{"KEY=VALUE"}

func (*Vars) UnmarshalJSON

func (vars *Vars) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON unserialize Vars from JSON string

func (*Vars) UnmarshalYAML

func (vars *Vars) UnmarshalYAML(unmarshal func(interface{}) error) (err error)

UnmarshalYAML parses YAML string and returns Vars

Jump to

Keyboard shortcuts

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