jirutka / esh

Simple templating engine based on shell.
MIT License
214 stars 16 forks source link
awk posix-sh shell template-engine

= ESH – Embedded SHell :source-language: sh :script-name: esh :script-sha1: 9084e3e8e70e4ea81c40cd1cf85559196c0fa2cc :gh-name: jirutka/{script-name} :version: 0.3.2

ifdef::env-github[] image:https://github.com/{gh-name}/workflows/CI/badge.svg[Build Status, link=https://github.com/{gh-name}/actions?query=workflow%3A%22CI%22] endif::env-github[]

esh (embedded shell) is a templating engine for evaluating shell commands embedded in arbitrary templates. It’s like ERB (Embedded RuBy) for shell, intended to be used for templating configuration files. Unlike ERB it provides support for including one ESH template into another (since version 0.2.0).

The template processing consists of two phases: conversion to a shell script and evaluation of that script. You can even run just the conversion phase: esh -d will dump a shell script that you can execute directly by a shell (even on a system without esh installed). However, in that case, you will not get error messages source-mapped to point to the locations in the template file.

esh is implemented in ~290 lines (LoC) of shell and awk.

== Requirements

== Installation

image::https://repology.org/badge/vertical-allrepos/esh-shell-template-engine.svg?columns=4[Packaging status, align=center, link=https://repology.org/project/esh-shell-template-engine/versions]

=== Alpine Linux

Install package {script-name} from the Alpine’s community repository:

[source, subs="+attributes"] apk add {script-name}

=== Fedora

[source, subs="+attributes"] dnf install {script-name}

=== Void Linux

[source, subs="+attributes"] xbps-install {script-name}

=== On CI

You can add {script-name} as a submodule into your repository or download it on demand, e.g.:

[source, subs="+attributes"] wget https://raw.githubusercontent.com/{gh-name}/v{version}/{script-name} \ && echo '{script-sha1} {script-name}' | sha1sum -c \ || exit 1

=== Manually

. Download and unpack release tarball: [source, subs="+attributes"] wget https://github.com/{gh-name}/archive/v{version}/{script-name}-{version}.tar.gz tar -xzf {script-name}-{version}.tar.gz cd {script-name}-{version}

. Run tests: [source] make test

. Build a man page and install esh: + [source] make install prefix=/usr/local DESTDIR=/ + or just copy esh whether you want, it’s a plain shell script.

== Usage

Read man page link:{script-name}.1.adoc[{script-name}(1)].

== Examples

[source, nginx]

http { access_log <%= $logs_dir/access.log %> main;

resolver <%= $(sed -En 's/^nameserver ([^#]+)/\1/p' /etc/resolv.conf) %>;

<% if nginx -V 2>&1 | grep -q lua-nginx-module; then -%>
lua_package_path '<%= $(pkg-config --variable=INSTALL_LMOD lua) %>/?.lua';
<% fi -%>

<%+ ./http-common.esh %>

<%# The rest of the config is omitted %>

}

== Credits

ESH template syntax is based on ERB (Embedded Ruby).

Tests and some concepts are inspired by https://github.com/jwilk/shellcat[shellcat].

== License

This project is licensed under http://opensource.org/licenses/MIT/[MIT License]. For the full text of the license, see the link:LICENSE[LICENSE] file.