Coder Social home page Coder Social logo

dominik-k / sigil Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gliderlabs/sigil

0.0 2.0 0.0 64 KB

Standalone string interpolator and template processor

License: BSD 3-Clause "New" or "Revised" License

Makefile 1.83% Go 79.71% Shell 18.47%

sigil's Introduction

Sigil

CircleCI IRC Channel

Standalone string interpolator and template processor

$ echo '${name} is{{ range seq ${count:-3} }} cool{{ end }}!' | sigil -p name=Sigil
Sigil is cool cool cool!

Sigil is a command line tool for template processing and POSIX-compliant variable expansion. It was created for configuration templating, but can be used for any text processing.

Getting Sigil

curl -L "https://github.com/gliderlabs/sigil/releases/download/v0.4.0/sigil_0.4.0_$(uname -sm|tr \  _).tgz" \
    | tar -zxC /usr/local/bin

Other releases can be downloaded from Github Releases.

Using Sigil

Template text can be provided via STDIN or from a file if provided with the -f flag. Any other arguments are key-values in the form <key>=<value>. They are used as variables.

  • echo 'Hello, $name' | sigil -p name=Jeff
  • sigil -p -f config.tmpl var1=foo "var2=Hello world"

Variables

POSIX style

There are two forms of variable syntax in Sigil. The first is POSIX style, which among other features allows default values or enforces required values:

  • $variable - normal POSIX style
  • ${variable:-"default"} - expansion with default value
  • ${variable:?} - fails when not set

Environment variables are also available as POSIX style variables. This makes Sigil great for quick and simple string interpolation.

Template style

The other syntax to use variables is consistent with the rest of the templating syntax. It uses {{ and }} to define template expressions. Variable expansion in this form is simply used as:

  • {{ $variable }}

You can do much more with this syntax, such as modifier pipelines. All of which is explained below.

Custom Delimiters

Sometimes you want to use sigil to generate text, which uses golang templating itself. For example if you want to generate packer configuration your template might contain a lot of {{ and }}.

Intead of replacing all {{ with {{โ€œ{{โ€}}, you can change the delimiters, by setting the SIGIL_DELIMS environment variable. It is the left and right delimiter strings, separated by a coma.

SIGIL_DELIMS={{{,}}}  sigil -i 'hello {{{ $name }}}' name=packer

Functions

There are a number of builtin functions that can be used as modifiers, conditional tests, expansion data sources, and more. There are two references for functions available:

Here are a few examples:

  • {{ $variable | capitalize }}
  • {{ include "file.tmpl" "var1=foo" "var2=bar" }}
  • {{ file "example.txt" | replace "old" "new" }}
  • {{ json "file.json" | pointer "/Widgets/0/Name" }}

Conditionals

  • {{ if expr }} true {{ end }}
  • {{ if expr }} true {{ else }} false {{ end }}
  • {{ if expr }} true {{ else if expr }} also true {{ end }}

Loops / Iteration

  • {{ range expr }} element: {{.}} {{ end }}
  • {{ range expr }} elements {{ else }} no elements {{ end }}

Full Syntax

Lots more is possible with this template syntax. Sigil is based on Go's text/template package. You can read full documentation there.

License

BSD

sigil's People

Contributors

josegonzalez avatar lalyos avatar mozamimy avatar progrium avatar stormcat24 avatar thenathanblack avatar

Watchers

 avatar  avatar

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.