Skip to content

Substitutes environment variables like envsubst do but with powerful template engine

Notifications You must be signed in to change notification settings

navrocky/muenvsubst

Repository files navigation

MuEnvSubst

Substitutes environment variables using one of the templating engines, as envsubst does, but using a powerfull templating engine.

This is a list of supported template engines:

One of the advantages of this utility is that it is built very small static binary without any dependencies. Architecture of binary is Linux x86 (32 bit). It can be run in any Linux x86 and x86_64, both new and very old versions.

Installation

Download and copy binary to any destination specified in PATH (~/bin, /usr/local/bin, /usr/bin). Do not forget to set execution flag on binary with chmod.

sudo curl https://github.com/navrocky/muenvsubst/releases/download/1.4.0/muenvsubst -Lo /usr/local/bin/muenvsubst
sudo chmod +x /usr/local/bin/muenvsubst

Usage

Substitutes environment variables using Inja templating engine, as envsubst 
does. 

USAGE: ./muenvsubst [ -h, --help <arg> ] [ -i, --in <arg> ] [ -o, 
       --out <arg> ] [ -v, --version ] 

OPTIONAL:
 -h, --help <arg> Print this help. 

 -i, --in <arg>   Input file 

 -o, --out <arg>  Output file 

 -v, --version    Output version information and exit

Inja syntax

Inja inspired by Python's Jinja and supports subset of original Jinja syntax. It much more powerfull then Mustache.

Inja syntax documented here and original Jinja syntax documented here.

Also supported function pipe calling syntax. These are equal expressions:

{{ split("A,B,C", ",") }}
{{ "A,B,C" | split(",") }}

{{ sh("198c126c-2691-463f-9708-1ee485ce4d68", "sed 's/-//g'") }}
{{ "198c126c-2691-463f-9708-1ee485ce4d68" | sh("sed 's/-//g'") }}

Additional Inja functions

  • error - throws an error
  • fromBase64 - decode base64 string
  • fromJson - parse JSON string to object
  • sh - execute shell script
  • split - splits text by delimiter
  • toBase64 - encode base64 string
  • toBool - convert any value to boolean
  • toJson - serialize value to JSON string
  • trim - trims text
  • varToBool - convert variable value to boolean

More Inja examples

Simple variable substitution

echo "Hello, {{ USER }}!" | muenvsubst

then output will be:

Hello, John!

Using variable and function

muenvsubst <<EOF
{%- set username = upper(USER) -%}
Hello, {{ username }}!
EOF

then output will be:

Hello, JOHN!

Render conditional block

USE_GREETER=no USE_GOODBYER=yes muenvsubst << EOF
## if USE_GREETER=="yes"
Hello, {{ USER }}!
## endif
## if USE_GOODBYER=="yes"
Goodbye, {{ USER }}!
## endif
EOF

then output will be:

Goodbye, John!

Using split and loop

USERS="John,Mark,Peter" muenvsubst << EOF
{%- for user in split(USERS,",") -%}
Hello, {{ user }}!
{%- endfor -%}
EOF

then output will be:

Hello, John!
Hello, Mark!
Hello, Peter!

About

Substitutes environment variables like envsubst do but with powerful template engine

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published