Template renderer with additional helpers based on Go's text/template used by rocker and rocker-compose.
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
rocker/template
exposes some Go's native functions from strings package. Here is the list of them:
compare
- strings.Comparecontains
- strings.ContainscontainsAny
- strings.ContainsAnycount
- strings.CountequalFold
- strings.EqualFoldhasPrefix
- strings.HasPrefixhasSuffix
- strings.HasSuffixindex
- strings.IndexindexAny
- strings.IndexAnyjoin
- strings.JoinlastIndex
- strings.LastIndexlastIndexAny
- strings.LastIndexAnyrepeat
- strings.Repeatreplace
- strings.Replacesplit
- strings.SplitsplitAfter
- strings.SplitAftersplitAfterN
- strings.SplitAfterNsplitN
- strings.SplitNtitle
- strings.TitletoLower
- strings.ToLowertoTitle
- strings.ToTitletoUpper
- strings.ToUppertrim
- strings.TrimtrimLeft
- strings.TrimLefttrimPrefix
- strings.TrimPrefixtrimRight
- strings.TrimRighttrimSpace
- strings.TrimSpacetrimSuffix
- strings.TrimSuffix
Example:
{{ replace "www.google.com" "google" "grammarly" -1 }}
Will yield:
www.grammarly.com
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",...}
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
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
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'
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",
...
}
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
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
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 }}
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 [email protected]
rocker-compose run -var [email protected]
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
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
- Yura Bogdanov [email protected]
(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.