Git Product home page Git Product logo

envtpl's Introduction

Docker Pulls

envtpl

envtpl renders Go templates on the command line using environment variables.

It is directly inspired by the original envtpl, a Python tool for rendering Jinja2 templates.

This port was motivated by the desire to add templating support for template-driven configuration files that needed to be part of a base Docker image without also requiring the installation of Python. For the same reason, I decided not to add variable support to my previous template utility njx, which depends on Node.js.

Despite the difference between Jinja and Go templates, an attempt was made to match the command line syntax of the original envtpl.

The biggest obvious difference is that Go template variables represent a path within a data context, so envtpl variables will need to be prepended with a leading . to match the keys of the internal environment variable map object (see example).

Get it

$ go get github.com/subfuzion/envtpl/...

Usage

envtpl [-o|--output outfile] [-m|--missingkey option] [template]
  • If template is not provided, envtpl reads from stdin
  • If outfile is not provided, envtpl writes to stdout
  • If missingkey is unset or set to either default or invalid, envtpl follows the default behavior of the golang template library and missing keys in the template will be filled in with the string <no value>. If missingkey is set to zero, missing keys will be filled in with the zero value for their data type (ie: an empty string). If missingkey is set to error, envtpl will fail and an error returned to the caller.

Example

greeting.tpl

Hello {{.USER}}

Render the template (assume the value of $USER is 'mary')

envtpl greeting.tpl  # writes "Hello mary" to stdout

USER=bob envtpl greeting.tpl  # overrides "mary" and writes "Hello bob" to stdout

echo "greetings {{.USER}}" | envtpl  # writes "greetings mary" to stdout

envtpl < greeting.tpl > out.txt  # writes "Hello mary" to out.txt
envtpl > out.txt < greeting.tpl  # same thing
cat greeting.tpl | envtpl > out.txt  # same thing

unset USER; envtpl greeting.tpl            # => "Hello <no value>"
unset USER; envtpl -m zero greeting.tpl    # => "Hello "
unset USER; envtpl -m error greeting.tpl   # => "map has no entry for key "USER"", aborts

# Use a GitHub gist with curl
curl -s https://gist.githubusercontent.com/subfuzion/d5a6e3b7c2577902408069deb1d8e4d7/raw/2e4c0b894e2983411a20ffa5ee84aeafa5c6ebfb/greeting.tpl | envtpl
# => Hello, Tony

See test/test.tpl for more examples, including conditional functions and looping over environment variables.

Template Functions

sprig

In addition to the standard set of template actions and functions that come with Go, envtpl also incorporates sprig for additional, commonly used functions.

For example:

echo "Greetings, {{.USER | title}}" | envtpl  # writes "Greetings, Mary" to stdout

In the example, the environment name of the user mary is converted to Mary by the title template function.

For reference, see sprig functions.

environment

To mimic the environment function for the original envtpl, an environment function allows to filter the environment with a prefix string

{{ range $key, $value := environment "TAG_"  }}{{ $key }}="{{ $value }}"{{ end }}

filters all environment variables starting with TAG_.

For example:

$ echo '{{ range $key, $value := environment "GO"  }}{{ $key }} => {{ $value }} {{ "\n" }}{{ end }}' | envtpl
GOPATH => /Users/tony/go
GOROOT => /usr/local/go

Building an envtpl Docker image

Docker Build Status

An image is available on Docker Hub subfuzion/envtpl

You can use run a container like this:

$ echo 'Hello {{ .NAME | title | printf "%s\n" }}' | docker run -i --rm -e NAME=world subfuzion/envtpl
Hello World

# using a gist
$ curl -s https://gist.githubusercontent.com/subfuzion/d5a6e3b7c2577902408069deb1d8e4d7/raw/2e4c0b894e2983411a20ffa5ee84aeafa5c6ebfb/greeting.tpl | docker run -i -e USER=$USER subfuzion/envtpl
Hello, Tony

To build your own local container:

$ make image

The final image is based on scratch and weighs in at less than 7MB:

$ docker images --format "{{ .Repository }}:{{ .Tag }} => {{ .Size }}" subfuzion/envtpl
subfuzion/envtpl:latest => 6.65MB

Test

The Dockerfile for the image explicitly runs tests, so if the image builds successfully, then the tests passed.

There is an automated build for subfuzion/envtpl on Docker Hub. Docker Hub runs tests based on the presence of docker-compose.test.yml, so there is a docker-compose.test.file that simply builds the image and runs envtpl --version. You can test this yourself:

$ make test

Similar Tools

As mentioned above, this tool was inspired by the original envtpl project and motivated to provide something similar without adding a Python dependency to Docker base images.

A search for similar Go-based tools turns up the following:

I haven't spent any time evaluating either yet. However, mattrobenolt/envtpl looks elegantly simple and arschles/envtpl offers tests, glide package management support and more template functionality using sprig.

Neither of these two packages appear to conform to the original envtpl command line syntax, which was one of my goals, although I don't think this is a big deal since all of these spin-off versions use an entirely different template syntax anyway. However, at first glance at least, this variant does offer more input/output options modeled after the original.

I'm inspired by arschles/envtpl to add sprig support for extended template functions, potentially glide support, and definitely tests. This version now has sprig template support, tests, and uses Go 1.1. modules instead of glide.

License

MIT

envtpl's People

Contributors

n-oden avatar ndegory avatar subfuzion avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

envtpl's Issues

Fix docker hub build

Currently the Docker Hub build requires that the alpine binary envtpl be built and pushed to master. If not, the build fails. This needs to be changed.

Allow to change default delimiters

Hi,

I am trying to use envtpl to render Prometheus configs.
Prometheus uses the same template engine, so the result is mixed template configurations.
Allowing to change the default delimiters for envtpl will resolve the issue.

Docker image tags missing

I would like to do

COPY --from=subfuzion/envtpl:v1.0.0 /bin/envtpl /bin/envtpl

in my Dockerfile. However, there is just the latest tag on dockerhub.

I'd appreciate if you could release it with docker build tags aligned with git tags.

Thank you!

USE alipine or busybox base image when something configure

the docker image "scratch" not have "bash/sh", maby change alipine or busybox image?
the scratch not support Dockerfile RUN command.

FROM subfuzion/envtpl
WORKDIR /app
RUN /bin/envtpl /app/tmpl/aaa.conf -o /app/gen/aaa.conf
RUN /bin/envtpl /app/tmpl/bbb.conf -o /app/gen/bbb.conf

/*
container_linux.go:235: starting container process caused "exec: \"/bin/sh\": stat /bin/sh: no such file or directory"
ERROR: Service 'init_gen' failed to build: oci runtime error: container_linux.go:235: starting container process caused "exec: \"/bin/sh\": stat /bin/sh: no such file or directory"
*/

#  must use RUN  ["/bin/envtpl", "/app/tmpl/aaa.conf", "-o", "/app/gen/aaa.conf"]

if i will render multi file, can not support a easy config in docker-compose.yml ,

sample:

image: subfuzion/envtpl  # or use tag "subfuzion/envtpl:alipine"
entrypoint:
      - sh # or bash
      - -c
      - |
        mkdir -p /app/gen
        envtpl /app/tmpl/aaa.conf -o /app/gen/aaa.conf
        envtpl /app/tmpl/bbb.conf -o /app/gen/ccc.conf

Jinja 3.1.0 was released and now I get an error

/code # pip install -U jinja2
Requirement already satisfied: jinja2 in /usr/lib/python3.9/site-packages (3.0.3)
Collecting jinja2
  Using cached Jinja2-3.1.0-py3-none-any.whl (132 kB)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/lib/python3.9/site-packages (from jinja2) (2.1.1)
Installing collected packages: jinja2
  Attempting uninstall: jinja2
    Found existing installation: Jinja2 3.0.3
    Uninstalling Jinja2-3.0.3:
      Successfully uninstalled Jinja2-3.0.3
Successfully installed jinja2-3.1.0
/code # envtpl < ci-templates/service-frontend.yml.j2 > service.yml
Traceback (most recent call last):
  File "/usr/bin/envtpl", line 33, in <module>
    sys.exit(load_entry_point('envtpl==0.6.0', 'console_scripts', 'envtpl')())
  File "/usr/bin/envtpl", line 25, in importlib_load_entry_point
    return next(matches).load()
  File "/usr/lib/python3.9/importlib/metadata.py", line 77, in load
    module = import_module(match.group('module'))
  File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/usr/lib/python3.9/site-packages/envtpl.py", line 150, in <module>
    @jinja2.contextfunction
AttributeError: module 'jinja2' has no attribute 'contextfunction'

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.