Giter Site home page Giter Site logo

gucci's Introduction

Gucci

A simple CLI templating tool written in golang.

GitHub version Build Status

Installation

If you have go installed:

$ go get github.com/noqcks/gucci

Or you can just download the binary and move it into your PATH:

VERSION=1.6.10
wget -q "https://github.com/noqcks/gucci/releases/download/${VERSION}/gucci-v${VERSION}-darwin-amd64"
chmod +x gucci-v${VERSION}-darwin-amd64
mv gucci-v${VERSION}-darwin-amd64 /usr/local/bin/gucci

Use

Locating Templates

gucci can locate a template in multiple ways.

File

Pass the template file path as the first argument:

$ gucci template.tpl > template.out

Stdin

Supply the template through standard input:

$ gucci
Start typing stuff {{ print "here" }}
^d
Start typing stuff here

Via piping:

$ echo '{{ html "<escape-me/>" }}' | gucci

Supplying Variable Inputs

gucci can receive variables for use in templates in the following ways (in order of lowest to highest precedence):

  • A JSON or YAML file
  • Environment variables
  • Variable command options

Variables File

Given an example variables file:

# vars.yaml
hosts:
  - name: bastion
  - name: app

Pass it into gucci with -f or --vars-file:

$ gucci -f vars.yaml template.tpl

Environment Variables

Here, MY_HOST is available to the template:

$ export MY_HOST=localhost
$ gucci template.tpl

Variable Options

Pass variable options into gucci with -s or --set-var, which can be repeated:

$ gucci -s foo.bar=baz template.tpl

Variable option keys are split on the . character, and nested such that the above example would equate to the following yaml variable input:

foo:
  bar: baz

Templating

Options

Existing golang templating options can be used for templating.

If no option is specified, the missingkey=error option will be used (execution stops immediately with an error if a key used in the template is not present in the supplied values).

One might want a different value for missingkey when using conditionals and having keys that won't be used at all.

For instance, given the following template, containing two docker-compose services service1 and service2:

# template.tpl
version: "3.8"

services:
{{- if .service1 }}
  service1:
    image: {{ .service1.image }}
    restart: "always"
    ports: {{ toYaml .service1.ports | nindent 6}}
{{- end }}
{{- if .service2 }}
  service2:
    image: {{ .service2.image }}
    restart: "unless-stopped"
    ports: {{ toYaml .service2.ports | nindent 6}}
{{- end }}

And imagine a scenario where whe only need service2. By using the following values file:

# values.yaml
service2:
  image: "myservice:latest"
  ports:
    - "80"
    - "443"

And using a different missingkey=error, we can actually get the desired result without having to define the values for service1:

$ gucci -o missingkey=zero -f values.yaml  template.tpl
version: "3.8"

services:
  service2:
    image: myservice:latest
    restart: "unless-stopped"
    ports:
      - "80"
      - "443"

GoLang Functions

All of the existing golang templating functions are available for use.

Sprig Functions

gucci ships with the sprig templating functions library offering a wide variety of template helpers.

Built In Functions

Furthermore, this tool also includes custom functions:

  • shell: For arbitrary shell commands

    {{ shell "echo hello world" }}
    

    and

    # guest: world
    {{ shell "echo hello " .guest }}
    

    Both produce:

    hello world
    
  • toYaml: Print items in YAML format

    {{ $myList := list "a" "b" "c" }}
    {{ toYaml $myList }}
    

    Produces:

    - a
    - b
    - c
    

Example

NOTE: gucci reads and makes available all environment variables.

For example a var $LOCALHOST = 127.0.0.1

gucci template.tpl > template.conf

# template.tpl
{{ .LOCALHOST }}

gucci template.tpl > template.conf -->

# template.conf
127.0.0.1

simple enough!

For an iteration example, you have $BACKENDS=server1.com,server2.com

# template.tpl
{{ range split .BACKENDS "," }}
server {{ . }}
{{ end }}

gucci template.tpl > template.conf -->

# template.conf
server server1.com
server server2.com

Testing

Setup:

go get github.com/noqcks/gucci
go get github.com/onsi/ginkgo/ginkgo
go get github.com/onsi/gomega

Run tests:

ginkgo ./...

gucci's People

Contributors

achaussier avatar fhemberger avatar j-zimnowoda avatar jfranzoi avatar jfranzoi-gucci avatar kir4h avatar mck-iain avatar noqcks avatar oliverisaac avatar pdufault avatar troykinsella avatar ucirello avatar zanerock 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  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gucci's Issues

Feature Request: Sub-templates

Hello,

First of all, thanks for this great project. I'm using it and it's working great.

This is a feature request to allow to include templates in the current template. I guess that all templates could be included (recursively) using a function like {{ template /path/to/template.tpl }} and after all templates are in place (as a one big template) process any other function.

Have a great day!

option to escape templating markers

Hi, in helmfile we are given the option to escape template markers with backtics like so:

{{`{{ function_in_some_other_template_language_using_same_markers }}`}}

Is this possible with gucci? Otherwise we would have to use dirty workarounds that prepare that output into env vars, which is not desired.

[Feature Request] Set optional missing_key value for Template Options

Currently, missing_key template Option is hardcoded (https://github.com/noqcks/gucci/blob/master/gucci.go#L123).

It would be nice if this behaviour can be configured with any of the options available in the Option func (https://golang.org/pkg/text/template/#Template.Option).

In my scenario, I am defining a docker-compose template having a number of services, where each individual service has a set of properties but can be disabled. With current behaviour, I need to declare every single property even if the service is disabled.

I can work on it if you are open to PR (or work on it yourself if you prefer @noqcks )

Thanks!

Feature Request: Allow reading vars/json from stdin

Hi, if i'm not mistaken it is only possible to read template files from stdin but not json data to be used. I'd like to be able to do something like this, to read json from other commands:

curl https://example.com/something.json | gucci template-file.tmpl

Linux release is linked to libc

This prevents portability between distributions. I would like to use Gucci in my Alpine container but the fact is, I have to recompile it. If you're binary distribution is compiled statically, then you're linux-amd64 will work for all of our distro's.

Any chance I can convince you to release future versions of Gucci as statically compiled

This is how I did it:
GOOS=linux GOARCH=amd64 go build -a -tags netgo -ldflags '-w -extldflags "-static"' -o gucci gucci.go funcs.go util.go

Switching to go mod?

For reproducible builds, it would be good to use a go.mod file.

Just run go mod init github.com/noqcks/gucci, go build and commit the go.mod and go.sum files.

Cant install Gucci

I tried to install gucci by following steps from Readme:

VERSION=1.5.5
wget -q https://github.com/noqcks/gucci/releases/download/${VERSION}/gucci-v${VERSION}-darwin-amd64
chmod +x gucci-v${VERSION}-darwin-amd64
mv gucci-v${VERSION}-darwin-amd64 /usr/local/bin/gucci

But wget can't find the binary to download.

Feature request: "shell" template function

It'd be super powerful to be able to execute a shell command and embed the output in the generated document. Something like this:

Latest release: {{shell "cat .version"}}

Rebuild With Newer Version of Go

Go 1.11 added variable re-assignment in templates. As seen in line 8 of the below example, we can append to the list and then use the list outside the range.

The current version produces an error like this: Error parsing template(s): template: tmp.dQEbEZBe9r:1: unexpected "=" in operand

Bumping the go version used to build would fix this issue.

varfile=$( mktemp --suffix json )
echo '{ "nums": [ { "n": "1" }, { "n": "2" }, { "n": "3" } ] }' > "$varfile"

templatefile=$( mktemp )
cat > $templatefile <<'EOF'
{{- $var := list }}
{{- range .nums }}
    {{- $var = append $var .n }}
{{- end }}
{{- join "," $var }}
EOF

gucci -f "$varfile" "$templatefile"

rm "$varfile" "$templatefile"

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.