From 55e76f2915fa8596dd5606f89f6531f395d4a09d Mon Sep 17 00:00:00 2001 From: "Subhas Dandapani (RDX)" Date: Mon, 19 Dec 2016 10:48:54 +0100 Subject: [PATCH] Add text/template (#29) * Add text/template * Simplify templating command * Update command --- .travis.yml | 3 ++- Godeps/Godeps.json | 18 +++++++++++++---- README.md | 6 +++--- bin/cross-compile.sh | 12 +++++++---- cmd/license.go | 6 ------ cmd/list.go | 6 ------ cmd/root.go | 14 +++++++++++-- cmd/run.go | 6 ------ cmd/template.go | 8 +------- core/bindata.go | 25 ++++++++++++++++++++++- core/template.go | 37 +++++++++++++++++++--------------- core/util.go | 11 ++++++++++ examples/template/myke.yml | 4 ++-- examples/template/template.tpl | 4 ++-- main.go | 18 +++++++++-------- 15 files changed, 110 insertions(+), 68 deletions(-) diff --git a/.travis.yml b/.travis.yml index f87f297..57931ba 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,8 @@ install: - godep restore -v - godep get -v github.com/onsi/ginkgo/ginkgo script: -- bin/cross-compile.sh +- ginkgo -r -v --trace --keepGoing +- bin/cross-compile.sh "$TRAVIS_TAG-$TRAVIS_COMMIT" deploy: provider: releases skip_cleanup: true diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 2c94488..79bc330 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -1,5 +1,5 @@ { - "ImportPath": "myke", + "ImportPath": "github.com/goeuro/myke", "GoVersion": "go1.7", "GodepVersion": "v75", "Packages": [ @@ -7,9 +7,14 @@ ], "Deps": [ { - "ImportPath": "github.com/flosch/pongo2", - "Comment": "v1.0-rc1-211-g22499f9", - "Rev": "22499f94d3d56c7238591344a6902715d0b8fbbf" + "ImportPath": "github.com/Masterminds/sprig", + "Comment": "2.7.0-4-g69011c0", + "Rev": "69011c0cd9b4d2e0733c4d9e2c8e2a5a0d0a2f2f" + }, + { + "ImportPath": "github.com/aokoli/goutils", + "Comment": "1.0.0-1-g9c37978", + "Rev": "9c37978a95bd5c709a15883b6242714ea6709e64" }, { "ImportPath": "github.com/ghodss/yaml", @@ -164,6 +169,11 @@ "Comment": "v1.0-133-g16f4cfd", "Rev": "16f4cfdc64db0987ed3e7baf813c13bdc253b24b" }, + { + "ImportPath": "github.com/satori/go.uuid", + "Comment": "v1.1.0-6-gb061729", + "Rev": "b061729afc07e77a8aa4fad0a2fd840958f1942a" + }, { "ImportPath": "github.com/tidwall/gjson", "Rev": "7afd24f7a27574c6a07a012c2bdbdb80b494dc20" diff --git a/README.md b/README.md index be222b5..49bdd60 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ You can pass task parameters like: * Environment variables can be defined in the yml or included from dotenv files, and can be overridden from the shell * Runtime parameters like `myke ...task[key1=val1, key2=val2, ...]` * One YML can mixin another YML, acquiring all tasks, env, env files, PATH defaults, etc, and can override all of them -* Built-in templating using [Pongo2](https://github.com/flosch/pongo2) for tasks as well as custom files, because templating is a basic necessity +* Built-in templating using golang text/template and 50+ functions provided by [sprig](https://github.com/Masterminds/sprig) * Other commands can run other tasks in `before/after` hooks, and they are chained with mixins ## Installation @@ -62,8 +62,8 @@ Explore the self documenting `examples` folder. * So, if you mixin `/myke.yml`, then that yml's `cwd/bin` is also added to the PATH, that yml's env/env_files/env_files.local are also loaded, and so on * shell exported environment variables take precedence * additional variables: `MYKE_PROJECT`, `MYKE_TASK`, `MYKE_CWD` are set -* command is templated using [pongo2](https://github.com/flosch/pongo2) - * `env` and `args` are passed as variables +* command is templated using golang text/template and sprig + * environment and task arguments are passed in as variables * command is run using `sh -exc` ## FAQs diff --git a/bin/cross-compile.sh b/bin/cross-compile.sh index 72ff2aa..ad0ad79 100755 --- a/bin/cross-compile.sh +++ b/bin/cross-compile.sh @@ -1,13 +1,17 @@ #!/usr/bin/env bash +#Usage: build.sh set -ex -# Run tests -ginkgo -r -v --trace --keepGoing +# Prepare tmp folder +rm -rf tmp +mkdir -p tmp + +# Insert version +version=${1?"version is required"} +echo $version >> "tmp/version" # Generate license notices deps="github.com/goeuro/myke $(go list -f '{{ join .Deps "\n"}}' . | grep -v 'goeuro/myke')" -rm -rf tmp -mkdir -p tmp out="tmp/LICENSES" echo -e "OPEN SOURCE LICENSES\n" > $out diff --git a/cmd/license.go b/cmd/license.go index e65e72e..c29f275 100644 --- a/cmd/license.go +++ b/cmd/license.go @@ -7,12 +7,6 @@ import ( "fmt" ) -var LicenseCmd = cli.Command{ - Name: "license", - Usage: "prints licenses", - Action: License, -} - func License(c *cli.Context) error { data, err := core.Asset("tmp/LICENSES") if err != nil { diff --git a/cmd/list.go b/cmd/list.go index ac2f8d3..2547423 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -8,12 +8,6 @@ import ( "strings" ) -var ListCmd = cli.Command{ - Name: "list", - Usage: "list available tasks", - Action: List, -} - func List(c *cli.Context) error { w := loadWorkspace(c.String("file")) table := tablewriter.NewWriter(os.Stdout) diff --git a/cmd/root.go b/cmd/root.go index cd938b9..0776be0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,17 +4,27 @@ import ( "gopkg.in/urfave/cli.v1" "github.com/goeuro/myke/core" "path/filepath" + "strings" "os" ) -func RunOrList(c *cli.Context) error { - if c.NArg() > 0 { +func Action(c *cli.Context) error { + if len(c.String("template")) > 0 { + return Template(c) + } else if c.Bool("license") { + return License(c) + } else if c.NArg() > 0 { return Run(c) } else { return List(c) } } +func Version() string { + version, _ := core.Asset("tmp/version") + return strings.TrimSpace(string(version)) +} + func loadWorkspace(path string) core.Workspace { if !filepath.IsAbs(path) { cwd, _ := os.Getwd() diff --git a/cmd/run.go b/cmd/run.go index 776cbfd..4988f56 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -6,12 +6,6 @@ import ( "log" ) -var RunCmd = cli.Command{ - Name: "run", - Usage: "query to execute of format tag1/tag2/project/task[arg1=val1,arg2=val2,...]", - Action: Run, -} - func Run(c *cli.Context) error { qs := make([]string, len(c.Args())) for i, v := range c.Args() { diff --git a/cmd/template.go b/cmd/template.go index 0329f05..5ce74d8 100644 --- a/cmd/template.go +++ b/cmd/template.go @@ -8,14 +8,8 @@ import ( "io/ioutil" ) -var TemplateCmd = cli.Command{ - Name: "template", - Usage: "render a template file with environment variables", - Action: Template, -} - func Template(c *cli.Context) error { - bytes, err := ioutil.ReadFile(c.Args().First()) + bytes, err := ioutil.ReadFile(c.String("template")) if err != nil { log.Fatal(err) } diff --git a/core/bindata.go b/core/bindata.go index 10f12eb..ad825bc 100644 --- a/core/bindata.go +++ b/core/bindata.go @@ -1,6 +1,7 @@ // Code generated by go-bindata. // sources: // tmp/LICENSES +// tmp/version // DO NOT EDIT! package core @@ -68,7 +69,7 @@ func (fi bindataFileInfo) Sys() interface{} { return nil } -var _tmpLicenses = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\xec\x9a\x4d\x73\xdb\x38\xd2\xc7\xef\xf8\x14\x5d\x39\x3c\x15\x3f\xc5\x48\x8e\x67\x6a\x0e\x93\x13\x4d\xc1\x12\x26\x32\xa9\x25\x28\x7b\x75\x84\x48\x48\xc4\x84\x04\xb8\x00\x28\x8f\xf6\xd3\x6f\x01\xa2\x24\xda\x56\x76\xc6\x8e\x77\x53\xb5\xd1\xc9\x7c\xc1\x4b\xa3\xd9\xfd\xc3\x1f\x6d\x25\x33\x1c\x03\x4d\xe6\x69\x84\x61\x4a\x22\x1c\x53\x4c\x11\x5a\x0b\x5b\xb6\xcb\x41\xae\xea\xe1\x5a\xf1\x56\xab\x61\xbd\xfd\xc2\x11\x42\xb7\x24\x83\xa9\xc8\xb9\x34\x1c\xa1\x48\x35\x5b\x2d\xd6\xa5\x85\xf7\xf9\x05\x5c\x5d\x7e\xfc\x05\xc6\x0a\xb7\x5a\x41\xa6\xd9\x86\x57\x30\xae\x97\x13\x84\x66\x5c\xd7\xc2\x18\xa1\x24\x08\x03\x25\xd7\x7c\xb9\x85\xb5\x66\xd2\xf2\x22\x80\x95\xe6\x1c\xd4\x0a\xf2\x92\xe9\x35\x0f\xc0\x2a\x60\x72\x0b\x0d\xd7\x46\x49\x50\x4b\xcb\x84\x14\x72\x0d\x0c\x72\xd5\x6c\x91\x5a\x81\x2d\x85\x01\xa3\x56\xf6\x81\x69\x0e\x4c\x16\xc0\x8c\x51\xb9\x60\x96\x17\x50\xa8\xbc\xad\xb9\xb4\xcc\xba\xf9\x56\xa2\xe2\x06\xde\xdb\x92\xc3\x3b\xda\xf5\x78\x77\xe1\x27\x29\x38\xab\x90\x90\xe0\xde\xed\x5f\xc1\x83\xb0\xa5\x6a\x2d\x68\x6e\xac\x16\xb9\x1b\x23\x00\x21\xf3\xaa\x2d\x9c\x0d\xfb\xd7\x95\xa8\x45\x37\x83\xeb\xee\x7d\x60\x90\x55\xd0\x1a\x1e\x78\x3b\x03\xa8\x55\x21\x56\xee\x2f\xf7\xcb\x6a\xda\x65\x25\x4c\x19\x40\x21\xdc\xd0\xcb\xd6\xf2\x00\x8c\x7b\xe8\x9d\x19\xb8\x75\x0c\x95\x06\xc3\xab\x0a\xe5\xaa\x11\xdc\x80\x5f\xeb\xd1\x3a\xdf\xc6\x99\xde\x38\x87\xda\xce\x45\xc6\x3d\x79\x28\x55\xfd\x78\x25\xc2\xa0\x55\xab\xa5\x30\x25\xf7\x7d\x0a\x05\x46\xf9\x19\x7f\xe7\xb9\x75\x4f\x5c\xf3\x95\xaa\x2a\xf5\xe0\x96\x96\x2b\x59\x08\xb7\x22\xf3\x2b\x42\x59\xc9\x81\x2d\xd5\x86\xfb\xb5\xec\x3e\xb1\x54\x56\xe4\x3b\x77\xfb\x0f\xd0\x1c\xbf\x6a\xf7\xca\x94\xac\xaa\x60\xc9\x3b\x87\xf1\x02\x84\x04\xd6\x5b\x8e\x76\xd3\x1b\xcb\xa4\x15\xac\x82\x46\x69\x3f\xdf\xd3\x65\x0e\x10\xca\x26\x18\x68\x72\x93\xdd\x87\x29\x06\x42\x61\x96\x26\x77\x64\x84\x47\xf0\x2e\xa4\x40\xe8\xbb\x00\xee\x49\x36\x49\xe6\x19\xdc\x87\x69\x1a\xc6\xd9\x02\x92\x1b\x08\xe3\x05\x7c\x26\xf1\x28\x00\xfc\xf7\x59\x8a\x29\x85\x24\x45\xe4\x76\x36\x25\x78\x14\x00\x89\xa3\xe9\x7c\x44\xe2\x31\x5c\xcf\x33\x88\x93\x0c\xa6\xe4\x96\x64\x78\x04\x59\x02\x6e\xc2\x6e\x28\x82\xa9\x1b\xec\x16\xa7\xd1\x24\x8c\xb3\xf0\x9a\x4c\x49\xb6\x08\xd0\x0d\xc9\x62\x37\xe6\x4d\x92\x42\x08\xb3\x30\xcd\x48\x34\x9f\x86\x29\xcc\xe6\xe9\x2c\xa1\x18\xc2\x78\x04\x71\x12\x93\xf8\x26\x25\xf1\x18\xdf\xe2\x38\x1b\x00\x89\x21\x4e\x00\xdf\xe1\x38\x03\x3a\x09\xa7\x53\x37\x15\x0a\xe7\xd9\x24\x49\x9d\x7d\x10\x25\xb3\x45\x4a\xc6\x93\x0c\x26\xc9\x74\x84\x53\x0a\xd7\x2e\x11\xc3\xeb\x29\xde\x4d\x15\x2f\x20\x9a\x86\xe4\x36\x80\x51\x78\x1b\x8e\xb1\xef\x95\x64\x13\x9c\x22\xd7\x6c\x67\x1d\xdc\x4f\xb0\x7b\xe4\xe6\x0b\x63\x08\xa3\x8c\x24\xb1\x5b\x46\x94\xc4\x59\x1a\x46\x59\x00\x59\x92\x66\x87\xae\xf7\x84\xe2\x00\xc2\x94\x50\xe7\x90\x9b\x34\xb9\x0d\x90\x73\x67\x72\xe3\x9a\x90\xd8\xf5\x8b\xf1\x6e\x14\xe7\x6a\x78\xf4\x45\x92\xd4\xdf\xcf\x29\x3e\x0c\x08\x23\x1c\x4e\x49\x3c\xa6\xae\xb3\x5b\xe2\xbe\xf1\x00\xa1\x47\x38\x59\x55\xca\xe4\xe5\xb0\x51\x72\xad\xae\xd0\x2e\xd4\x7a\x50\x81\xf7\xb7\x24\xbb\x38\x81\x96\x9f\x3e\x5c\x5d\x7e\xfc\x19\x6e\x2a\xa5\x05\x93\x40\xf3\xb2\x62\x79\x69\xb9\x7e\x5b\xbe\x80\x5a\xa1\xb7\xe3\x0b\x08\x89\xde\x8c\x2f\x60\x15\x7a\x1b\xbe\xc0\x81\x2f\xe8\x1b\xf8\x02\x27\xf8\x82\xce\x7c\x79\x25\x5f\xa0\xe3\x0b\x7a\x03\xbe\xc0\x91\x2f\xe8\x1b\xf8\x02\xcf\xf9\x82\x5e\xcf\x17\xe8\xf3\x05\x7d\x2b\x5f\xe0\x6b\x7c\x59\x97\xaa\x30\x66\xb8\x65\x75\xf5\x02\xba\xfc\x0c\x94\xd5\x30\x76\x7d\xcf\x7a\xe5\xac\x57\xfe\x07\x78\x72\xd6\x2b\xaf\xd7\x2b\xcf\xf0\x70\x05\x2e\xc2\xc6\x0a\xc2\xd6\x96\x4a\x9b\x01\x84\x55\xb5\xdf\x96\x35\x37\x5c\x6f\x78\x31\x40\x28\xe5\x87\x6c\x71\x71\xe6\x62\xaf\x35\x2e\xbe\xc0\xa8\x56\x77\xd1\xb8\x14\x92\xe9\x2d\xac\x94\xae\x4d\xe0\x53\xd4\x45\x5a\x97\xaa\xc8\xa7\xa1\xc8\xd9\x2e\x95\x5d\x66\xec\xb2\xc7\xb1\xa2\xd1\x6a\x23\x5c\x9c\xda\x92\xd9\xaf\xe6\x83\xeb\x84\x6a\x6e\x7f\x45\x08\x00\xfe\x1f\x1e\x1b\xe5\xc3\xb7\xb3\x26\x57\x05\x87\xba\x35\x8e\x1f\x0e\x5b\x7e\xc8\x27\x79\x84\x76\xc9\x12\xec\x92\xa8\x12\xc6\x7a\xf8\xf5\x66\xf3\x09\xd6\x37\xa5\x10\x26\xaf\x98\xa8\xb9\x1e\x9c\xb6\x40\xc8\xbe\x13\xf6\x16\x34\x5a\x15\x6d\xce\x8f\x46\xa0\xa7\xc9\xfc\x3a\x23\xf6\xd0\x7c\x4c\xda\x0e\x5f\xca\x96\x5c\x43\xcd\x2c\xd7\x82\x55\xe6\xe8\x62\xff\x5d\x6c\xc9\x51\xdf\xf4\x6e\x3d\x31\x17\xbe\x9b\x1b\x55\xb2\xda\x6f\x07\x63\xa5\xd6\x15\x07\x22\xf3\x01\x48\x75\x7c\xe7\xfd\x2d\xac\x41\xb9\x92\xbb\x71\x94\x36\x50\xb3\xad\x03\x4f\x6b\x76\xb0\xe3\xb2\x50\xda\x70\x17\x07\x8d\x56\xb5\xb2\x1c\x76\xde\xb0\x06\x0a\xae\xc5\x86\x17\xb0\xd2\xaa\x7e\x22\x4d\xf7\x78\x37\x0d\xcf\x5d\xd0\x40\xa3\x85\x0b\x25\xed\xc2\x45\xf6\x88\xe7\x11\x45\xe8\x69\x46\x5d\x2f\x7c\x76\x3c\x4f\x6c\x07\x08\x9f\x8f\xe4\x7a\x9e\x25\x29\x45\x1d\xcd\xfc\x0b\x97\xe7\x47\x6c\x41\x87\xad\x1e\x94\x7a\x04\x0b\xf6\x08\x43\x47\x84\x05\x7e\xd2\xe7\xdd\x4e\xb0\xcc\xcf\xd7\xc3\x19\x3a\x8d\xb3\x14\xc3\x88\x50\xcf\x1e\x3c\xfa\x9a\x32\x3a\xac\x12\x25\xf7\x31\x4e\x77\x44\x3b\x2e\xf1\x04\xcc\x46\x24\xc5\x8e\x47\x24\x3e\x5e\x45\x64\x84\xe3\x2c\x9c\x06\x88\xce\x70\x44\xc2\xa9\x43\x38\xbe\x9d\x4d\xc3\x74\x11\x74\x63\x52\xfc\xb7\x39\x8e\x33\x12\x4e\x0f\x20\x7c\xff\x27\x1e\x99\xa5\x49\x34\x4f\x3d\x89\x9d\x1b\xe8\xfc\x9a\x66\x24\x9b\x67\x18\xc6\x49\x32\xf2\x7e\xa6\x38\xbd\x23\x11\xa6\x9f\x60\x9a\x50\xef\xac\x39\xc5\x01\x1a\x85\x59\xe8\x27\x9e\xa5\xc9\x0d\xc9\xe8\x27\x77\x7d\x3d\xa7\xc4\xfb\x8c\xc4\x19\x4e\xd3\xf9\xcc\xe1\xf1\x02\x26\xc9\x3d\xbe\xc3\x29\x44\xe1\x9c\xe2\x91\x77\x6e\xe2\x28\xbc\x70\xdb\x58\x92\xfa\xad\xe9\x34\xa9\x8f\x70\xa6\x59\x4a\xa2\xac\xdf\xcc\x31\x36\x49\x33\x74\x5c\x23\xc4\x78\x3c\x25\x63\x1c\x47\xf8\x11\xc7\x2f\x0e\x1c\xf7\xf0\x5f\xc0\x7d\xb8\xd8\x8b\xc3\x0e\xd3\xc8\x5f\xf6\x02\x36\xf0\x1f\x12\xc8\x0d\x84\xa3\x3b\xe2\xcc\xee\x1a\xcf\x12\x4a\x49\x17\x26\xde\x65\xd1\xa4\x73\xf7\x53\x61\xf8\xbb\x2a\xd5\x70\xad\x0a\x65\xb9\xdc\x9c\x82\xfc\x4f\xf0\x9b\x2a\x25\x5c\x33\x6d\x95\x7c\x52\xe9\x7a\x13\x49\x88\x0e\x47\xcc\xd7\x49\x42\xf4\x95\x23\xe6\x4b\x24\x21\xfa\xb3\x23\xe6\x5f\x90\x84\xe8\x45\x47\xcc\x93\x92\x10\xbd\xfe\x88\xd9\x93\x84\xe8\x3f\x26\x09\xd1\x13\x49\x08\xff\x6d\x49\x88\x9e\xb3\xf5\xa5\x92\x10\x7d\xed\x88\xf9\x6f\x25\x21\x7a\xf1\x11\xf3\xa4\x24\x44\xaf\x3e\x62\x3e\x92\x84\xe8\xf5\x47\xcc\x9e\x24\x44\x6f\x70\xc4\xec\xa3\xa4\x66\xd6\xca\xe1\x5a\x7d\xd0\xad\xe4\x0f\xa2\xb0\xe5\x0b\x8e\x9a\xbf\xc0\x82\x99\xb6\x14\x5a\xc1\x2d\xb3\xa6\xad\x95\x55\xe7\x33\xe7\xf9\xcc\x79\x3e\x73\xfe\xd0\x67\xce\x3e\x5f\x54\xc5\xbf\xb4\x5f\x94\xfc\xa2\x86\x96\x2d\x2b\xee\x04\x3d\xd7\x8f\x35\x4b\xd4\xd5\xad\x96\x5b\x48\x5c\x73\xf8\xec\xda\x9f\x41\xf2\x23\x83\x04\x7d\x0f\xa5\x72\x06\xc9\x77\x05\x09\x7a\xaa\x54\xd6\xaa\xf9\xb2\x1e\x08\x39\x6c\xf5\x8a\x6d\xf8\x30\xaf\xc4\x60\xf3\xf1\x2f\xfc\xeb\xfe\x37\xae\x79\xbd\x05\xca\xb8\xfc\x27\xfc\x1f\x44\xbd\x7a\xc5\x19\x2b\x3f\x32\x56\xce\xfa\xe4\xc7\xc3\xca\x09\x7d\xb2\xe7\xca\x96\xd5\xd5\x60\x73\xf5\x48\x8e\x5c\x5d\x7e\xfc\xf8\xc1\x53\x24\x62\x52\x49\x91\xb3\x0a\xa6\xb6\x18\x20\xd4\x11\xa7\x80\x56\x16\x5d\xc9\x34\x6c\x58\x5e\xf2\x3d\x8b\x02\xb8\xe3\xda\x87\xe0\xd5\xe0\xb2\x4b\xf3\xee\xd5\xbb\x8b\x4f\x68\xab\x5a\x5f\x2d\x95\xca\xfa\x5a\xba\x8f\x5b\x87\x04\xe0\x7f\xe4\xbc\xb1\x2e\x3e\x73\x55\x37\x95\x60\x32\xe7\x87\xb2\xed\x7e\xf4\x01\x5a\x74\x03\xec\x28\x04\xfd\x1a\xcc\xa1\x15\x30\xeb\x8b\xe5\x50\x5a\xdb\xfc\x3a\x1c\x3e\x3c\x3c\x0c\x98\xb7\x72\xa0\xf4\x7a\xd8\xe5\xb7\x19\x76\x3f\x90\xfa\x70\x35\xb8\x44\x68\x2e\x2b\x6e\x0c\x68\xfe\x8f\x56\x68\x5e\x38\x25\xc6\x9a\xa6\x12\xb9\x93\x6c\x50\xb1\x07\x97\x2c\x6c\xad\xf9\x2e\x79\x85\xf4\x85\x59\x21\xd7\xc1\x01\x7c\xbd\x4a\x4a\xdf\x41\x7b\xab\x84\x81\x7e\x03\x5f\xb5\xde\xa7\x0f\x5c\x87\x94\xd0\x00\x3d\x49\x22\x82\xbb\x88\x8c\x47\xc4\x7d\x79\xfa\x38\xa9\xba\xc2\x35\xff\xa3\xd1\xce\x76\xa5\x41\x38\xd7\xf1\x62\x80\x28\xe7\x8f\x26\x5f\x75\x45\xec\x43\x71\xb9\x62\x72\xdd\xb2\x35\x87\xb5\xda\x70\xed\x79\x7e\xe4\x87\x2f\xc1\xa3\x23\x53\xcd\xf3\xe5\x0c\x3c\x92\x8e\xb4\xda\x71\xfd\x81\x6b\xee\x31\xb2\xf3\xd2\x58\xf9\x42\x37\x44\xdd\x6b\xb5\x82\x4a\x2c\x5d\xc8\x75\xe4\x2c\x5b\x83\x1c\x1b\x8d\x15\x5e\xec\x6c\x78\xe7\x7b\x5b\x72\xa1\x41\x69\xb1\x16\x92\x55\x3d\xe8\xb9\x6e\xdd\x17\xdc\xfd\x4b\x04\x58\x23\xf2\xc1\x5a\xf9\x6b\xee\xff\xb7\xa2\x0f\xf7\x0d\xd3\xa6\x77\xab\x39\x2b\x7a\xb7\x26\x67\x52\xf6\xee\x77\xca\xfc\x70\xeb\xec\x2c\xfb\x37\x8d\x16\x1b\x66\xb9\x7f\xf6\x6c\xcf\xbd\xfc\x05\x3e\x0b\xed\x96\x41\x45\xad\xa4\xda\x9c\x7f\xca\xf2\xda\x9f\xb2\xa0\x6f\xdb\x66\x9f\xd6\x19\xd1\x79\x9b\x3d\x6f\xb3\xdf\x67\x9b\xfd\x57\x00\x00\x00\xff\xff\x35\xd6\xb4\xd5\x90\x2b\x00\x00") +var _tmpLicenses = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\xec\x9a\xcf\x73\xdb\xba\x11\xc7\xef\xf8\x2b\x76\x72\xe8\xc4\x1d\x46\x4a\xdc\x4e\x0e\xc9\x89\xa6\x60\x09\x2f\x12\xa9\x12\x94\x5d\x1d\x21\x12\x12\xf1\x42\x02\x2c\x00\xca\x4f\xfd\xeb\x3b\x80\x28\x89\xb2\x9d\x3c\x27\x4d\x9b\x43\x74\x8a\x48\xe2\xc7\xee\x72\xf7\x83\x2f\xd7\x49\xe6\x38\x06\x9a\x2c\xd2\x08\xc3\x94\x44\x38\xa6\x98\x22\xb4\x11\xb6\x6c\x57\x83\x5c\xd5\xc3\x8d\xe2\xad\x56\xc3\x7a\xf7\x99\x23\x84\x66\x24\x83\xa9\xc8\xb9\x34\x1c\xa1\x48\x35\x3b\x2d\x36\xa5\x85\xd7\xf9\x15\x5c\xbf\x7d\xf7\x1e\xc6\x0a\xb7\x5a\x41\xa6\xd9\x96\x57\x30\xae\x57\x13\x84\xe6\x5c\xd7\xc2\x18\xa1\x24\x08\x03\x25\xd7\x7c\xb5\x83\x8d\x66\xd2\xf2\x22\x80\xb5\xe6\x1c\xd4\x1a\xf2\x92\xe9\x0d\x0f\xc0\x2a\x60\x72\x07\x0d\xd7\x46\x49\x50\x2b\xcb\x84\x14\x72\x03\x0c\x72\xd5\xec\x90\x5a\x83\x2d\x85\x01\xa3\xd6\xf6\x81\x69\x0e\x4c\x16\xc0\x8c\x51\xb9\x60\x96\x17\x50\xa8\xbc\xad\xb9\xb4\xcc\xba\xfd\xd6\xa2\xe2\x06\x5e\xdb\x92\xc3\x2b\xda\xcd\x78\x75\xe5\x37\x29\x38\xab\x90\x90\xe0\x9e\x1d\x1e\xc1\x83\xb0\xa5\x6a\x2d\x68\x6e\xac\x16\xb9\x5b\x23\x00\x21\xf3\xaa\x2d\x9c\x0d\x87\xc7\x95\xa8\x45\xb7\x83\x9b\xee\x63\x60\x90\x55\xd0\x1a\x1e\x78\x3b\x03\xa8\x55\x21\xd6\xee\x5f\xee\xdd\x6a\xda\x55\x25\x4c\x19\x40\x21\xdc\xd2\xab\xd6\xf2\x00\x8c\xbb\xe9\x83\x19\x38\x3f\x86\x4a\x83\xe1\x55\x85\x72\xd5\x08\x6e\xc0\xfb\x7a\xb2\xce\x8f\x71\xa6\x37\x2e\xa0\xb6\x0b\x91\x71\x77\x1e\x4a\x55\x9f\x7b\x22\x0c\x5a\xb7\x5a\x0a\x53\x72\x3f\xa7\x50\x60\x94\xdf\xf1\x77\x9e\x5b\x77\xc7\x0d\x5f\xab\xaa\x52\x0f\xce\xb5\x5c\xc9\x42\x38\x8f\xcc\x07\x84\xb2\x92\x03\x5b\xa9\x2d\xf7\xbe\xec\x5f\xb1\x54\x56\xe4\xfb\x70\xfb\x17\xd0\x9c\xde\x6a\xf7\xc8\x94\xac\xaa\x60\xc5\xbb\x80\xf1\x02\x84\x04\xd6\x73\x47\xbb\xed\x8d\x65\xd2\x0a\x56\x41\xa3\xb4\xdf\xef\xb1\x9b\x03\x84\xb2\x09\x06\x9a\xdc\x66\xf7\x61\x8a\x81\x50\x98\xa7\xc9\x1d\x19\xe1\x11\xbc\x0a\x29\x10\xfa\x2a\x80\x7b\x92\x4d\x92\x45\x06\xf7\x61\x9a\x86\x71\xb6\x84\xe4\x16\xc2\x78\x09\x9f\x48\x3c\x0a\x00\xff\x73\x9e\x62\x4a\x21\x49\x11\x99\xcd\xa7\x04\x8f\x02\x20\x71\x34\x5d\x8c\x48\x3c\x86\x9b\x45\x06\x71\x92\xc1\x94\xcc\x48\x86\x47\x90\x25\xe0\x36\xec\x96\x22\x98\xba\xc5\x66\x38\x8d\x26\x61\x9c\x85\x37\x64\x4a\xb2\x65\x80\x6e\x49\x16\xbb\x35\x6f\x93\x14\x42\x98\x87\x69\x46\xa2\xc5\x34\x4c\x61\xbe\x48\xe7\x09\xc5\x10\xc6\x23\x88\x93\x98\xc4\xb7\x29\x89\xc7\x78\x86\xe3\x6c\x00\x24\x86\x38\x01\x7c\x87\xe3\x0c\xe8\x24\x9c\x4e\xdd\x56\x28\x5c\x64\x93\x24\x75\xf6\x41\x94\xcc\x97\x29\x19\x4f\x32\x98\x24\xd3\x11\x4e\x29\xdc\xb8\x42\x0c\x6f\xa6\x78\xbf\x55\xbc\x84\x68\x1a\x92\x59\x00\xa3\x70\x16\x8e\xb1\x9f\x95\x64\x13\x9c\x22\x37\x6c\x6f\x1d\xdc\x4f\xb0\xbb\xe5\xf6\x0b\x63\x08\xa3\x8c\x24\xb1\x73\x23\x4a\xe2\x2c\x0d\xa3\x2c\x80\x2c\x49\xb3\xe3\xd4\x7b\x42\x71\x00\x61\x4a\xa8\x0b\xc8\x6d\x9a\xcc\x02\xe4\xc2\x99\xdc\xba\x21\x24\x76\xf3\x62\xbc\x5f\xc5\x85\x1a\xce\xde\x48\x92\xfa\xeb\x05\xc5\xc7\x05\x61\x84\xc3\x29\x89\xc7\xd4\x4d\x76\x2e\x1e\x06\x0f\x10\x3a\xc7\x49\xa9\x0a\x63\x86\x3b\x56\x57\x68\x9f\x68\x3d\xa4\xc0\xeb\x19\xc9\xae\x9e\x01\xcb\xdf\x81\xb2\x1a\xc6\x6e\xee\x85\x27\x17\x9e\x5c\x78\xf2\x4b\xf3\xe4\x09\x1e\xae\xc1\x65\xd8\x58\x41\xd8\xda\x52\x69\x33\x80\xb0\xaa\xba\x32\x72\xb5\xc7\xf5\x96\x17\x03\x84\x52\x7e\xac\x16\x97\x67\x2e\xf7\x5a\xe3\xf2\x0b\x8c\x6a\x75\x97\x8d\x2b\x21\x99\xde\xc1\x5a\xe9\xda\x04\xbe\x44\x5d\xa6\x75\xa5\x8a\x7c\x19\x8a\x9c\xed\x4b\xd9\x55\xc6\xbe\x7a\x1c\x2b\x1a\xad\xb6\xc2\xe5\xa9\x2d\x99\xfd\x62\x3d\xb8\x49\xa8\xe6\xf6\x03\x42\x00\xf0\x57\x38\x37\xca\xa7\x6f\x67\x4d\xae\x0a\x0e\x75\x6b\x1c\x3f\x1c\xb6\xfc\x92\x8f\xea\x08\xed\x8b\x25\xd8\x17\x51\x25\x8c\xf5\xf0\xeb\xed\xe6\x0b\xac\x6f\x4a\x21\x4c\x5e\x31\x51\x73\x3d\x78\xde\x02\x21\xfb\x41\x38\x58\xd0\x68\x55\xb4\x39\x3f\x19\x81\x1e\x17\xf3\xf7\x19\x71\x80\xe6\x39\x69\x3b\x7c\x29\x5b\x72\x0d\x35\xb3\x5c\x0b\x56\x99\x53\x88\xfd\x7b\xb1\x25\x47\x7d\xd3\x3b\x7f\x62\x2e\xfc\x34\xb7\xaa\x64\xb5\x3f\x0e\xc6\x4a\x6d\x2a\x0e\x44\xe6\x03\x90\xea\xf4\xcc\xc7\x5b\x58\x83\x72\x25\xf7\xeb\x28\x6d\xa0\x66\x3b\x07\x9e\xd6\xec\x61\xc7\x65\xa1\xb4\xe1\x2e\x0f\x1a\xad\x6a\x65\x39\xec\xa3\x61\x0d\x14\x5c\x8b\x2d\x2f\x60\xad\x55\x8d\xce\x8f\x92\x03\xde\x4d\xc3\x73\x97\x34\xd0\x68\xe1\x52\x49\xbb\x74\x91\x3d\xe2\x79\x44\x11\xfa\x3c\xa3\x6e\x96\xbe\x3a\x9e\x16\xb6\x03\x84\xaf\x47\x72\xb3\xc8\x92\x94\xa2\x8e\x66\xfe\x81\xab\xf3\x13\xb6\xa0\xc3\x56\x0f\x4a\x3d\x82\x05\x07\x84\xa1\x13\xc2\x02\xbf\xe9\xd3\x69\xcf\xb0\xcc\xef\xd7\xc3\x19\x7a\x1e\x67\x29\x86\x11\xa1\x9e\x3d\x78\xf4\x05\x92\x9d\xbc\x44\xc9\x7d\x8c\xd3\x3d\xd1\x4e\x2e\x3e\x03\xb3\x11\x49\xb1\xe3\x11\x89\x4f\xbf\x22\x32\xc2\x71\x16\x4e\x03\x44\xe7\x38\x22\xe1\xd4\x21\x1c\xcf\xe6\xd3\x30\x5d\x06\xdd\x9a\x14\xff\x63\x81\xe3\x8c\x84\xd3\x23\x08\x5f\xff\x49\x44\xe6\x69\x12\x2d\x52\x4f\x62\x17\x06\xba\xb8\xa1\x19\xc9\x16\x19\x86\x71\x92\x8c\x7c\x9c\x29\x4e\xef\x48\x84\xe9\x47\x98\x26\xd4\x07\x6b\x41\x71\x80\x46\x61\x16\xfa\x8d\xe7\x69\x72\x4b\x32\xfa\xd1\xfd\xbe\x59\x50\xe2\x63\x46\xe2\x0c\xa7\xe9\x62\xee\xf0\x78\x05\x93\xe4\x1e\xdf\xe1\x14\xa2\x70\x41\xf1\xc8\x07\x37\x71\x14\x5e\xba\x63\x2c\x49\xfd\xd1\xf4\x3c\xa9\x4f\x70\xa6\x59\x4a\xa2\xac\x3f\xcc\x31\x36\x49\x33\x74\xf2\x11\x62\x3c\x9e\x92\x31\x8e\x23\x7c\xc6\xf1\xab\x23\xc7\x3d\xfc\x97\x70\x1f\x2e\xa1\x83\x79\x87\x69\xe4\x7f\xf6\x12\x36\xf0\x2f\x12\xc8\x2d\x84\xa3\x3b\xe2\xcc\xee\x06\xcf\x13\x4a\x49\x97\x26\x3e\x64\xd1\xa4\x0b\xf7\x63\x61\xf8\xbb\x2a\xd5\x70\xa3\x0a\x65\xb9\xdc\x3e\x07\xf9\xbf\xc1\x6f\xaa\x94\x70\xc3\xb4\x55\xf2\xd1\x97\xe8\x0f\x91\x84\x68\x2f\x09\xe1\x7b\x25\x21\x7a\x4e\x12\xc2\xb7\x49\x42\xf4\x55\x49\x08\x2f\x92\x84\xe8\x05\x92\x10\xbe\x2e\x09\xd1\xcb\x24\x21\x7c\x5d\x12\xa2\xff\x99\x24\x44\x8f\x24\x21\xfc\xbf\x25\x21\x7a\xca\xd6\x6f\x95\x84\xe8\xb1\x24\x84\x97\x48\x42\xf4\x32\x49\x08\x7f\x22\x09\xd1\xcb\x24\xe1\x17\x40\x73\x94\x84\xe8\x9b\x24\x21\x7c\x41\x12\xa2\xef\x92\x84\x70\xf6\x89\xd9\x47\x49\xcd\xac\x95\xc3\x8d\x7a\xa3\x5b\xc9\x1f\x44\x61\xcb\x6f\xf8\xd4\x7c\x0f\x4b\x66\xda\x52\x68\x05\x33\x66\x4d\x5b\x2b\xab\x2e\xdf\x9c\x97\x6f\xce\xcb\x37\xe7\x2f\xfd\xcd\xd9\xe7\x8b\xaa\xf8\xe7\xf6\xb3\x92\x9f\xd5\xd0\xb2\x55\xc5\x9d\xa0\xe7\xfa\x5c\xb3\x44\x5d\xdf\x6a\xb5\x83\xc4\x0d\x87\x4f\x6e\xfc\x05\x24\xbf\x32\x48\xd0\xcf\x50\x2a\x17\x90\xfc\x54\x90\xa0\xc7\x4a\x65\xa3\x9a\xcf\x9b\x81\x90\xc3\x56\xaf\xd9\x96\x0f\xf3\x4a\x0c\xb6\xef\x5e\xf0\xa7\xb5\xdf\xb8\xe6\xf5\x0e\x28\xe3\xf2\xdf\xf0\x17\x88\x7a\xfd\x8a\x0b\x56\x7e\x65\xac\x5c\xf4\xc9\xaf\x87\x95\x67\xf4\xc9\x81\x2b\x3b\x56\x57\x83\xed\xf5\x99\x1c\xb9\x7e\xfb\xee\xdd\x1b\x4f\x91\x88\x49\x25\x45\xce\x2a\x98\xda\x62\x80\x50\x47\x9c\x02\x5a\x59\x74\x2d\xd3\xb0\x61\x79\xc9\x0f\x2c\x0a\xe0\x8e\x6b\x9f\x82\xd7\x83\xb7\x5d\x99\x77\x8f\x5e\x5d\x7d\x44\x3b\xd5\xfa\x6e\xa9\x54\xd6\xf7\xd2\x7d\xde\x3a\x24\x00\xff\x23\xe7\x8d\x75\xf9\x99\xab\xba\xa9\x04\x93\x39\x3f\xb6\x6d\x0f\xab\x0f\xd0\xb2\x5b\x60\x4f\x21\xe8\xf7\x60\x8e\xa3\x80\x59\xdf\x2c\x87\xd2\xda\xe6\xc3\x70\xf8\xf0\xf0\x30\x60\xde\xca\x81\xd2\x9b\x61\x57\xdf\x66\xd8\xfd\x07\x86\x37\xd7\x83\xb7\x08\x2d\x64\xc5\x8d\x01\xcd\xff\xd5\x0a\xcd\x0b\xa7\xc4\x58\xd3\x54\x22\x77\x92\x0d\x2a\xf6\xe0\x8a\x85\x6d\x34\xdf\x17\xaf\x90\xbe\x31\x2b\xe4\x26\x38\x82\xaf\xd7\x49\xe9\x07\xe8\x60\x95\x30\xd0\x1f\xe0\xbb\xd6\x87\xf2\x81\x9b\x90\x12\x1a\xa0\x47\x45\x44\x70\x97\x91\xf1\x88\xb8\x37\x4f\xcf\x8b\xaa\x6b\x5c\xf3\x3f\x1a\xed\x6c\x57\x1a\x84\x0b\x1d\x2f\x06\x88\x72\x7e\xb6\xf9\xba\x6b\x62\x1f\x9b\xcb\x15\x93\x9b\x96\x6d\x38\x6c\xd4\x96\x6b\xcf\xf3\x13\x3f\x7c\x0b\x1e\x9d\x98\x6a\x9e\xba\x33\xf0\x48\x3a\xd1\x6a\xcf\xf5\x07\xae\xb9\xc7\xc8\x3e\x4a\x63\xe5\x1b\xdd\x10\x75\x8f\xd5\x1a\x2a\xb1\x72\x29\xd7\x91\xb3\x6c\x0d\x72\x6c\x34\x56\x78\xb1\xb3\xe5\x5d\xec\x6d\xc9\x85\x06\xa5\xc5\x46\x48\x56\xf5\xa0\xe7\xa6\x75\x6f\x70\xff\x27\x11\x60\x8d\xc8\x07\x1b\xe5\x7f\x73\xff\xb7\x15\x7d\xbc\x6e\x98\x36\xbd\x4b\xcd\x59\xd1\xbb\x34\x39\x93\xb2\x77\xbd\x57\xe6\xc7\x4b\x67\x67\xd9\xbf\x68\xb4\xd8\x32\xcb\xfd\xbd\x27\x67\xee\xdb\xf7\xf0\x49\x68\xe7\x06\x15\xb5\x92\x6a\xfb\x63\x8f\x59\x50\x6b\xf4\xe3\x8e\x59\x27\x2f\x7f\xd8\x31\x0b\x56\xa1\x1f\x73\xcc\x76\x72\x17\xfd\x77\xc7\xec\xe3\x3e\x23\xba\x1c\xb3\x97\x63\xf6\xe7\x1c\xb3\xff\x09\x00\x00\xff\xff\x31\x48\xf0\x71\x30\x27\x00\x00") func tmpLicensesBytes() ([]byte, error) { return bindataRead( @@ -88,6 +89,26 @@ func tmpLicenses() (*asset, error) { return a, nil } +var _tmpVersion = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\xca\x4d\x2c\x2e\x49\x2d\xe2\x02\x04\x00\x00\xff\xff\xd1\x78\x4a\x5d\x07\x00\x00\x00") + +func tmpVersionBytes() ([]byte, error) { + return bindataRead( + _tmpVersion, + "tmp/version", + ) +} + +func tmpVersion() (*asset, error) { + bytes, err := tmpVersionBytes() + if err != nil { + return nil, err + } + + info := bindataFileInfo{name: "tmp/version", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)} + a := &asset{bytes: bytes, info: info} + return a, nil +} + // Asset loads and returns the asset for the given name. // It returns an error if the asset could not be found or // could not be loaded. @@ -141,6 +162,7 @@ func AssetNames() []string { // _bindata is a table, holding each asset generator, mapped to its name. var _bindata = map[string]func() (*asset, error){ "tmp/LICENSES": tmpLicenses, + "tmp/version": tmpVersion, } // AssetDir returns the file names below a certain @@ -185,6 +207,7 @@ type bintree struct { var _bintree = &bintree{nil, map[string]*bintree{ "tmp": &bintree{nil, map[string]*bintree{ "LICENSES": &bintree{tmpLicenses, map[string]*bintree{}}, + "version": &bintree{tmpVersion, map[string]*bintree{}}, }}, }} diff --git a/core/template.go b/core/template.go index ef564b1..068c4fd 100644 --- a/core/template.go +++ b/core/template.go @@ -1,32 +1,37 @@ package core import ( - "github.com/flosch/pongo2" - "strings" + "github.com/Masterminds/sprig" + "text/template" + "errors" + "bytes" + "fmt" ) func RenderTemplate(tmpl string, env map[string]string, args map[string]string) (string, error) { - tpl, err := pongo2.FromString(tmpl) - if err != nil { - return "", err + w := new(bytes.Buffer) + params := union(env, args) + funcs := template.FuncMap{ + "required": templateRequired, } - out, err := tpl.Execute(pongo2.Context{"env":env, "args":args}) + tpl, err := template.New("test"). + Funcs(sprig.TxtFuncMap()). + Funcs(funcs). + Option("missingkey=zero"). + Parse(tmpl) if err != nil { return "", err - } else { - return out, nil } + + err = tpl.Execute(w, params) + return w.String(), err } -func filterRequired(in *pongo2.Value, param *pongo2.Value) (out *pongo2.Value, err *pongo2.Error) { - if len(strings.TrimSpace(in.String())) == 0 { - return in, &(pongo2.Error{Sender: "filter:required", ErrorMsg: "required parameter missing"}) +func templateRequired(s string) (interface{}, error) { + if len(s) > 0 { + return s, nil } else { - return in, nil + return s, errors.New(fmt.Sprintf("variable not provided to template")) } } - -func init() { - pongo2.RegisterFilter("required", filterRequired) -} diff --git a/core/util.go b/core/util.go index c67ec1d..7d28ca4 100644 --- a/core/util.go +++ b/core/util.go @@ -59,6 +59,17 @@ func mergeEnv(first map[string]string, next map[string]string) map[string]string return first } +func union(first map[string]string, next map[string]string) map[string]string { + res := make(map[string]string) + for k, v := range first { + res[k] = v + } + for k, v := range next { + res[k] = v + } + return res +} + func normalizeEnvPaths(cwd string, paths string) string { newPaths := []string{} for _, path := range strings.Split(strings.TrimSpace(paths), PathSep) { diff --git a/examples/template/myke.yml b/examples/template/myke.yml index 5b1ec6a..07ef7c0 100644 --- a/examples/template/myke.yml +++ b/examples/template/myke.yml @@ -6,8 +6,8 @@ env: PARAM2: value2 tasks: args: - cmd: echo {{args.from|required}} {{args.to|default:"something_to"}} + cmd: echo {{.from|required}} {{.to|default "something_to"}} desc: run as myke template/args[from=...,to=...] file: - cmd: myke template template.tpl + cmd: myke --template template.tpl desc: run as myke template/file diff --git a/examples/template/template.tpl b/examples/template/template.tpl index 1b7aa96..fce5a7c 100644 --- a/examples/template/template.tpl +++ b/examples/template/template.tpl @@ -1,3 +1,3 @@ I am a template. -PARAM1={{env.PARAM1}} -PARAM2={{env.PARAM2}} +PARAM1={{.PARAM1}} +PARAM2={{.PARAM2}} diff --git a/main.go b/main.go index b4155c6..c3bc383 100644 --- a/main.go +++ b/main.go @@ -10,21 +10,23 @@ import ( func main() { app := cli.NewApp() app.Name = "myke" - app.Version = "0.3.2" + app.Version = cmd.Version() app.Usage = "make with yml" - app.Action = cmd.RunOrList + app.Action = cmd.Action app.Flags = []cli.Flag { cli.StringFlag{ Name: "f, file", Value: "myke.yml", Usage: "`yml-file` to load", }, - } - app.Commands = []cli.Command{ - cmd.ListCmd, - cmd.RunCmd, - cmd.TemplateCmd, - cmd.LicenseCmd, + cli.StringFlag{ + Name: "template", + Usage: "render template `tpl-file` (will not run any command)", + }, + cli.BoolFlag{ + Name: "license", + Usage: "show license", + }, } if err := app.Run(os.Args); err != nil { log.Fatal(err)