Skip to content

Commit

Permalink
Add override files and ENV target
Browse files Browse the repository at this point in the history
Write test for new approach

update travis file

Fix test command

Remove 1.10

Enable go modules for tests

Test overriding and add env var rather then setting go modules in scripts

Remove go 1.11 since go mod is version specific
  • Loading branch information
quantumew committed Apr 12, 2019
1 parent 0a94e18 commit 446f303
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 139 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ _testmain.go

# Binary
mustache

# deps
vendor
14 changes: 7 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
language: go
go:
- 1.6
- 1.5
- 1.4
- 1.3
- 1.2
- 1.12
- tip
install: make get-deps
script: go test ./...
env:
- GO111MODULE=on
install: true
script:
- go build
- go test
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ $(BINARY): $(SOURCE)
strip mustache

get-deps:
go get -d
go get -d "github.com/onsi/gomega"
go get -d "github.com/onsi/ginkgo"
go mod vendor

clean:
if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi

dist-clean:
rm -r mustache lib

test:
go test
32 changes: 20 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Mustache Cli
============
# Mustache Cli

[![Build Status](https://travis-ci.org/quantumew/mustache-cli.svg?branch=master)](https://travis-ci.org/quantumew/mustache-cli)

Expand All @@ -11,27 +10,36 @@ See examples directory for a more in depth example of using JSON, YAML, and stdi

[Build of latest release.](https://github.com/quantumew/mustache-cli/releases)

Usage:
## Usage

mustache [<data-file>] <template-path>
mustache <template-path>

Options:
## Examples

-h --help - Show this message.
# Basic template usage
mustache data.json template.mustache

Arguments:
<data-file> - Path to data file.
# Pull variables from environment
mustache ENV template.mustache

<template-path> - Path to template file.
# Pull variables from environment with overrides. This will merge starting with env vars.
# Think of order as priority.
mustache ENV template.mustache --override data.json --override data1.json

Examples:
# get base data from stdin
cat data-source.json | mustache template.mustache

mustache data-source.json template.mustache
## Arguments

mustache data-source.yaml template.mustache
<data-file> Path to data file. ENV is a special identifier to use environment variables.

cat data-source.json | mustache template.mustache
<template-path> Path to template file.

# Options

-h --help Show help message.
-o --override <file> Override data files. Overrides will be done in order.

See also: [EXAMPLES](examples/README.md)

Expand Down
3 changes: 3 additions & 0 deletions examples/data-source-override.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"OVERRIDE": "yes"
}
5 changes: 5 additions & 0 deletions examples/run-example-override
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
# Example of running mustache with a JSON data source.
# NOTE: This script is used as part of the mustache-cli unit tests.
cd "${0%/*}"
ENV_VAR=say OVERRIDE=ignoreme ../mustache.go data-source.json template-override.mustache --override data-source-override.json --override ENV
9 changes: 9 additions & 0 deletions examples/template-override.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Hello Earthlings,
my name is {{alienName}} but you can call me {{humanName}}.
I come from {{planet}} in the {{galaxy}} galaxy.
I would like to {{#actions}}{{action}}{{#comma}}, {{/comma}}{{/actions}}.

P.S. {{ENV_VAR}} {{OVERRIDE}}

Thank you,
{{alienName}}
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/quantumew/mustache-cli

go 1.12

require (
github.com/cbroglie/mustache v1.0.1
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
github.com/ghodss/yaml v1.0.0
github.com/onsi/ginkgo v1.8.0
github.com/onsi/gomega v1.5.0
)
33 changes: 33 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
github.com/cbroglie/mustache v1.0.1 h1:ivMg8MguXq/rrz2eu3tw6g3b16+PQhoTn6EZAhst2mw=
github.com/cbroglie/mustache v1.0.1/go.mod h1:R/RUa+SobQ14qkP4jtx5Vke5sDytONDQXNLPY/PO69g=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.8.0 h1:VkHVNpR4iVnU8XQR6DBm8BqYjN7CRzw+xKUbVVbbW9w=
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
156 changes: 102 additions & 54 deletions mustache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,84 +3,132 @@ package main

import (
"fmt"
"github.com/cbroglie/mustache"
"github.com/docopt/docopt-go"
"github.com/ghodss/yaml"
"io/ioutil"
"log"
"os"
"strings"

"github.com/cbroglie/mustache"
"github.com/docopt/docopt-go"
"github.com/ghodss/yaml"
)

var logger = log.New(os.Stderr, "", 0)

func main() {
doc := `Mustache Cli
doc := `Mustache Cli
Command line interface for rendering mustache templates.
Data is either expected via data option with a file name or
via stdin. If data option is given that will be used.
via stdin. If data option is given that will be used.
Examples:
# Basic template usage
mustache data.json template.mustache
# Pull variables from environment
mustache ENV template.mustache
# Pull variables from environment with overrides. This will merge starting with env vars.
# Think of order as priority.
mustache ENV template.mustache --override data.json --override data1.json
# get base data from stdin
cat data-source.json | mustache template.mustache
Usage:
mustache [<data-file>] <template-path>
mustache <template-path>
mustache [<data-file>] <template-path> [--override=<data-file>]...
mustache <template-path> [--override=<data-file>]...
Options:
-h --help Show this message.
Arguments:
<data-file> Path to data file.
<template-path> Path to template file.
`
arguments, _ := docopt.Parse(doc, nil, true, "Mustache 0.1", false)
dataPath := arguments["<data-file>"]
templatePath := arguments["<template-path>"].(string)

var (
err error
data interface{}
)

if dataPath == nil {
data, err = loadFromStdin()
} else {
path := dataPath.(string)
data, err = loadFromFile(path)
}

if err != nil {
logError("Error occurred loading data", err)
os.Exit(1)
}

output, err := mustache.RenderFile(templatePath, data)

if err != nil {
logError("Error occurred rendering template", err)
os.Exit(1)
}

fmt.Println(output)
<data-file> Path to data file. ENV is a special identifier to use environment variables.
<template-path> Path to template file.
Options:
-h --help Show help message.
-o --override <file> Override data files. Overrides will be done in order.
`

arguments, _ := docopt.Parse(doc, nil, true, "Mustache 1.0.0", false)
dataPath := arguments["<data-file>"]
templatePath := arguments["<template-path>"].(string)
overrideList := arguments["--override"].([]string)

var (
err error
data interface{}
context = make([]interface{}, 1+len(overrideList))
)

if dataPath == nil {
data, err = loadFromStdin()
} else {
path := dataPath.(string)
data = loadFromEnvOrFile(path)
}
context[0] = data

for i, override := range overrideList {
newData := loadFromEnvOrFile(override)
checkErr(err)
context[i+1] = newData
}

checkErr(err)
output, err := mustache.RenderFile(templatePath, context...)
checkErr(err)
fmt.Println(output)
}

func checkErr(err error) {
if err != nil {
logError("Error occurred rendering template", err)
os.Exit(1)
}
}

func loadFromEnvOrFile(path string) interface{} {
if path == "ENV" {
return loadFromEnv()
}

d, err := loadFromFile(path)
checkErr(err)
return d
}

func loadFromEnv() interface{} {
m := map[string]string{}

for _, item := range os.Environ() {
splits := strings.Split(item, "=")
m[splits[0]] = os.Getenv(splits[0])
}

return m
}

func loadFromFile(path string) (interface{}, error) {
raw, readErr := ioutil.ReadFile(path)
raw, readErr := ioutil.ReadFile(path)

if readErr != nil {
return nil, readErr
}
if readErr != nil {
return nil, readErr
}

return decodeData(raw)
return decodeData(raw)
}

func loadFromStdin() (interface{}, error) {
raw, readErr := ioutil.ReadAll(os.Stdin)
raw, readErr := ioutil.ReadAll(os.Stdin)

if readErr != nil {
return nil, readErr
}
if readErr != nil {
return nil, readErr
}

return decodeData(raw)
return decodeData(raw)
}

func decodeData(raw []byte) (interface{}, error) {
Expand All @@ -91,6 +139,6 @@ func decodeData(raw []byte) (interface{}, error) {
}

func logError(msg string, err error) {
logger.Println(msg)
logger.Println(err.Error())
logger.Println(msg)
logger.Println(err.Error())
}
Loading

0 comments on commit 446f303

Please sign in to comment.